mridulm commented on code in PR #48483:
URL: https://github.com/apache/spark/pull/48483#discussion_r1885759273


##########
common/utils/src/main/scala/org/apache/spark/util/SparkErrorUtils.scala:
##########
@@ -48,6 +48,22 @@ private[spark] trait SparkErrorUtils extends Logging {
     try f.apply(resource) finally resource.close()
   }
 
+  /**
+   * Try to initialize a resource. If an exception is throw during 
initialization, closes the
+   * resource before propagating the error. Otherwise, the caller is 
responsible for closing
+   * the resource. This means that [[T]] should provide some way to close the 
resource.
+   */
+  def tryInitializeResource[R <: Closeable, T](createResource: => 
R)(initialize: R => T): T = {
+    val resource = createResource
+    try {
+      initialize(resource)
+    } catch {
+      case e: Throwable =>
+        resource.close()
+        throw e

Review Comment:
   If `close` results in an `IOException`, `e` will be lost - we should catch 
and add it via `addSuppressed`.
   Do you want to create a follow up PR for this @davintjong-db ?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to