Skip to content

Instantly share code, notes, and snippets.

@zerotypic
Last active December 4, 2020 08:00
Show Gist options
  • Select an option

  • Save zerotypic/74ac1a7d6b4b946c14c1ebd86de6027b to your computer and use it in GitHub Desktop.

Select an option

Save zerotypic/74ac1a7d6b4b946c14c1ebd86de6027b to your computer and use it in GitHub Desktop.
# Triggers a bug related to coroutines and stack frames.
# Run this in Python 3:
# $ python3 async_bug.py [bad]
# When the commandline argument "bad" is passed in, the bug is triggered, and the foo() coroutine never completes.
#
import asyncio
import sys
LOG = print
ev = asyncio.Event()
exn = None
foo_task = None
async def foo():
global exn
try:
LOG("foo: raising exception")
raise TypeError("blah")
except TypeError as e:
exn = e
LOG("foo: waiting for ev")
await ev.wait()
LOG("foo: ev set, quitting now.")
return "quit successfully"
async def bar(bad):
await asyncio.sleep(1)
LOG("bar: exn = {!r}".format(exn))
if bad:
LOG("bar: Clearing frame.")
exn.__traceback__.tb_frame.clear()
LOG("bar: setting ev")
ev.set()
await asyncio.sleep(1)
LOG("bar: foo_task = {!r}".format(foo_task))
bad = len(sys.argv) > 1 and sys.argv[1] == "bad"
foo_task = asyncio.get_event_loop().create_task(foo())
asyncio.get_event_loop().run_until_complete(bar(bad))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment