Skip to content

fix(session): add file locking to prevent race conditions in multi-session sync#52

Open
JasonOA888 wants to merge 1 commit intoHKUDS:mainfrom
JasonOA888:fix/race-condition-session-save
Open

fix(session): add file locking to prevent race conditions in multi-session sync#52
JasonOA888 wants to merge 1 commit intoHKUDS:mainfrom
JasonOA888:fix/race-condition-session-save

Conversation

@JasonOA888
Copy link

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:

  • Exclusive lock during write
  • Atomic updates prevent lost data
  • Fallback for systems without flock support

Changes

  • anygen/agent-harness/cli_anything/anygen/core/session.py

Testing

  • Concurrent CLI commands no longer drop state
  • Works on Linux/macOS (fcntl available)
  • Graceful fallback on unsupported filesystems

Impact

Multi-session workflows now have consistent state sync.

…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.
@yuh-yang
Copy link
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>
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>
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.

Race conditions in multi-session project state syncing

2 participants