Skip to content

fix(core): add flock to serialize session file read/write#325

Open
chenhg5 wants to merge 1 commit intomainfrom
fix/session-state-file-locking
Open

fix(core): add flock to serialize session file read/write#325
chenhg5 wants to merge 1 commit intomainfrom
fix/session-state-file-locking

Conversation

@chenhg5
Copy link
Copy Markdown
Owner

@chenhg5 chenhg5 commented Mar 25, 2026

Summary

  • Add exclusive advisory file locking via flock(2) to SessionManager.load() and SessionManager.saveLocked()
  • Lock file: {storePath}.lock - acquired exclusively before read/write
  • This prevents race conditions when multiple projects or goroutines concurrently access the same session state file

Fixes #324.

Test plan

  • go build ./... passes
  • go vet ./... passes
  • go test ./... passes

Closes #324

Adds exclusive advisory file locking to SessionManager.load() and
SessionManager.saveLocked() using flock(2). This prevents race conditions
when multiple projects or goroutines concurrently access the same session
state file, ensuring counter/sessions/active_session are always consistent.

Fixes #324.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chenhg5
Copy link
Copy Markdown
Owner Author

chenhg5 commented Mar 28, 2026

Note: CI lint check failed with the following errors:

core/session.go:450:13: Error return value of `unix.Flock` is not checked (errcheck)
core/session.go:451:19: Error return value of `unix.Flock` is not checked (errcheck)
core/session.go:498:13: Error return value of `unix.Flock` is not checked (errcheck)

Please consider handling the error return values from unix.Flock calls before merging.

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.

New session creation fails to persist session state when running multiple projects concurrently

2 participants