Skip to content

fix(db): make the compaction lock token unique independent of the clock - #1221

Closed
Dumbris wants to merge 1 commit into
mainfrom
claude/recursing-nash-945605
Closed

Dumbris wants to merge 1 commit into
mainfrom
claude/recursing-nash-945605

Conversation

@Dumbris

@Dumbris Dumbris commented Sep 6, 2026

Copy link
Copy Markdown
Member

Problem

TestCompactLockReleaseDoesNotRemoveSomeoneElsesLock fails intermittently on the windows-latest "Build Binaries" job. It is not branch-specific — it failed on #1215 (a docs-only change) and on #1216, then passed on the next run of the same branch.

It is also a real correctness bug, not just a flaky test.

acquireCompactLock stamps the lock file with:

token := fmt.Sprintf("pid=%d\nstarted=%s\n", os.Getpid(), time.Now().UTC().Format(time.RFC3339Nano))

and release compares string(current) != token to decide whether the lock is still its own. On Windows the system clock granularity is coarse (~0.5-15ms), so two acquisitions in the same process with no work between them can read the same time.Now(). Same pid + same timestamp = identical token, so A's release believes B's lock is its own and deletes it — freeing the pathname for a third compaction to run alongside B on the same BBolt database. Whether the two calls straddle a clock tick is what made it intermittent; the observed failures run in 0.00s.

Fix

Add 16 crypto/rand bytes, hex-encoded, to the token. pid and started stay for whoever has to diagnose a stale lock file. The lock protocol and the compaction flow are unchanged.

Testing

compactLockNow is a seam so tests can freeze the clock — which is the Windows condition, made deterministic.

  • The existing TestCompactLockReleaseDoesNotRemoveSomeoneElsesLock now freezes the clock, so it exercises the collision on every platform instead of by luck. With the nonce reverted it reproduces the byte-identical windows-latest failure on macOS: A's release removed B's lock, so a third compaction could start: stat …config.db.compact.lock: no such file or directory.
  • New TestCompactLockTokenIsUniqueWithoutClockAdvance asserts the token property directly (acquire → read file → release → re-acquire → read file → compare).

Both fail without the nonce and pass with it. Verified: go test -count=5 ./cmd/mcpproxy/ (green), go test -race (green), GOOS=windows go build ./... and GOOS=windows go test -c (both build), golangci-lint run --config .github/.golangci.yml ./cmd/mcpproxy/... → 0 issues.

Windows CI on this PR is the remaining confirmation.

🤖 Generated with Claude Code

acquireCompactLock stamped its lock file with pid+timestamp, and release
compared that string to decide whether the lock was still its own. On Windows
the system clock granularity is coarse (~0.5-15ms), so two acquisitions in the
same process with no work between them can read the same time.Now(). Same pid +
same timestamp = identical token, and A's release then deletes B's lock —
leaving the pathname free for a third compaction to start alongside B's.

The token is the only thing standing between a stale-lock cleanup and two
concurrent compactions on the same BBolt database, so this is a correctness
bug, not just the intermittent windows-latest failure of
TestCompactLockReleaseDoesNotRemoveSomeoneElsesLock (introduced in #1214, seen
on unrelated PRs #1215 and #1216).

Add 16 crypto/rand bytes, hex-encoded, to the token. pid and started stay for
whoever has to read a stale lock file. The lock protocol and the compaction
flow are unchanged.

Testing: compactLockNow is a seam so tests can freeze the clock, which is
exactly the Windows condition. Frozen, the existing test reproduces the
byte-identical windows-latest failure on macOS instead of by luck, and a new
TestCompactLockTokenIsUniqueWithoutClockAdvance asserts the token property
directly. Both fail without the nonce and pass with it, on every platform.

Co-Authored-By: Claude Opus 5 <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 50.00000% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/mcpproxy/db_cmd.go 50.00% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: claude/recursing-nash-945605

Available Artifacts

  • archive-darwin-amd64 (29 MB)
  • archive-darwin-arm64 (26 MB)
  • archive-linux-amd64 (17 MB)
  • archive-linux-arm64 (16 MB)
  • archive-windows-amd64 (29 MB)
  • archive-windows-arm64 (26 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (23 MB)
  • installer-dmg-darwin-arm64 (21 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 34050048343 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris

Dumbris commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

Duplicate of #1217, which has the same crypto/rand nonce fix and got there first. #1217 also handles the failure path correctly (it closes the file handle before os.Remove, which matters because Windows refuses to delete an open file).

The one thing this PR had that #1217 lacked — the compactLockNow seam that freezes the clock so the pre-existing TestCompactLockReleaseDoesNotRemoveSomeoneElsesLock reproduces the Windows collision on every platform — is ported over in 9feb4ec. Closing in favour of #1217.

@Dumbris Dumbris closed this Sep 7, 2026
@Dumbris
Dumbris deleted the claude/recursing-nash-945605 branch September 7, 2026 07:27
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.

2 participants