Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Terminate sub processes at exit #119

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions python/xoscar/backends/indigen/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from __future__ import annotations

import asyncio
import atexit
import concurrent.futures as futures
import configparser
import contextlib
Expand All @@ -29,6 +30,7 @@
import threading
import uuid
from dataclasses import dataclass
from multiprocessing import util
from types import TracebackType
from typing import List, Optional

Expand Down Expand Up @@ -79,6 +81,19 @@ def _mp_kill(self):
_init_main_suspended_local = threading.local()


def _terminate_children():
for c in multiprocessing.active_children():
try:
c.terminate()
except Exception:
pass


if util:
# Import multiprocessing.util to register _exit_function at exit.
atexit.register(_terminate_children)


def _patch_spawn_get_preparation_data():
try:
from multiprocessing import spawn as mp_spawn
Expand Down Expand Up @@ -309,6 +324,7 @@ async def _create_sub_pool(
raise
finally:
status_queue.put(process_status)
status_queue.cancel_join_thread()
await pool.join()

async def append_sub_pool(
Expand Down
2 changes: 1 addition & 1 deletion python/xoscar/backends/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ async def _send_channel(
try:
await channel.send(result)
except (ChannelClosed, ConnectionResetError):
if not self._stopped.is_set():
if not self._stopped.is_set() and not channel.closed:
raise
except Exception as ex:
logger.exception(
Expand Down
Loading