better-files icon indicating copy to clipboard operation
better-files copied to clipboard

file descriptor leaks when `file.children` is used without consuming it

Open scalway opened this issue 4 years ago • 1 comments

This is not better-files bug in fact but still this behavior is unexpected and not documented.

here is how I tested it. In linux shell:

$ ulimit -n 100 #limits how many files can be opened by subprocesses. ammonite needs ~40 then if should fail fast :)
$ amm

and then I paste parts of this scala code:

//common
import $ivy.`com.github.pathikrit::better-files:3.8.0`
import better.files._ 
def repeat[T](x: => T) = for (i <- 0 to 1000) { x }

//this will throw java.io.IOException: error=24, Too many open files ...
repeat { File("/home/slovic").children }

//this will throw java.io.IOException: error=24, Too many open files ...
//toSeq doesn't help because it returns lazy stream
repeat { File("/home/scalway").children.toSeq }

//this will throw java.io.IOException: error=24, Too many open files ...
while(true) { File("/home/scalway").children.find(x => true) }

//this is OK
repeat { File("/home/").children.toIndexedSeq }

//this is OK
repeat { File("/home/").children.foreach(x => ()) }

scalway avatar Apr 17 '20 10:04 scalway

Even worse is that it keeps resources when you call for example find(...) on it, and to be honest I consider it as a serious BUG.

//this will throw java.io.IOException: error=24, Too many open files ...
while(true) { File("/home/scalway").children.find(x => true) }

scalway avatar Apr 22 '20 14:04 scalway