nim-lang / threading

New atomics, thread primitives, atomic refcounting for --gc:arc/orc.
MIT License
73 stars 12 forks source link

Flushing channels on destroy #78

Open darkestpigeon opened 1 week ago

darkestpigeon commented 1 week ago

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.

darkestpigeon commented 1 day ago

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.