-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do file.isEmpty and file.nonEmpty have a resource leak? #241
Comments
I further looked into this by means of experiments:
val file = // some path to a directory
println(file.exists) // should print 'true'
// case 1
file.children.toList
// case 2
file.children.take(1).toList // note: the directory has more than 1 child
// case 3
file.nonEmpty For case 1 I found that the breakpoint on I also put similar breakpoints at other lines that I expected the 'dispose process' to hit and none of them where ever hit in cases 2 and 3, while being hit in case 1. |
When can we expect this bug fix to be released? |
@rvanheest : You can either depend on the snapshot versions for now or you can simply depend on a git hash:
|
Introduction
I was looking at the source code of this repository. I think it's very clever how you have hidden resource management for the
jStream
s that result from functions such asFiles.list
. As I understand it, ajStream
is implicitly converted to a ScalaIterator
like this:better-files/core/src/main/scala/better/files/Implicits.scala
Lines 340 to 342 in 021384b
The
toAutoClosedIterator
that is used here is used in that same file, just a couple of lines up:better-files/core/src/main/scala/better/files/Implicits.scala
Lines 311 to 321 in 021384b
Here is what triggered me in
toAutoClosedIterator
:In other words: if you don't iterate through all elements of the
Iterator
, the resource is not being closed.Code analysis
For my use case I wanted to check if a directory is not empty. So the easiest thing to do is to use the
file.nonEmpty
operator.better-files/core/src/main/scala/better/files/File.scala
Lines 1079 to 1087 in 021384b
When I trace this call to
file.isEmpty
, I find this implementation:better-files/core/src/main/scala/better/files/File.scala
Lines 1065 to 1077 in 021384b
For a directory this calls
file.children.isEmpty
, whereisEmpty
for anIterator
is implemented using!hasNext
. Note thatfile.children
implicitly uses thetoAutoClosedIterator
with its 'NOT closed if not depleted' statement.Question
This leads to the following question: do operators like
file.isEmpty
andfile.nonEmpty
close the underlyingjStream
that is produced infile.children
? After all, they do not deplete the stream with a singlehasNext
call. Therefore: do operators likefile.isEmpty
andfile.nonEmpty
cause resource leaks?The text was updated successfully, but these errors were encountered: