-
Notifications
You must be signed in to change notification settings - Fork 603
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
Fixed memory leak in readAllFromFileHandle due to use of for/yield #992
Fixed memory leak in readAllFromFileHandle due to use of for/yield #992
Conversation
@@ -111,9 +111,10 @@ private[file] object FileHandle { | |||
asyncCompletionHandler[F, Lock](f => chan.lock(position, size, shared, null, f)) | |||
|
|||
override def read(numBytes: Int, offset: Long): F[Option[Chunk[Byte]]] = { | |||
val buf = ByteBuffer.allocate(numBytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapped these buffer allocations in F.delay
so the returned action is safe to use more than once. Unrelated to memory leak.
wow nice catch |
private def _readAllFromFileHandle0[F[_]](chunkSize: Int, offset: Long)(h: FileHandle[F]): Pull[F, Byte, Unit] = for { | ||
res <- Pull.eval(h.read(chunkSize, offset)) | ||
next <- res.fold[Pull[F, Byte, Unit]](Pull.done)(o => Pull.output(o) *> _readAllFromFileHandle0(chunkSize, offset + o.size)(h)) | ||
} yield next |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This yield next
was the cause of the leak -- each time through this pull, we'd push a map(identity)
in to the FreeC
, which would accumulate on the return. In essence, the yield
results in the recursive call being out of tail position.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. It took me a while to realise what was going on here and I would have really struggled to find this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Looks good to me.
Fixes #987.