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

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 53c5fb61c5 GROOVY-8373: Fix resource leak in filterLine 
Writable.writeTo method
53c5fb61c5 is described below

commit 53c5fb61c56b4db6041f5d7de8a2aedbfd997251
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 4cc24f1a20..5270a7f716 100644
--- a/src/main/java/org/codehaus/groovy/runtime/IOGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/IOGroovyMethods.java
@@ -1499,17 +1499,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