fix(session): atomic file locking to prevent race conditions in all harnesses#94
Merged
yuh-yang merged 1 commit intoHKUDS:mainfrom Mar 17, 2026
Merged
Conversation
…ll harnesses Closes HKUDS#51 The root cause: all 10 harnesses used bare `open("w") + json.dump()` for session saves. `open("w")` truncates the file before any lock can be acquired, so concurrent writes silently corrupt or lose data. Fix: add `_locked_save_json()` to each session.py that opens with "r+" (no truncation), acquires fcntl.LOCK_EX, then truncates inside the lock. Falls back gracefully on Windows where fcntl is unavailable. Also: - Update HARNESS.md with session file locking guidance (per maintainer request on PR HKUDS#52) - Add concurrent write tests (4 threads × 50 writes) to verify the fix Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
|
A mirror of #73. I'm merging this one which is cleaner and less noisy. |
5 tasks
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
Closes #51
_locked_save_json()to all 10 harnesses'session.py— usesopen("r+")+fcntl.LOCK_EXto prevent concurrent writes from corrupting session filesHARNESS.mdwith session file locking guidance (as requested by @yuh-yang in PR fix(session): add file locking to prevent race conditions in multi-session sync #52)The problem
Every harness saved session JSON with bare
open("w") + json.dump(). Three compounding bugs:open("w")truncates before any lock is acquiredjson.dump()calls interleave bytes → invalid JSONflush()before closeThe fix
Key properties:
"r+"mode avoids zeroing the file before the lockflush()before unlock — ensures data is written before other processes can readfinallyblock — lock is always released, even on exceptionsfcntlimport failure gracefully falls back to unlocked writesFiles changed (12)
*/agent-harness/cli_anything/*/core/session.py— all 10 harnessescli-anything-plugin/HARNESS.md— added session locking guidancegimp/agent-harness/cli_anything/gimp/tests/test_core.py— 5 new testsDesign decision: local copies, not a shared package
Per discussion in PR #73, the
_locked_save_jsonfunction is kept as a local copy in each harness rather than a shared package. This avoids adding distribution complexity for a single utility function.Test plan
🤖 Generated with Claude Code