Skip to content

test(memory): make the heartbeat freshness test deterministic - #5715

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/5680-memory-lock-heartbeat-test
Nov 30, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/5680-memory-lock-heartbeat-test

Conversation

@cliffhall

@cliffhall cliffhall commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closes #5680

Description

Makes the withFileLock test "keeps a lock held longer than staleMs fresh, so it is not broken" deterministic. The test is the only file changed.

Root cause: the test's timing, not the heartbeat. The test held a lock for 400 ms with staleMs: 100, so the heartbeat refreshed it every 50 ms, and a second waiter judged the lock's age against the real clock. If a refresh landed more than 50 ms late (a slow runner's disk or scheduler), the waiter found a live lock older than staleMs and broke it. That is the order CI reported: [long start, waiter, long end]. The heartbeat is sound. With the defaults (staleMs 30 s, a beat every 15 s) the same failure needs a 15 s stall in the holder, which the lock protocol already assumes does not happen ("No process stalls for longer than staleMs inside a guarded section", above guardPathOf). So there is no product change and no changeset.

The fix uses vitest fake timers for Date, setInterval and clearInterval only. The lock's age and the heartbeat now move only when the test advances them. setTimeout stays real, so the waiter's retry loop keeps running on its own. The test:

  1. takes the lock with a long holder that waits on a promise, not a sleep;
  2. starts the waiter and waits until it has judged the lock at least once;
  3. eight times: advances the clock by staleMs / 2, which fires exactly one heartbeat, waits for that beat's utimes to land, then waits for the waiter to judge the lock again;
  4. checks that the clock moved by 4 × staleMs while only the holder had run, then releases the holder and expects [long start, long end, waiter].

Every wait is a sync point on an observed event (a spied fs.open of the lock for reading, or a spied fs.utimes), and none is a timed sleep. A slow runner makes the test take longer but cannot change its result. It has an explicit 30 s timeout for that reason. Each wait gives up after 10 s of real time with an error that names what it waited for, so a broken heartbeat fails the test by name (checked by mutation, below). The test's finally and afterEach then restore real timers and settle both lock operations, so nothing leaks into later tests (Copilot round 1). This follows the house style of #5634, which replaced a fixed sleep with a wait on the event itself.

Server Details

  • Server: memory
  • Changed: src/memory/__tests__/file-lock.test.ts only, one test. No tools, resources, prompts, configuration or source changed.

Motivation and Context

The test failed intermittently in CI (Coverage memory on #5677, a PR that touches only src/git) and once locally. See #5680.

How Has This Been Tested?

This change has no client-observable surface, so the evidence is targeted probes (pr-flow step 5). All probes ran on macOS with Node 24, using npx vitest run __tests__/file-lock.test.ts -t "staleMs fresh" from src/memory:

Probe Old test New test
Load: 36 CPU-bound yes processes on an 18-core machine, plus 6 parallel loops of the file under --coverage, 15 runs each 0 / 90 failed (plain contention does not trigger it on this machine) 0 / 90 failed
Slow I/O injected: every heartbeat fs.utimes delayed by 120 ms 10 / 10 failed, with exactly the CI signature: expected [ 'long start', 'waiter', 'long end' ] to deeply equal [ 'long start', 'long end', 'waiter' ] —
Slow I/O and a stall injected: each utimes delayed by 120 ms, plus a 150 ms synchronous busy-wait that blocks the event loop on every beat — 0 / 10 failed
Mutation: heartbeat disabled (refreshLock call replaced with a no-op in index.ts, then reverted) — Fails with Gave up waiting for heartbeat 1 to refresh the lock. The other tests that fail are exactly the heartbeat-dependent ones that fail under the same mutation on the original file, so nothing leaks
Whole file (57 tests), 30 runs in a row, to check the fake timers do not leak into sibling tests — 0 / 30 failed

The probe copies and the mutation were temporary and are not part of the diff. npm run local:gate exits 0 on the final head.

--sequence.concurrent, which the issue suggests, does not apply to this file: its tests spy on the shared fs module (vi.spyOn(fs, "open") and others), so running them concurrently makes them interfere with each other (35 of the 57 fail under --sequence.concurrent, from the shared spies, not from this test). Load and injected delays were used instead.

Breaking Changes

None.

Types of changes

  • Bug fix (non-breaking change which fixes an issue): a flaky test
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation (not applicable: no protocol surface changed)
  • My changes follow MCP security best practices (not applicable: test-only change)
  • I have updated the server's README accordingly (not applicable: nothing the README describes changed)
  • I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes (not applicable: test-only change; nothing published changes)
  • I have tested this with an LLM client (not applicable: no client-observable change; see the probes above)
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling (not applicable: test-only change)
  • I have documented all environment variables and configuration options (not applicable: none added)

Additional context

Siblings #5624 (guard clearing in index.ts) and #5689 (missing parent directory in withFileLock) touch the lock code at the same time. This PR does not touch index.ts, so it does not overlap with either.

🤖 Generated with Claude Code

… of racing it

"keeps a lock held longer than staleMs fresh" held a lock for 400 ms with
staleMs 100, so the heartbeat refreshed it every 50 ms, and a waiter judged
its age against the real clock. When a refresh landed more than 50 ms late
(a slow runner's disk or scheduler), the waiter saw a live lock older than
staleMs and broke it. Delaying each heartbeat utimes by 120 ms reproduces
the CI order [long start, waiter, long end] every time. The heartbeat itself
is sound: production keeps a 15 s margin (staleMs 30 s, a beat every 15 s).

The test now fakes Date and the heartbeat's interval only. It advances the
clock one beat at a time, waits for that beat's refresh to land and for the
waiter to look at the lock again, and holds the lock for four times staleMs
before releasing it. Timeouts stay real, so the waiter retries on its own.

Closes #5680

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@changeset-bot

changeset-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d871592

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@cliffhall
cliffhall requested a balanced review from Copilot November 30, 2026 08:06
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

memory: MCP interface diff

Base: c0075947ac6b (Merge pull request #5682 from modelcontextprotocol/v2/chore/5681-python-calver-2026.11.27)
Head: the build in this checkout, at 6aad9e60d149 (Merge d871592 into 465d3bd)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 9 0 1 0
Head 9 0 1 0

Updated by this run for d8715927988d122dffda10d395e5f638435bad6c.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The heartbeat test can hang indefinitely and leak fake timers and a blocked lock holder into subsequent tests because synchronization waits are unbounded and cleanup is not timeout-safe.

1 open issue
What changed in this PR

This test-only PR addresses #5680 by replacing scheduler-dependent timing in the memory server’s lock-heartbeat test with controlled clock advancement. Production code and published behavior are unchanged.

Changes:

  • Fakes the clock and heartbeat interval while keeping retry timeouts real.
  • Synchronizes on holder startup, heartbeat writes, and waiter reads instead of fixed sleeps.
  • Verifies the lock remains held across four stale periods, with a 30-second test timeout.
File Description
src/​memory/​__tests__/​file-lock.test.ts Reworks the heartbeat freshness test to use controlled timing and observed filesystem events.

Review effort: Balanced

Comment thread src/memory/__tests__/file-lock.test.ts Resolved
Review of #5715: a heartbeat that stopped refreshing left the test waiting
until Vitest's timeout, which skips the test's finally, so the fake timers
and the blocked lock holder outlived it. Each wait now gives up after 10 s of
real time with an error that names what it waited for. The finally releases
the holder and settles both lock operations, and afterEach restores real
timers for a test that times out anyway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: one finding, fixed in d871592.

  • Bound the heartbeat waits and restore timers on a test timeout (fixed): a defect in the test this PR adds. If the heartbeat stopped, the wait never ended, Vitest's timeout skipped the test's finally, and the fake timers and the blocked holder outlived the test. Each wait now gives up after 10 s of real time with a named error. The finally releases the holder and settles both lock operations, and afterEach restores real timers. A mutation check (heartbeat disabled) fails the test with Gave up waiting for heartbeat 1 to refresh the lock, and nothing leaks into the tests after it.

Re-checked after the fix: the injected-delay probe (each utimes delayed 120 ms plus a 150 ms event-loop stall) passes 10 of 10 runs, and the whole file passes 30 of 30. npm run local:gate exits 0 on d871592. Requesting round 2.

@github-actions

Copy link
Copy Markdown
Contributor

filesystem: MCP interface diff

Base: c0075947ac6b (Merge pull request #5682 from modelcontextprotocol/v2/chore/5681-python-calver-2026.11.27)
Head: the build in this checkout, at 6aad9e60d149 (Merge d871592 into 465d3bd)

Diffed because it was named (--server).

⚠️ Interface changes detected in 1 part(s) of the interface. Review them below to confirm they are intended.

Tools Prompts Resources Resource templates
Base 16 0 0 1
Head 16 0 0 1

tools

--- base/tools.json
+++ target/tools.json

- tools[edit_file].inputSchema.properties.edits.items.properties.oldText.description: "Text to search for - must match exactly"
+ tools[edit_file].inputSchema.properties.edits.items.properties.oldText.description: "Text to search for. It matches the file's text exactly, except that line endings (LF or CRLF) never ..."

Updated by this run for d8715927988d122dffda10d395e5f638435bad6c.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the test-only changes to lock-heartbeat timing and cleanup.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 2 (on d871592): clean. No new findings, and the round-1 finding is marked resolved. The review loop ends here, on the first clean round.

@cliffhall
cliffhall merged commit 3b7c042 into v2/main Nov 30, 2026
54 checks passed
@cliffhall
cliffhall deleted the v2/fix/5680-memory-lock-heartbeat-test branch November 30, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

memory: file-lock test "keeps a lock held longer than staleMs fresh" is flaky on a slow runner

2 participants