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

blackdrag pushed a commit to branch GROOVY-8373-fix-hanging-reader
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit f3b6fdd794171fe8992b4024f881dec1c974f97a
Author: Jochen Theodorou <[email protected]>
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

Reply via email to