Skip to content

Commit

Permalink
BUG: Terminate sub processes at exit (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingl2k1 authored Dec 12, 2024
1 parent 930b5da commit a30e253
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit a30e253

Please sign in to comment.