Skip to content

Fix batch scheduler stale timer on premature flush - #5500

Closed
zjuncal wants to merge 1 commit into
wevm:mainfrom
zjuncal:main
Closed

zjuncal wants to merge 1 commit into
wevm:mainfrom
zjuncal:main

Conversation

@zjuncal

@zjuncal zjuncal commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

What does this PR solve?

Fixes a bug in createBatchScheduler where a premature batch flush (triggered by shouldSplitBatch) does not cancel the pending setTimeout. The stale timer later fires and reads the global schedulerCache, which by then contains items from a new batch, causing that batch to be flushed prematurely — effectively bypassing the wait parameter.

Root cause

schedule() calls setTimeout(exec, wait) but does not store the timer handle. When shouldSplitBatch triggers exec() → flush(), the scheduler cache is cleared but the timer is not cancelled. The timer's callback (exec) re-reads the global scheduler at fire time, so it operates on whatever batch happens to be current.

Fix

  • Added a timerCache Map (keyed by scheduler id) to track active timer handles.
  • flush() now calls clearTimeout(timer) before deleting the scheduler cache.
  • schedule() stores the setTimeout return value in timerCache.

Test

Added a regression test that creates a time gap between the stale timer (T1) and the new batch's timer (T2) by using wait=30 with an await wait(10) between the second and third schedule() calls. A fourth item is scheduled after T1's deadline but before T2's. Without the fix, item 3 is flushed alone by the stale T1; with the fix, items 3 and 4 are correctly batched together by T2.

Verified: test fails without the source fix (expected [3, [3]] to deeply equal [3, [3, 4]]), passes with it.

Alternatives considered

Considered moving the timer into the SchedulerItem array, but that would change the data structure shape and affect every read path. The timerCache Map mirrors the existing schedulerCache pattern and keeps the change minimal.

@changeset-bot

changeset-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 135b0cb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

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

@vercel

vercel Bot commented Nov 12, 2026

Copy link
Copy Markdown

@zjuncal is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

jxom commented Nov 13, 2026

Copy link
Copy Markdown
Member

Thanks @zjuncal, the bug is real and your fix was right. Superseded by #5504, which keeps your fix, simplifies the timer clear, and rewrites the test to match our test conventions (no vi, inline snapshot). You're credited as co-author on the commit.

@jxom jxom closed this Nov 13, 2026
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