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

Flushing channels on destroy #78

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

darkestpigeon
Copy link

Right now when a channel with pending messages is destroyed, a memory leak can occur if the messages themselves have non-trivial destructors. This PR aims to fix this via receiving all the pending messages during channel destruction.
Also adding tryRecv with Option[T} result instead of an out parameter and a boolean flag for convenience.

@@ -100,7 +100,7 @@ runnableExamples("--threads:on --gc:orc"):
when not (defined(gcArc) or defined(gcOrc) or defined(gcAtomicArc) or defined(nimdoc)):
{.error: "This module requires one of --mm:arc / --mm:atomicArc / --mm:orc compilation flags".}

import std/[locks, isolation, atomics]
import std/[locks, isolation, atomics, options]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah. I don't like options and nothing about this new API is hardly any better than the old one:

var x: T
if tryRecv(chan, x): ...

vs:

let x = tryRecv(chan) 
if x.isSome: ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough

@darkestpigeon
Copy link
Author

Found some apparent problems with the compiler due to this change, distilled and reported them in nim-lang/Nim#24374
Here the =destroy, and hence tryRecv can be called with a wrong type during flushing, which can lead to a segfault or other nasty behaviour.

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.

2 participants