This is an automated email from the ASF dual-hosted git repository.

yangjie01 pushed a commit to branch branch-3.4
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-3.4 by this push:
     new 142677bcd203 [SPARK-47455][BUILD] Fix resource leak during the 
initialization of `scalaStyleOnCompileConfig` in `SparkBuild.scala`
142677bcd203 is described below

commit 142677bcd203caf2b6d07bf41d654e123d910ee8
Author: yangjie01 <yangji...@baidu.com>
AuthorDate: Wed Mar 20 15:19:33 2024 +0800

    [SPARK-47455][BUILD] Fix resource leak during the initialization of 
`scalaStyleOnCompileConfig` in `SparkBuild.scala`
    
    ### What changes were proposed in this pull request?
    
https://github.com/apache/spark/blob/e01ed0da22f24204fe23143032ff39be7f4b56af/project/SparkBuild.scala#L157-L173
    
    `Source.fromFile(in)` opens a `BufferedSource` resource handle, but it does 
not close it, this pr fix this issue.
    
    ### Why are the changes needed?
    Close resource after used.
    
    ### Does this PR introduce _any_ user-facing change?
    No
    
    ### How was this patch tested?
    Pass GitHub Actions
    
    ### Was this patch authored or co-authored using generative AI tooling?
    No
    
    Closes #45582 from LuciferYang/SPARK-47455.
    
    Authored-by: yangjie01 <yangji...@baidu.com>
    Signed-off-by: yangjie01 <yangji...@baidu.com>
    (cherry picked from commit 85bf7615f85eea3e9192a7684ef711cf44042e05)
    Signed-off-by: yangjie01 <yangji...@baidu.com>
---
 project/SparkBuild.scala | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/project/SparkBuild.scala b/project/SparkBuild.scala
index 31263eaa4c8d..31516c8c6ffe 100644
--- a/project/SparkBuild.scala
+++ b/project/SparkBuild.scala
@@ -159,16 +159,21 @@ object SparkBuild extends PomBuild {
     val replacements = Map(
       """customId="println" level="error"""" -> """customId="println" 
level="warn""""
     )
-    var contents = Source.fromFile(in).getLines.mkString("\n")
-    for ((k, v) <- replacements) {
-      require(contents.contains(k), s"Could not rewrite '$k' in original 
scalastyle config.")
-      contents = contents.replace(k, v)
-    }
-    new PrintWriter(out) {
-      write(contents)
-      close()
+    val source = Source.fromFile(in)
+    try {
+      var contents = source.getLines.mkString("\n")
+      for ((k, v) <- replacements) {
+        require(contents.contains(k), s"Could not rewrite '$k' in original 
scalastyle config.")
+        contents = contents.replace(k, v)
+      }
+      new PrintWriter(out) {
+        write(contents)
+        close()
+      }
+      out
+    } finally {
+      source.close()
     }
-    out
   }
 
   // Return a cached scalastyle task for a given configuration (usually 
Compile or Test)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to