LuciferYang opened a new issue, #3813:
URL: https://github.com/apache/celeborn/issues/3813

   ### Problem
   
   `Utils.tryWithResources` declares its resource parameter as by-name (`f: => 
R`) but references it twice in the body:
   
   ```scala
   def tryWithResources[R <: Closeable, U](f: => R)(func: R => U): U = {
     val res = f        // evaluation #1
     try {
       func(f)          // evaluation #2 — re-executes the caller's expression
     } finally {
       if (null != res) {
         res.close()    // closes instance #1, not the one handed to func
       }
     }
   }
   ```
   
   Since `f` is by-name, every reference re-executes the caller's expression. 
The instance passed to `func` is therefore a *different* instance from the one 
closed in `finally`: one resource is leaked per call, and non-idempotent 
expressions run twice.
   
   ### Affected call sites (current main)
   
   - `cli/.../CliConfigManager.scala:42` — `new FileInputStream(file)`: file 
opened twice per read, one stream leaked (blocks file deletion on Windows)
   - `cli/.../CliVersionProvider.scala:37` — `Source.fromFile(...)`: 
BufferedSource opened twice, one leaked
   
   ### Reproduce
   
   ```scala
   val evaluations = new AtomicInteger(0)
   val received = Utils.tryWithResources {
     evaluations.incrementAndGet()
     new Closeable { var closed = false; def close() = closed = true }
   }(res => res)
   // current main: evaluations == 2 and received.closed == false (leaked)
   ```
   
   ### Expected
   
   Evaluate the expression exactly once, and close the same instance that was 
handed to the function.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to