Skip to content

Commit

Permalink
[SPARK-47455][BUILD] Fix resource leak during the initialization of `…
Browse files Browse the repository at this point in the history
…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 <yangjie01@baidu.com>
Signed-off-by: yangjie01 <yangjie01@baidu.com>
  • Loading branch information
LuciferYang committed Mar 20, 2024
1 parent c3a04fa commit 85bf761
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions project/SparkBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,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)
Expand Down

0 comments on commit 85bf761

Please sign in to comment.