fix(session): add file locking to prevent race conditions in multi-session sync#52
Open
JasonOA888 wants to merge 1 commit intoHKUDS:mainfrom
Open
fix(session): add file locking to prevent race conditions in multi-session sync#52JasonOA888 wants to merge 1 commit intoHKUDS:mainfrom
JasonOA888 wants to merge 1 commit intoHKUDS:mainfrom
Conversation
…ssion sync Fixes HKUDS#51 ## Root Cause _auto_save() writes to session file without any locking mechanism. When multiple CLI commands run concurrently (e.g., 'layer add' + 'text edit'), they can: 1. Both read the same state 2. Both write their changes 3. Result: lost updates (one write overwrites the other) ## Solution Add fcntl file locking (LOCK_EX) during save: - Exclusive lock during write - Atomic updates prevent lost data - Fallback for systems without flock support ## Testing - [x] Concurrent CLI commands no longer drop state - [x] Works on Linux/macOS (fcntl available) - [x] Graceful fallback on unsupported filesystems ## Impact Multi-session workflows now have consistent state sync.
Collaborator
|
great bug-finding. Can we improve HARNESS.md slightly to prevent this? |
JasonOA888
added a commit
to JasonOA888/CLI-Anything
that referenced
this pull request
Mar 13, 2026
Following PR HKUDS#52, add a new subsection to 'Critical Lessons Learned' documenting best practices for session file locking: - Use fcntl.flock() for exclusive locking during writes - Prevent race conditions in multi-session scenarios - Handle filesystems that don't support flock - Document common pitfalls (use fcntl.flock, not flock directly) This prevents future contributors from making similar mistakes when implementing session persistence. Co-authored-by: yuh-yang <yuh-yang@users.noreply.github.com>
sanjayrohith
added a commit
to sanjayrohith/CLI-Anything
that referenced
this pull request
Mar 14, 2026
Fixes HKUDS#51. When multiple CLI commands run concurrently against the same project file, concurrent writes to session/project JSON could silently overwrite each other due to missing file locking. Adds exclusive fcntl.flock() locking inside every save method across all 10 harnesses that write JSON state to disk: - gimp, blender, inkscape, audacity, libreoffice, obs-studio, kdenlive: save_session() in core/session.py - shotcut, drawio: save_session_state() in core/session.py - anygen: save() in core/session.py A try/except (ImportError, OSError) fallback ensures the write still proceeds on Windows (no fcntl) or unsupported filesystems. The lock is explicitly released after the write completes. PR HKUDS#52 addressed this for anygen only; this commit extends the same protection to all remaining harnesses. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
5 tasks
ZJZAC
added a commit
to ZJZAC/CLI-Anything
that referenced
this pull request
Mar 17, 2026
…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>
3 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.
Fixes #51
Root Cause
_auto_save()writes to session file without any locking mechanism. When multiple CLI commands run concurrently, they can both read the same state, then both write their changes, resulting in lost updates.Solution
Add fcntl file locking (LOCK_EX) during save:
Changes
Testing
Impact
Multi-session workflows now have consistent state sync.