Skip to content

Commit 5eba07e

Browse files
committed
fix: explicitly shutdown process pool at exit
1 parent e991881 commit 5eba07e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

python/cocoindex/subprocess_exec.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import asyncio
2020
import os
2121
import time
22+
import atexit
2223
from .user_app_loader import load_user_app
2324
from .runtime import execution_context
2425
import logging
@@ -35,6 +36,25 @@
3536
_logger = logging.getLogger(__name__)
3637

3738

39+
def _shutdown_pool_at_exit() -> None:
40+
"""Best-effort shutdown of the global ProcessPoolExecutor on interpreter exit."""
41+
global _pool
42+
with _pool_lock:
43+
if _pool is not None:
44+
try:
45+
_pool.shutdown(cancel_futures=True)
46+
except Exception:
47+
_logger.debug(
48+
"Error during ProcessPoolExecutor shutdown at exit",
49+
exc_info=True,
50+
)
51+
finally:
52+
_pool = None
53+
54+
55+
atexit.register(_shutdown_pool_at_exit)
56+
57+
3858
def _get_pool() -> ProcessPoolExecutor:
3959
global _pool
4060
with _pool_lock:

0 commit comments

Comments
 (0)