Backtestsvuerun-20261001-022054#15142
Suspense: flushing buffered effects overflows the call stack when many effects accumulate while pending (RangeError: Maximum call stack size exceeded)
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches replace the spread pushes the issue names with plain loops: `pendingPostFlushCbs.push(...cb)` in `queuePostFlushCb`, `activePostFlushCbs.push(...deduped)` in nested `flushPostFlushCbs`, and `parent.effects.push(...effects)` in Suspense's merge into a pending parent. So both resolve the reported root cause in the same way. Patch A also fixes a fourth spread site, `suspense.effects.push(...fn)` in `queueEffectWithSuspense`. This is exactly where a component's lifecycle hook array enters the suspense buffer. A large hook array would overflow there before ever reaching the scheduler. The change is low-risk and makes the fix complete across every path that buffers suspense effects, rather than leaving one spread in the same data flow. Tests also favor A. - **B's nested-merge test does not isolate the path it claims to test.** It calls `queuePostFlushCb(cbs)` with a 1M array inside the active flush, so before the fix it would overflow in `queuePostFlushCb` first. It never checks the `activePostFlushCbs` merge on its own. - **A's nested test isolates that path.** It queues the callbacks in 10k chunks so only the merge into `activePostFlushCbs` sees the full-size array. It also catches the error so a failure doesn't corrupt scheduler state for later tests. - **A adds a Suspense integration test that B lacks.** In it, an inner suspense resolves first and merges 1M buffered `onMounted` effects into a still-pending outer suspense. This exercises the `parent.effects` merge and `queueEffectWithSuspense` paths end to end, and checks that the effects are deferred and then all run. B adds no test for the Suspense code it changed. Craft is comparable: B's comments are slightly more explanatory, but this doesn't outweigh A's more complete fix and better-targeted tests.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
4m 44s11 turns
Wall-clock time in the container
Full test suite
No new failures
3,635 passed · 6 skipped · 3,641 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
122vs 55 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.39 (also reproduces on 3.5.40 / @b5f8518 in the playground)
Link to minimal reproduction
Steps to reproduce
- Open the playground and show the Preview pane.
- The app renders a
<Suspense>whose default slot contains an async component (resolves after ~4s) plus a large tree (2000 rows, each with a fewflush: 'post'watchers). - While the suspense is pending, a timer keeps mutating a ref, so the still-mounted tree re-renders repeatedly. Each re-render buffers its post effects into
suspense.effects(viaqueueEffectWithSuspense). - When the async component resolves, the suspense flushes the buffered effects.
What is expected?
The buffered effects flush and the app keeps working.
What is actually happening?
Uncaught (in promise): RangeError: Maximum call stack size exceeded — thrown from queuePostFlushCb called by suspense.resolve():
RangeError: Maximum call stack size exceeded
at queuePostFlushCb (runtime-core.esm-bundler.js:331)
at Object.resolve (runtime-core.esm-bundler.js:7426)
The scheduler dies mid-flush, leaving the app permanently wedged (blank page).
Root cause
suspense.effects is unbounded — every post effect of every component under a pending suspense boundary is buffered into it. On resolve, the whole array is flushed with a spread call:
scheduler.ts:pendingPostFlushCbs.push(...cb)scheduler.ts(flushPostFlushCbsnested case):activePostFlushCbs.push(...deduped)Suspense.ts(merge into pending parent):parent.effects.push(...effects)
Spreading an array into a function call is limited by the engine's call stack (~100k–200k args in V8), so once enough effects accumulate the flush itself throws.
Real-world impact
We hit this in production in a Nuxt app: a kanban board page with ~1000 mounted cards that re-renders once per second (elapsed-time ticker). Navigating to another page keeps the board mounted under <NuxtPage>'s pending Suspense; its buffered effects exceed the spread limit and every SPA navigation away from the board crashes with a permanent white screen (hard reload of the target URL works fine, since there is no pending suspense in that case).
Suggested fix
Replace the unbounded spread pushes with plain loops. I have a PR ready with the fix and a regression test (queuePostFlushCb with a 1M-entry array).
The two patches
packages/runtime-core/__tests__/components/Suspense.spec.tsindex d40f85b86..b319271b0 100644@@ -747,6 +747,72 @@ describe('Suspense', () => { expect(calls).toEqual([`inner mounted`, `outer mounted`]) }) + test('nested suspense (child resolves first) with a large number of buffered effects', async () => {+ const N = 1_000_000+ let count = 0++ const AsyncOuter = defineAsyncComponent(+ {+ setup: () => () => h('div', 'async outer'),+ },+ 10,+ )++ const AsyncInner = defineAsyncComponent(+ {+ setup: () => () => h('div', 'async inner'),+ },+ 1,+ )++ const Child = {+ setup() {+ for (let i = 0; i < N; i++) {+ onMounted(() => count++)+ }+ return () => h('div', 'child')+ },+ }++ const Inner = {+ setup() {+ return () =>+ h(Suspense, null, {+ default: h('div', [h(AsyncInner), h(Child)]),+ fallback: h('div', 'fallback inner'),+ })+ },+ }++ const Comp = {+ setup() {+ return () =>+ h(Suspense, null, {+ default: h('div', [h(AsyncOuter), h(Inner)]),+ fallback: h('div', 'fallback outer'),+ })+ },+ }++ const root = nodeOps.createElement('div')+ render(h(Comp), root)+ expect(serializeInner(root)).toBe(`<div>fallback outer</div>`)++ // inner resolves first and merges its buffered effects into the+ // still-pending outer suspense+ await deps[1]+ await nextTick()+ expect(serializeInner(root)).toBe(`<div>fallback outer</div>`)+ expect(count).toBe(0)++ await Promise.all(deps)+ await nextTick()+ expect(serializeInner(root)).toBe(+ `<div><div>async outer</div><div><div>async inner</div><div>child</div></div></div>`,+ )+ expect(count).toBe(N)+ })+ test('error handling', async () => { const Async = { async setup() {packages/runtime-core/__tests__/scheduler.spec.tsindex 3e454aec0..6f3f33a2f 100644@@ -341,6 +341,44 @@ describe('scheduler', () => { await nextTick() expect(calls).toEqual(['cb1', 'cb2']) })++ it('should not overflow the call stack when queueing a large array', async () => {+ const N = 1_000_000+ let count = 0+ const cbs = Array.from({ length: N }, () => () => count++)++ expect(() => queuePostFlushCb(cbs)).not.toThrow()+ await nextTick()+ expect(count).toBe(N)+ })++ it('should not overflow the call stack when nested flushPostFlushCbs merges a large queue', async () => {+ const N = 1_000_000+ const CHUNK = 10_000+ let count = 0+ let error: unknown+ const cbs = Array.from({ length: N }, () => () => count++)++ queuePostFlushCb(() => {+ // queue in small chunks so only the nested merge into+ // activePostFlushCbs deals with the full-size array+ for (let i = 0; i < N; i += CHUNK) {+ queuePostFlushCb(cbs.slice(i, i + CHUNK))+ }+ // catch so a failure here doesn't leave the outer flush in a+ // broken state and leak into subsequent tests+ try {+ flushPostFlushCbs()+ } catch (e) {+ error = e+ }+ })+ flushPostFlushCbs()++ await nextTick()+ expect(error).toBeUndefined()+ expect(count).toBe(N)+ }) }) describe('queueJob w/ queuePostFlushCb', () => {packages/runtime-core/src/components/Suspense.tsindex ba5e194ae..94db0d9e6 100644@@ -617,7 +617,10 @@ function createSuspenseBoundary( if (parent.pendingBranch) { // found a pending parent suspense, merge buffered post jobs // into that parent- parent.effects.push(...effects)+ // avoid spread to prevent call stack overflow on large arrays+ for (let i = 0; i < effects.length; i++) {+ parent.effects.push(effects[i])+ } hasUnresolvedAncestor = true break }@@ -906,7 +909,9 @@ export function queueEffectWithSuspense( ): void { if (suspense && suspense.pendingBranch) { if (isArray(fn)) {- suspense.effects.push(...fn)+ for (let i = 0; i < fn.length; i++) {+ suspense.effects.push(fn[i])+ } } else { suspense.effects.push(fn) }packages/runtime-core/src/scheduler.tsindex af6752078..d4a4e354b 100644@@ -134,7 +134,10 @@ export function queuePostFlushCb(cb: SchedulerJobs): void { // if cb is an array, it is a component lifecycle hook which can only be // triggered by a job, which is already deduped in the main queue, so // we can skip duplicate check here to improve perf- pendingPostFlushCbs.push(...cb)+ // avoid spread to prevent call stack overflow on large arrays+ for (let i = 0; i < cb.length; i++) {+ pendingPostFlushCbs.push(cb[i])+ } } queueFlush() }@@ -179,7 +182,9 @@ export function flushPostFlushCbs(seen?: CountMap): void { // #1947 already has active queue, nested flushPostFlushCbs call if (activePostFlushCbs) {- activePostFlushCbs.push(...deduped)+ for (let i = 0; i < deduped.length; i++) {+ activePostFlushCbs.push(deduped[i])+ } return }
packages/runtime-core/__tests__/scheduler.spec.tsindex 3e454aec0..000a0d578 100644@@ -303,6 +303,44 @@ describe('scheduler', () => { expect(calls).toEqual(['cb1', 'cb2', 'cb3']) }) + it('should not overflow the call stack with a very large cb array', async () => {+ // a suspense boundary can buffer an unbounded number of post effects+ // while pending (e.g. a large tree re-rendering under it) and flush them+ // all at once on resolve - spreading such an array into push() throws+ // RangeError: Maximum call stack size exceeded+ let called = 0+ const cbs: SchedulerJob[] = []+ for (let i = 0; i < 1000000; i++) {+ cbs.push(() => {+ called+++ })+ }++ queuePostFlushCb(cbs)+ await nextTick()+ expect(called).toBe(1000000)+ })++ it('should not overflow the call stack when merging a very large cb array into an active flush', async () => {+ // same as above, but the large array is queued while a post flush is+ // already in progress, exercising the merge into activePostFlushCbs+ // inside a nested flushPostFlushCbs call+ let called = 0+ const cbs: SchedulerJob[] = []+ for (let i = 0; i < 1000000; i++) {+ cbs.push(() => {+ called+++ })+ }++ queuePostFlushCb(() => {+ queuePostFlushCb(cbs)+ flushPostFlushCbs()+ })+ await nextTick()+ expect(called).toBe(1000000)+ })+ it('should dedupe queued postFlushCb', async () => { const calls: string[] = [] const cb1 = () => {packages/runtime-core/src/components/Suspense.tsindex ba5e194ae..37e5067ff 100644@@ -617,7 +617,9 @@ function createSuspenseBoundary( if (parent.pendingBranch) { // found a pending parent suspense, merge buffered post jobs // into that parent- parent.effects.push(...effects)+ for (let i = 0; i < effects.length; i++) {+ parent.effects.push(effects[i])+ } hasUnresolvedAncestor = true break }packages/runtime-core/src/scheduler.tsindex af6752078..4355018a0 100644@@ -133,8 +133,13 @@ export function queuePostFlushCb(cb: SchedulerJobs): void { } else { // if cb is an array, it is a component lifecycle hook which can only be // triggered by a job, which is already deduped in the main queue, so- // we can skip duplicate check here to improve perf- pendingPostFlushCbs.push(...cb)+ // we can skip duplicate check here to improve perf.+ // use a loop instead of `push(...cb)`: the array can be arbitrarily large+ // (e.g. effects buffered by a suspense boundary) and spreading it would+ // overflow the call stack (#15142)+ for (let i = 0; i < cb.length; i++) {+ pendingPostFlushCbs.push(cb[i])+ } } queueFlush() }@@ -179,7 +184,9 @@ export function flushPostFlushCbs(seen?: CountMap): void { // #1947 already has active queue, nested flushPostFlushCbs call if (activePostFlushCbs) {- activePostFlushCbs.push(...deduped)+ for (let i = 0; i < deduped.length; i++) {+ activePostFlushCbs.push(deduped[i])+ } return }
What the agent reported
I fixed the crash: a suspense with a very large number of buffered effects now flushes them without RangeError: Maximum call stack size exceeded. The full suite passes with the fix (./bt-test: 182 files passed, 1 skipped; 3635 tests passed, 6 skipped, 0 failed).
What was wrong: while a <Suspense> is pending, its post effects pile up in suspense.effects with no limit. Several places then copied whole arrays using push(...array). Passing a huge array as function arguments is limited by the call stack, so once enough effects build up, the flush throws partway through and the scheduler stops working.
What I changed: I replaced each push(...array) with a plain for loop that pushes the same items in the same order.
packages/runtime-core/src/scheduler.ts:queuePostFlushCb:pendingPostFlushCbs.push(...cb)flushPostFlushCbs, nested case:activePostFlushCbs.push(...deduped)packages/runtime-core/src/components/Suspense.ts:resolve(), merging into a pending parent:parent.effects.push(...effects)queueEffectWithSuspense:suspense.effects.push(...fn). The report didn't list this one, but a component's array of lifecycle hooks goes through it before any of the other three, so the same scenario would still crash there.
Tests added:
scheduler.spec.ts, "should not overflow the call stack when queueing a large array": callsqueuePostFlushCbwith 1,000,000 callbacks and checks they all run. This is the test the report suggested.scheduler.spec.ts, "should not overflow the call stack when nested flushPostFlushCbs merges a large queue": covers only the nested merge.Suspense.spec.ts, "nested suspense (child resolves first) with a large number of buffered effects": this is the reported scenario. An inner suspense holding 1MonMountedhooks resolves while the outer one is still pending. The test checks the final rendered output and that all 1M hooks ran.
Test evidence:
- Without the fix: I set the production changes aside and ran the two spec files directly with vitest. All three new tests failed with
RangeError: Maximum call stack size exceeded, and the other 95 tests passed. - With the fix: the new Suspense test passed on its own, and the full
./bt-testrun above includes all three new tests. ./bt-testwith file arguments refused to run (exit 64; its fixed command can't take extra arguments). That's why the targeted runs used vitest and the gate was the full suite.- A reviewer agent checked the diff and found no blocking issues. It suggested that 1M entries makes the tests slower than needed and 200k–500k would still trigger the bug. I kept 1M to match the report's suggested test.
Requirements:
- Fix the spread in
queuePostFlushCb: done, covered by the first scheduler test. - Fix the spread in the nested
flushPostFlushCbscase: done, covered by the nested scheduler test. - Fix the spread when merging into a pending parent suspense: done, covered by the Suspense test.
- Use plain loops, as the report suggested: done.
- Add a regression test with a 1M-entry
queuePostFlushCbarray: done. - Buffered effects flush and the app keeps working: covered by the Suspense test, which checks the rendered output and that every hook ran.
The changes are uncommitted in the working tree, and no scratch files were left behind.