Skip to content

Tile retry: an early timer waits for the cache's retry time (EXP-296 flake) - #426

Merged
prete merged 1 commit into
mainfrom
claude/tile-retry-early-timer
Nov 24, 2026
Merged

prete merged 1 commit into
mainfrom
claude/tile-retry-early-timer

Conversation

@prete

@prete prete commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Fixes the flaky Vitest test src/components/MorphologyCanvas.test.tsx > "reads a failed tile again and draws it, with the view still (EXP-296)". It failed in CI on #409, #411 and #412 with expected "vi.fn()" to be called 4 times, but got 5 times, at line 322 (the last drawImage count).

Cause: the scheduler's retry timer, not the test

The tile cache decides a failed tile may be read again with Date.now() >= retryAt. The scheduler (src/tiff/tileRequestScheduler.ts) sets a timer for retryAt - Date.now() ms. Node timers run on libuv's monotonic millisecond clock, and Date.now() on the wall clock. Because the two round to whole milliseconds at different points, a timer can fire up to a millisecond before Date.now() reaches the time.

When that happened:

  1. The timer asked the cache for the tile, and the cache handed back the old failure without reading it.
  2. The scheduler treated that as a settled read. It set the timer again (≤ 1 ms) and called onSettled.
  3. onSettled redrew the canvas once more with only the ready tile: 2 → 3 draws.
  4. The real retry followed, and both tiles were drawn: 5 draws instead of 4.

So this is an app bug, not only a test problem: one extra redraw and an extra cache call per early timer. No tile was ever fetched twice.

Fix

When the retry timer fires and Date.now() is still short of the cache's retry time, the scheduler sets the timer again without asking the cache or settling the tile. The MorphologyCanvas test is unchanged and still checks exact counts: the failed tile is asked for again under the same key and drawn, while the view stays still.

Evidence (macOS, Node 25)

  • Reproduced before the fix: the test with { repeats: 400 }, run 3 times, failed 4 of about 1,200 runs. A log in the mock cache showed that each of those 4 runs had a timer fire before Date.now() reached retryAt, and no other run drew 5 times.
  • Amplified: with setTimeout patched to fire 2 ms early, 86 of 101 runs drew too many times (5 or 6) before the fix, and 0 of 101 after.
  • After the fix: 2,000 unpatched runs, with the full suite running in parallel as CPU load, all passed.
  • New scheduler test: "waits again when its timer fires before the retry time" uses fake setTimeout with a separately controlled Date.now(). It fails without the fix (onSettled called twice) and passes with it. The test cache double now hands back a failure unread before its retry time, as TileCache does.

Checks

  • npm run format:check
  • npm run lint
  • npx tsc -b
  • npx vitest run: 396 files, 3,836 tests passed. Also passed run in parallel with the repeated EXP-296 runs, and with --sequence.shuffle on the MorphologyCanvas, scheduler and tile cache test files.

Node timers can fire a millisecond before Date.now() reaches the time they
were set for. The scheduler's retry timer then asked the cache, which handed
back the failure unread, and the tile settled again: one more canvas redraw,
which made the EXP-296 MorphologyCanvas test draw 5 times instead of 4 on CI.
@prete
prete merged commit f806991 into main Nov 24, 2026
4 checks passed
prete added a commit that referenced this pull request Nov 24, 2026
@prete
prete deleted the claude/tile-retry-early-timer branch November 24, 2026 21:39
prete added a commit that referenced this pull request Nov 24, 2026
The canvas's tile caches already read a failed tile again after 1, 2
and 4 s (TILE_RETRY_DELAYS_MS; the early-timer fix in #426), then keep
the error until the tile is evicted, with no way to forget one tile. So
the screenshot gets its own TileCache and scheduler per channel: a
Retry reads failed tiles again, and a read of hundreds of tiles does not
push the canvas's tiles out of its cache. D4, the drawing steps, task C
and the risks say so; the code map is rechecked at fe50d70.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant