Skip to content

move remove_done_callback to finally block to fix RuntimeError('Event loop stopped before Future completed') #106

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

Merged
merged 2 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ async def foo():
with self.assertRaisesRegex(ValueError, 'aaa'):
self.loop.run_until_complete(foo())

def test_run_until_complete_loop_orphan_future_close_loop(self):
async def foo(sec=0):
await asyncio.sleep(sec)
self.loop.close()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
with mock.patch('asyncio.base_events.BaseEventLoop.run_forever', side_effect=Exception):
loop.run_until_complete(foo())
except:
pass
loop.run_until_complete(foo(0.1))
loop.close()

def test_debug_slow_callbacks(self):
logger = logging.getLogger('asyncio')
self.loop.set_debug(True)
Expand Down
3 changes: 2 additions & 1 deletion uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,8 @@ cdef class Loop:
# local task.
future.exception()
raise
future.remove_done_callback(done_cb)
finally:
future.remove_done_callback(done_cb)
if not future.done():
raise RuntimeError('Event loop stopped before Future completed.')

Expand Down