Skip to content

Commit 4c716e5

Browse files
committed
fix: register shutting down process pool for early execution
1 parent 11ec058 commit 4c716e5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

python/cocoindex/subprocess_exec.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import asyncio
2020
import os
2121
import time
22-
import atexit
2322
from .user_app_loader import load_user_app
2423
from .runtime import execution_context
2524
import logging
@@ -63,10 +62,18 @@ def _get_pool() -> ProcessPoolExecutor:
6362
with _pool_lock:
6463
if _pool is None:
6564
if not _pool_cleanup_registered:
66-
# Register the shutdown at exit at creation time (rather than at import time)
67-
# to make sure it's executed earlier in the shutdown sequence.
68-
atexit.register(_shutdown_pool_at_exit)
69-
_pool_cleanup_registered = True
65+
# CRITICAL: register with threading's early-exit list
66+
reg = getattr(threading, "_register_atexit", None)
67+
if callable(reg):
68+
reg(_shutdown_pool_at_exit) # goes ahead of normal atexit
69+
_pool_cleanup_registered = True
70+
else:
71+
# Fallback for odd Pythons: normal atexit (will run later)
72+
import atexit
73+
74+
if not _pool_cleanup_registered:
75+
atexit.register(_shutdown_pool_at_exit)
76+
_pool_cleanup_registered = True
7077

7178
# Single worker process as requested
7279
_pool = ProcessPoolExecutor(

0 commit comments

Comments
 (0)