Skip to content

Commit 8a38087

Browse files
committed
fix: register atexit for process pools later so it's executed earlier
1 parent 5eba07e commit 8a38087

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

python/cocoindex/subprocess_exec.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
# ---------------------------------------------
3333
_pool_lock = threading.Lock()
3434
_pool: ProcessPoolExecutor | None = None
35+
_pool_cleanup_registered = False
3536
_user_apps: list[str] = []
3637
_logger = logging.getLogger(__name__)
3738

@@ -52,13 +53,16 @@ def _shutdown_pool_at_exit() -> None:
5253
_pool = None
5354

5455

55-
atexit.register(_shutdown_pool_at_exit)
56-
57-
5856
def _get_pool() -> ProcessPoolExecutor:
59-
global _pool
57+
global _pool, _pool_cleanup_registered
6058
with _pool_lock:
6159
if _pool is None:
60+
if not _pool_cleanup_registered:
61+
# Register the shutdown at exit at creation time (rather than at import time)
62+
# to make sure it's executed earlier in the shutdown sequence.
63+
atexit.register(_shutdown_pool_at_exit)
64+
_pool_cleanup_registered = True
65+
6266
# Single worker process as requested
6367
_pool = ProcessPoolExecutor(
6468
max_workers=1,

0 commit comments

Comments
 (0)