Skip to content

Commit

Permalink
fix: Explicitly attach the event loop to the child watcher (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
achimnol committed Apr 25, 2022
1 parent 0f619cd commit 248fbed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/50.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Explicitly attach the event loop to the `PidfdChildWatcher` when first initialized
12 changes: 7 additions & 5 deletions src/aiotools/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,18 @@ def helper(*args, **kwargs):
main = _main_ctxmgr


def setup_child_watcher():
def setup_child_watcher(loop: asyncio.AbstractEventLoop) -> None:
try:
asyncio.get_child_watcher()
if hasattr(asyncio, 'PidfdChildWatcher'):
asyncio.set_child_watcher(asyncio.PidfdChildWatcher())
watcher = asyncio.PidfdChildWatcher()
asyncio.set_child_watcher(watcher)
watcher.attach_loop(loop)
except NotImplementedError:
pass # for uvloop


async def cancel_all_tasks():
async def cancel_all_tasks() -> None:
loop = get_running_loop()
cancelled_tasks = []
for task in all_tasks():
Expand Down Expand Up @@ -289,7 +291,7 @@ def _worker_main(
) -> int:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
setup_child_watcher()
setup_child_watcher(loop)
interrupted = asyncio.Event()
ctx = worker_actxmgr(loop, proc_idx, args)
if _cv_available:
Expand Down Expand Up @@ -551,7 +553,7 @@ def noop_main_ctxmgr():
main_future = main_loop.create_future()

# to make subprocess working in child threads
setup_child_watcher()
setup_child_watcher(main_loop)

# build a main-to-worker interrupt channel using signals
def handle_stop_signal(signum: signal.Signals) -> None:
Expand Down

0 comments on commit 248fbed

Please sign in to comment.