Skip to content
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

Merged

Conversation

mpilquist
Copy link
Member

Fixes #987.

@@ -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)
Copy link
Member Author

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.

@SystemFw
Copy link
Collaborator

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
Copy link
Member Author

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.

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.

Copy link

@lukestephenson lukestephenson left a 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.

@mpilquist mpilquist merged commit 3a45c66 into typelevel:series/0.10 Nov 24, 2017
@mpilquist mpilquist deleted the topic/leak-in-readAllFromFileHandle branch November 27, 2017 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants