8000 bpo-33469: RuntimeError after closing loop that used run_in_executor … · python/cpython@a6d6bd7 · GitHub
[go: up one dir, main page]

Skip to content

Commit a6d6bd7

Browse files
miss-islington1st1
andauthored
bpo-33469: RuntimeError after closing loop that used run_in_executor (GH-7171)
(cherry picked from commit fdccfe0) Co-authored-by: Yury Selivanov <yury@magic.io>
1 parent d8af830 commit a6d6bd7

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
@@ -408,6 +408,9 @@ def _call_check_cancel(destination):
408408
source_loop.call_soon_threadsafe(source.cancel)
409409

410410
def _call_set_state(source):
411+
if (destination.cancelled() and
412+
dest_loop is not None and dest_loop.is_closed()):
413+
return
411414
if dest_loop is None or dest_loop is source_loop:
412415
_set_state(destination, source)
413416
else:

Lib/test/test_asyncio/test_events.py

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

365+
def test_run_in_executor_cancel(self):
366+
called = False
367+
368+
def patched_call_soon(*args):
369+
nonlocal called
370+
called = True
371+
372+
def run():
373+
time.sleep(0.05)
374+
375+
f2 = self.loop.run_in_executor(None, run)
376+
f2.cancel()
377+
self.loop.close()
378+
self.loop.call_soon = patched_call_soon
379+
self.loop.call_soon_threadsafe = patched_call_soon
380+
time.sleep(0.4)
381+
self.assertFalse(called)
382+
365383
def test_reader_callback(self):
366384
r, w = test_utils.socketpair()
367385
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)
0