Skip to content

Commit fdccfe0

Browse files
authored
bpo-33469: RuntimeError after closing loop that used run_in_executor (GH-7171)
1 parent f9b364f commit fdccfe0

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

‎Lib/asyncio/futures.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ def _call_check_cancel(destination):
353353
source_loop.call_soon_threadsafe(source.cancel)
354354

355355
def _call_set_state(source):
356+
if (destination.cancelled() and
357+
dest_loop is not None and dest_loop.is_closed()):
358+
return
356359
if dest_loop is None or dest_loop is source_loop:
357360
_set_state(destination, source)
358361
else:

‎Lib/test/test_asyncio/test_events.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,24 @@ def run(arg):
347347
self.assertEqual(res, 'yo')
348348
self.assertNotEqual(thread_id, threading.get_ident())
349349

350+
def test_run_in_executor_cancel(self):
351+
called = False
352+
353+
def patched_call_soon(*args):
354+
nonlocal called
355+
called = True
356+
357+
def run():
358+
time.sleep(0.05)
359+
360+
f2 = self.loop.run_in_executor(None, run)
361+
f2.cancel()
362+
self.loop.close()
363+
self.loop.call_soon = patched_call_soon
364+
self.loop.call_soon_threadsafe = patched_call_soon
365+
time.sleep(0.4)
366+
self.assertFalse(called)
367+
350368
def test_reader_callback(self):
351369
r, w = socket.socketpair()
352370
r.setblocking(False)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix RuntimeError after closing loop that used run_in_executor

0 commit comments

Comments
 (0)