You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/01/13 10:20:55 UTC
(groovy) 01/02: GROOVY-8373: Fix resource leak in filterLine Writable.writeTo method
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_5_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 2bc8dab4c301451b9bf0941afe6a48051084fced
Author: Jochen Theodorou <bl...@gmx.org>
AuthorDate: Wed Dec 10 18:07:15 2025 +0100
GROOVY-8373: Fix resource leak in filterLine Writable.writeTo method
The filterLine method returns a Writable whose writeTo method reads from
a BufferedReader but never closes it, causing the underlying file handle
to remain open and cause resource leaks.
This fix wraps the filtering logic in writeTo within a try-finally block
to ensure the BufferedReader is closed after use.
---
.../codehaus/groovy/runtime/IOGroovyMethods.java | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/IOGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/IOGroovyMethods.java
index e104966b6d..671cc32942 100644
--- a/src/main/java/org/codehaus/groovy/runtime/IOGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/IOGroovyMethods.java
@@ -1500,17 +1500,21 @@ public class IOGroovyMethods extends DefaultGroovyMethodsSupport {
return new Writable() {
@Override
public Writer writeTo(Writer out) throws IOException {
- BufferedWriter bw = new BufferedWriter(out);
- String line;
- BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
- while ((line = br.readLine()) != null) {
- if (bcw.call(line)) {
- bw.write(line);
- bw.newLine();
+ try {
+ BufferedWriter bw = new BufferedWriter(out);
+ String line;
+ BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure);
+ while ((line = br.readLine()) != null) {
+ if (bcw.call(line)) {
+ bw.write(line);
+ bw.newLine();
+ }
}
+ bw.flush();
+ return out;
+ } finally {
+ closeWithWarning(br);
}
- bw.flush();
- return out;
}
@Override