Skip to content

Instantly share code, notes, and snippets.

@cablehead
Last active October 16, 2015 19:09
Show Gist options
  • Save cablehead/0be5e29329278aec5b97 to your computer and use it in GitHub Desktop.
Save cablehead/0be5e29329278aec5b97 to your computer and use it in GitHub Desktop.
Abandoned coroutines in Vanilla

Ben Bangert raised an excellent point about leaking abandoned coroutines in a recent talk:

https://docs.google.com/presentation/d/1LO_WI3N-3p2Wp9PDWyv5B6EGFZ8XTOTNJ7Hd40WOUHo/edit#slide=id.g70b0035b2_1_168

This gist demonstrates a design decision in Vanilla to attempt to help with this.

The sender, recver ends of pipes are only connected by weakref's which have callback's registered for when the corresponding end is garbage collected. When one end is blocked on a send or a recv, and the other end is abandoned without being closed explicitly the blocked coroutine will receive a vanilla.Abandoned exception. This will also result in a StopIteration exception if you are iterating over a recver.

import vanilla
h = vanilla.Hub()
def produce():
sender, recver = h.pipe()
@h.spawn
def _():
for i in xrange(3):
sender.send(i)
# sender will be abandoned here and garbage collected.
return recver
items = produce()
for i in items:
print(i)
print("recver closed.")
hop:~ andy$ python ~/tmp/abandon.py
0
1
2
recver closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment