Github user stephenh commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5223#discussion_r27590863
  
    --- Diff: core/src/main/scala/org/apache/spark/util/Utils.scala ---
    @@ -1214,6 +1214,44 @@ private[spark] object Utils extends Logging {
         }
       }
     
    +  /**
    +   * Execute a block of code, then a finally block, but if exceptions 
happen in
    +   * the finally block, do not suppress the origin exception.
    +   *
    +   * This is primarily an issue with `finally { out.close() }` blocks, 
where
    +   * close needs to be called to clean up `out`, but if an exception 
happened
    +   * in `out.write`, it's likely `out` may be corrupted and `out.close` 
will
    +   * fail as well. This would then suppress the original/likely more 
meaningful
    +   * exception from the original `out.write` call.
    +   */
    +  def tryWithSafeFinally[T](block: => T)(finallyBlock: => Unit): T = {
    +    // It would be nice to find a method on Try that did this
    +    var originalThrowable: Throwable = null
    +    try {
    +      block
    +    } catch {
    +      case t: Throwable =>
    +        // Purposefully not using NonFatal, because even fatal exceptions
    +        // we don't want to have our finallyBlock suppress
    +        originalThrowable = t
    +        throw originalThrowable
    +    } finally {
    +      try {
    +        finallyBlock
    +      } catch {
    +        case t: Throwable =>
    +          if (originalThrowable != null) {
    +            // We could do originalThrowable.addSuppressed(t), but it's
    +            // not available in JDK 1.6.
    +            logWarning(s"Skipping exception that happening in finally", t)
    +            throw originalThrowable
    --- End diff --
    
    Sure, will add.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to