Skip to content

Bug: Concurrent JSON Writes Can Corrupt command_history.json and Workspace State Files #63

@charu2210

Description

@charu2210

Description

The application performs direct JSON writes to state/history files without atomic write protection or file locking.

Affected areas include:

  • save_command_history()
  • save_workspace_state()
  • workspace profile saves

Current implementation writes directly using:

with open(path, 'w', encoding='utf-8') as f:
    json.dump(data, f)

Because the application uses threaded execution flows and supports multiple simultaneous actions, concurrent writes can corrupt JSON files.

Possible Impact

  • command_history.json becomes malformed/truncated
  • workspace state resets unexpectedly
  • profile JSON files become unreadable
  • silent data loss after concurrent actions
  • inconsistent UI state after reload

Steps to Reproduce

  1. Open multiple terminals/tabs
  2. Execute commands rapidly in parallel
  3. Simultaneously clear command history or save workspace state
  4. Interrupt execution during write operations
  5. Restart/reload application

Observed Behavior

Under concurrent write conditions, JSON state files may become partially written, truncated, or malformed. On reload, the application may fail to restore history/workspace state correctly and can fall back to empty defaults.

Suggested Fix

  • Use atomic file writes via temporary files + os.replace()
  • Add threading/file locks around write operations
  • Validate JSON integrity before replacing original files
  • Add recovery handling for partially written state files

Expected Outcome

State/history files remain consistent and recoverable even under concurrent operations or interrupted writes.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions