fix(logging): prevent log file race condition under parallel attempts#1827
Open
nuthalapativarun wants to merge 2 commits into
Open
fix(logging): prevent log file race condition under parallel attempts#1827nuthalapativarun wants to merge 2 commits into
nuthalapativarun wants to merge 2 commits into
Conversation
When multiprocessing.Pool forks worker processes they inherit the parent's open FileHandler file descriptors. Concurrent writes (or reentrant flushes triggered by third-party library destructors such as openai/httpcore closing connections in __del__) on those shared handles cause a RuntimeError in Python 3.13+. Add a _worker_logging_init pool initializer that closes all inherited handlers and re-opens a fresh FileHandler in each worker, giving every worker its own private file descriptor. The log file path is read from the GARAK_LOG_FILE env var that garak/__init__.py already sets before spawning any children. Fixes NVIDIA#1355 Signed-off-by: Varun Nuthalapati <nuthalapativarun@gmail.com>
Collaborator
|
The fix description appears promising but can we obtain:
|
Adds tests/probes/test_probes_base_parallel_logging.py to reproduce and verify the fix for NVIDIA#1355. Four unit tests cover handler teardown, file handler creation from GARAK_LOG_FILE, absence of handler when env is unset, and idempotency. Two integration tests spawn a real Pool to confirm workers log concurrently without RuntimeError and that each worker opens its own private file descriptor rather than sharing the parent's inherited fd. Signed-off-by: Varun Nuthalapati <nuthalapativarun@gmail.com> Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
|
Thanks for the clear feedback, @aishwaryap! I've added Unit tests for
Integration tests that exercise the actual Pool path:
All six pass locally. Let me know if you'd like any adjustments. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1355.
When
multiprocessing.Poolforks worker processes they inherit the parent's openFileHandlerfile descriptor. Concurrent writes — or reentrant flushes triggered by third-party library destructors (e.g.openai/httpcoreclosing connections in__del__) — on those shared handles cause aRuntimeError: reentrant call inside <_io.BufferedWriter>in Python 3.13+.Fix: pass
initializer=_worker_logging_inittoPool(...)ingarak/probes/base.py. The initializer runs once in each worker process and:FileHandlervialogging.basicConfig, using theGARAK_LOG_FILEenv var thatgarak/__init__.pyalready sets before any children are spawned.Each worker then has its own private file descriptor pointing at the same log file, eliminating the shared-handle race.
Files changed
garak/probes/base.py— add_worker_logging_init()and pass it asinitializertoPoolTest plan
parallel_attempts: 2and a multi-probe run (e.g. the config from the issue comments) — confirm noRuntimeErrorappears in the log