Skip to content

fix: make EventWriter thread-safe to prevent event loss during cleanup#55

Merged
nh13 merged 1 commit intomainfrom
nh/thread-safe-event-writer
Mar 28, 2026
Merged

fix: make EventWriter thread-safe to prevent event loss during cleanup#55
nh13 merged 1 commit intomainfrom
nh/thread-safe-event-writer

Conversation

@nh13
Copy link
Copy Markdown
Collaborator

@nh13 nh13 commented Mar 28, 2026

Summary

  • Adds threading.Lock and _closed flag to EventWriter so that close() and write() are mutually exclusive
  • Writes after close() are silently ignored instead of crashing the QueueListener thread
  • close() is now idempotent (safe to call multiple times)

Root cause: Snakemake's _cleanup() calls handler.close() from the main thread while the QueueListener background thread may still be delivering events via emit(). Without thread safety, this race causes a ValueError (I/O on closed file) that crashes the background thread, silently dropping remaining events. Filed upstream as snakemake/snakemake#4136.

Test plan

  • New test: concurrent write + close from separate threads doesn't crash or corrupt
  • New test: write after close is silently ignored
  • New test: double close is safe
  • All 33 existing logger plugin tests pass

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved thread-safety for concurrent logging operations to prevent data races.
    • Made the close operation idempotent—safe to call multiple times without side effects.
    • Write operations after closure are now safely ignored.
  • Tests

    • Added tests for concurrent write and close scenarios.
    • Added tests for edge cases in close operations.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

Warning

Rate limit exceeded

@nh13 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 9 minutes and 49 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 9 minutes and 49 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 698f29cb-7980-4f19-8d30-3378bbc1b4b5

📥 Commits

Reviewing files that changed from the base of the PR and between 7fd2b45 and 9d507b8.

📒 Files selected for processing (2)
  • snakemake-logger-plugin-snakesee/src/snakemake_logger_plugin_snakesee/writer.py
  • snakemake-logger-plugin-snakesee/tests/test_writer.py
📝 Walkthrough

Walkthrough

Thread-safety synchronization is added to EventWriter using a threading.Lock to prevent race conditions with background QueueListener threads. The implementation introduces idempotent closure behavior and silent handling of writes after closure, with supporting test coverage for concurrent scenarios.

Changes

Cohort / File(s) Summary
Thread-Safety Implementation
src/snakemake_logger_plugin_snakesee/writer.py
Added threading.Lock-based synchronization to gate truncate(), write(), flush(), and close() methods. Introduced _closed state flag to make close() idempotent and write() a no-op post-closure. Refactored with new private _flush_locked() method invoked within lock scope.
Concurrency Test Coverage
tests/test_writer.py
Added three new test cases: test_concurrent_write_and_close (validates concurrent writes during close), test_write_after_close_is_silent (verifies post-close writes are ignored), and test_double_close (ensures closure idempotency). Added threading import.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A writer that raced with the thread,
Now locked down and perfectly spread,
No race condition fright,
Just synchronized might—
Safe events in a queue, clean and fed! 🔒

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately summarizes the main change: adding thread-safety to EventWriter to prevent event loss during cleanup, which is the core objective of the PR.
Description check ✅ Passed The PR description is comprehensive and follows the template with all key sections completed: summary of changes, root cause analysis, test plan with checkmarks, and reference to upstream issue.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nh/thread-safe-event-writer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.47%. Comparing base (b0e3a49) to head (9d507b8).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #55   +/-   ##
=======================================
  Coverage   88.47%   88.47%           
=======================================
  Files          48       48           
  Lines        4684     4684           
=======================================
  Hits         4144     4144           
  Misses        540      540           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
snakemake-logger-plugin-snakesee/src/snakemake_logger_plugin_snakesee/writer.py (1)

114-123: Consider: Exception during flush could leak the file handle.

If _flush_locked() raises an exception (e.g., disk full, I/O error), _closed is already True but self._file is never closed. Subsequent calls to close() will return early due to the idempotency check, leaving the file handle leaked.

🛡️ Proposed fix using try/finally
     def close(self) -> None:
         """Close the file and flush any remaining events."""
         with self._lock:
             if self._closed:
                 return
             self._closed = True
-            self._flush_locked()
-            if self._file is not None:
-                self._file.close()
-                self._file = None
+            try:
+                self._flush_locked()
+            finally:
+                if self._file is not None:
+                    self._file.close()
+                    self._file = None
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@snakemake-logger-plugin-snakesee/src/snakemake_logger_plugin_snakesee/writer.py`
around lines 114 - 123, The close method sets self._closed before calling
_flush_locked, so if _flush_locked raises the file handle (self._file) can be
leaked; update Writer.close to call _flush_locked inside a try/finally (or
similar) so that regardless of exceptions you still close and set self._file to
None and preserve idempotency: call _flush_locked() in the try, and in the
finally block check and close self._file and set it to None; ensure you still
set self._closed (or only set it after successful/guaranteed cleanup depending
on desired semantics) and keep the locking around these operations to avoid
races.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@snakemake-logger-plugin-snakesee/src/snakemake_logger_plugin_snakesee/writer.py`:
- Around line 114-123: The close method sets self._closed before calling
_flush_locked, so if _flush_locked raises the file handle (self._file) can be
leaked; update Writer.close to call _flush_locked inside a try/finally (or
similar) so that regardless of exceptions you still close and set self._file to
None and preserve idempotency: call _flush_locked() in the try, and in the
finally block check and close self._file and set it to None; ensure you still
set self._closed (or only set it after successful/guaranteed cleanup depending
on desired semantics) and keep the locking around these operations to avoid
races.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 170a1d92-72a4-408c-9926-de9d94a7c07c

📥 Commits

Reviewing files that changed from the base of the PR and between b0e3a49 and 7fd2b45.

📒 Files selected for processing (2)
  • snakemake-logger-plugin-snakesee/src/snakemake_logger_plugin_snakesee/writer.py
  • snakemake-logger-plugin-snakesee/tests/test_writer.py

Snakemake's _cleanup() calls handler.close() from the main thread while
the QueueListener background thread may still be delivering events via
emit(). This race condition causes a ValueError (I/O on closed file)
that crashes the QueueListener thread, silently dropping remaining
events.

Add a threading.Lock and _closed flag to EventWriter so that close()
and write() are mutually exclusive. Writes after close() are silently
ignored instead of crashing. close() is now idempotent.

See: snakemake/snakemake#4136
@nh13 nh13 force-pushed the nh/thread-safe-event-writer branch from 7fd2b45 to 9d507b8 Compare March 28, 2026 16:41
@nh13 nh13 merged commit 055d169 into main Mar 28, 2026
8 checks passed
@nh13 nh13 deleted the nh/thread-safe-event-writer branch March 28, 2026 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant