8000 gh-110205: Fix asyncio ThreadedChildWatcher._join_threads() (#110884) · python/cpython@c3bb10c · GitHub
[go: up one dir, main page]

Skip to content

Commit c3bb10c

Browse files
authored
gh-110205: Fix asyncio ThreadedChildWatcher._join_threads() (#110884)
- `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`.
1 parent 1c9a0c4 commit c3bb10c

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
@@ -1371,14 +1371,7 @@ def is_active(self):
13711371
return True
13721372

13731373
def close(self):
1374-
self._join_threads()
1375-
1376-
def _join_threads(self):
1377-
"""Internal: Join all non-daemon threads"""
1378-
threads = [thread for thread in list(self._threads.values())
1379-
if thread.is_alive() and not thread.daemon]
1380-
for thread in threads:
1381-
thread.join()
1374+
pass
13821375

13831376
def __enter__(self):
13841377
return self
@@ -1397,7 +1390,7 @@ def __del__(self, _warn=warnings.warn):
13971390
def add_child_handler(self, pid, callback, *args):
13981391
loop = events.get_running_loop()
13991392
thread = threading.Thread(target=self._do_waitpid,
1400-
name=f"waitpid-{next(self._pid_counter)}",
1393+
name=f"asyncio-waitpid-{next(self._pid_counter)}",
14011394
args=(loop, pid, callback, args),
14021395
daemon=True)
14031396
self._threads[pid] = thread

Lib/test/test_asyncio/utils.py

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

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

0 commit comments

Comments
 (0)
0