8000 [3.12] gh-110205: Fix asyncio ThreadedChildWatcher._join_threads() (G… · python/cpython@2398036 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2398036

Browse files
[3.12] gh-110205: Fix asyncio ThreadedChildWatcher._join_threads() (GH-110884) (#111412)
- `ThreadedChildWatcher.close()` is now *officially* a no-op; `_join_threads()` never did anything. - Threads created by that class are now named `asyncio-waitpid-NNN`. - `test.test_asyncio.utils.TestCase.close_loop()` now waits for the child watcher's threads, but not forever; if a thread hangs, it raises `RuntimeError`. (cherry picked from commit c3bb10c) Co-authored-by: Guido van Rossum <guido@python.org>
1 parent 754fda8 commit 2398036

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

Lib/asyncio/unix_events.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,14 +1367,7 @@ def is_active(self):
13671367
return True
13681368

13691369
def close(self):
1370-
self._join_threads()
1371-
1372-
def _join_threads(self):
1373-
"""Internal: Join all non-daemon threads"""
1374-
threads = [thread for thread in list(self._threads.values())
1375-
if thread.is_alive() and not thread.daemon]
1376-
for thread in threads:
1377-
thread.join()
1370+
pass
13781371

13791372
def __enter__(self):
13801373
return self
@@ -1393,7 +1386,7 @@ def __del__(self, _warn=warnings.warn):
13931386
def add_child_handler(self, pid, callback, *args):
13941387
loop = events.get_running_loop()
13951388
thread = threading.Thread(target=self._do_waitpid,
1396-
name=f"waitpid-{next(self._pid_counter)}",
1389+
name=f"asyncio-waitpid-{next(self._pid_counter)}",
13971390
args=(loop, pid, callback, args),
13981391
daemon=True)
13991392
self._threads[pid] = thread

Lib/test/test_asyncio/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ def close_loop(loop):
547547
else:
548548
loop._default_executor.shutdown(wait=True)
549549
loop.close()
550+
550551
policy = support.maybe_get_event_loop_policy()
551552
if policy is not None:
552553
try:
@@ -558,9 +559,13 @@ def close_loop(loop):
558559
pass
559560
else:
560561
if isinstance(watcher, asyncio.ThreadedChildWatcher):
561-
threads = list(watcher._threads.values())
562-
for thread in threads:
563-
8072 thread.join()
562+
# Wait for subprocess to finish, but not forever
563+
for thread in list(watcher._threads.values()):
564+
thread.join(timeout=support.SHORT_TIMEOUT)
565+
if thread.is_alive():
566+
raise RuntimeError(f"thread {thread} still alive: "
567+
"subprocess still running")
568+
564569

565570
def set_event_loop(self, loop, *, cleanup=True):
566571
if loop is None:

0 commit comments

Comments
 (0)
0