Skip to content

fix(storage): refcount the process lock for same-process stores - #58

Merged
Patel230 merged 1 commit into
mainfrom
fix/storage-lock-intraprocess
Aug 15, 2026
Merged

fix(storage): refcount the process lock for same-process stores#58
Patel230 merged 1 commit into
mainfrom
fix/storage-lock-intraprocess

Conversation

@Patel230

Copy link
Copy Markdown
Contributor

The process lock merged in #47 used flock(2)/LockFileEx directly in NewStore/Close. These OS locks are per-process, so a second NewStore on the same DB within one process was rejected (verified: probe returned database locked by another yaad process: resource temporarily unavailable).

Hosts that construct the store from multiple callsites (hawk creates a YaadBridge per bridge) would then silently fail and lose memory access.

Fix:

  • Keep the OS lock exclusive across processes.
  • Within a process, share it per lock path via a refcount registry: AcquireProcessLock bumps, Release unrefs; the file is unlocked + removed only on the last release.
  • Release is idempotent; sync.Once guards double release; in-memory no-op locks unchanged.

Tests: same-process sharing (4 concurrent stores), refcount release ordering, idempotency, and a cross-process sentinel test proving another process is still blocked. Updated TestProcessLock to the refcount semantics. Full go test ./... green, race-clean, gofumpt/lint clean, both platforms build.

flock(2)/LockFileEx is per-process, so the merged PR #47 lock rejected a
second NewStore on the same DB within one process. Hosts that construct
the store from multiple callsites (hawk opens a YaadBridge per callsite)
would then silently lose memory access.

- Keep the OS lock exclusive across processes (flock / LockFileEx).
- Within a process, share it per lock path via a refcount registry:
  Acquire bumps, Release unrefs; the file is unlocked and removed only
  on the last release.
- Release is idempotent; in-memory no-op locks are unchanged.

- Tests: same-process sharing, refcount release ordering, and a
  cross-process sentinel test asserting another process is still blocked.
@Patel230
Patel230 merged commit fbc0466 into main Aug 15, 2026
12 checks passed
@Patel230
Patel230 deleted the fix/storage-lock-intraprocess branch August 15, 2026 13:24
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.

1 participant