FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-110205: Fix asyncio ThreadedChildWatcher._join_threads() by vstinner · Pull Request #110790 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 7 additions & 3 deletions Lib/asyncio/unix_events.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1373,12 +1373,16 @@ def is_active(self):
def close(self):
self._join_threads()

def _join_threads(self):
def _join_threads(self, timeout=None):
"""Internal: Join all non-daemon threads"""
threads = [thread for thread in list(self._threads.values())
if thread.is_alive() and not thread.daemon]
for thread in threads:
thread.join()
thread.join(timeout)

# Clear references to terminated threads
self._threads[:] = [thread for thread in list(self._threads.values())
if thread.is_alive() and not thread.daemon]
Comment on lines +1384 to +1385

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

As I was trying to let you see for yourself, self._threads is a dict, not a list. If you want to update it using a comprehension you can use something like this:

self._threads = {key: thread for key, thread in self._threads.items() if thread.daemon or thread.is_alive()}

Please test locally before merging.


def __enter__(self):
return self
Expand All @@ -1397,7 +1401,7 @@ def __del__(self, _warn=warnings.warn):
def add_child_handler(self, pid, callback, *args):
loop = events.get_running_loop()
thread = threading.Thread(target=self._do_waitpid,
name=f"waitpid-{next(self._pid_counter)}",
name=f"asyncio-waitpid-{next(self._pid_counter)}",
args=(loop, pid, callback, args),
daemon=True)
self._threads[pid] = thread
Expand Down
9 changes: 6 additions & 3 deletions Lib/test/test_asyncio/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ def close_loop(loop):
else:
loop._default_executor.shutdown(wait=True)
loop.close()

policy = support.maybe_get_event_loop_policy()
if policy is not None:
try:
Expand All @@ -557,9 +558,11 @@ def close_loop(loop):
pass
else:
if isinstance(watcher, asyncio.ThreadedChildWatcher):
threads = list(watcher._threads.values())
for thread in threads:
thread.join()
watcher._join_threads(timeout=support.SHORT_TIMEOUT)
threads = watcher._threads
if threads:
self.fail(f"watcher still has running threads: "
f"{threads}")

def set_event_loop(self, loop, *, cleanup=True):
if loop is None:
Expand Down

Back | FazBrowse Home | New Git URL