From 1004b2e6555464779eea7a87b4b77c0a363bdc39 Mon Sep 17 00:00:00 2001 From: Alice Chen Date: Wed, 4 Mar 2026 13:28:41 -0800 Subject: [PATCH] fix: ensure batch processor flushes on interpreter shutdown Adds a more robust atexit handler that waits for the flush thread to complete. This fixes the race condition where the daemon thread is killed before the final flush. Closes #234 --- src/acme_sdk/utils/batching.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/acme_sdk/utils/batching.py b/src/acme_sdk/utils/batching.py index 0895a46..b9aa7f0 100644 --- a/src/acme_sdk/utils/batching.py +++ b/src/acme_sdk/utils/batching.py @@ -99,3 +99,13 @@ def shutdown(self, timeout: float | None = None) -> None: if timeout is None: timeout = self._flush_interval * 3 self._thread.join(timeout=timeout) + + +def _ensure_flush_on_shutdown(processor: BatchProcessor) -> None: + """Ensure final flush happens before interpreter exit. + + This is registered as an atexit handler and ensures that the + background flush thread completes before the interpreter exits. + """ + if not processor._shutdown: + processor.shutdown(timeout=processor._flush_interval * 3)