Skip to content

Commit c04a45d

Browse files
fix(scheduler): avoid stack overflow when flushing very large post cb arrays (#15143)
close #15142
1 parent 71f9ff5 commit c04a45d

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎packages/runtime-core/__tests__/scheduler.spec.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,44 @@ describe('scheduler', () => {
303303
expect(calls).toEqual(['cb1', 'cb2', 'cb3'])
304304
})
305305

306+
it('should not overflow the call stack with a very large cb array', async () => {
307+
// a suspense boundary can buffer an unbounded number of post effects
308+
// while pending (e.g. a large tree re-rendering under it) and flush them
309+
// all at once on resolve - spreading such an array into push() throws
310+
// RangeError: Maximum call stack size exceeded
311+
let called = 0
312+
const cbs: SchedulerJob[] = []
313+
for (let i = 0; i < 1000000; i++) {
314+
cbs.push(() => {
315+
called++
316+
})
317+
}
318+
319+
queuePostFlushCb(cbs)
320+
await nextTick()
321+
expect(called).toBe(1000000)
322+
})
323+
324+
it('should not overflow the call stack when merging a very large cb array into an active flush', async () => {
325+
// same as above, but the large array is queued while a post flush is
326+
// already in progress, exercising the merge into activePostFlushCbs
327+
// inside a nested flushPostFlushCbs call
328+
let called = 0
329+
const cbs: SchedulerJob[] = []
330+
for (let i = 0; i < 1000000; i++) {
331+
cbs.push(() => {
332+
called++
333+
})
334+
}
335+
336+
queuePostFlushCb(() => {
337+
queuePostFlushCb(cbs)
338+
flushPostFlushCbs()
339+
})
340+
await nextTick()
341+
expect(called).toBe(1000000)
342+
})
343+
306344
it('should dedupe queued postFlushCb', async () => {
307345
const calls: string[] = []
308346
const cb1 = () => {

‎packages/runtime-core/src/components/Suspense.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,9 @@ function createSuspenseBoundary(
617617
if (parent.pendingBranch) {
618618
// found a pending parent suspense, merge buffered post jobs
619619
// into that parent
620-
parent.effects.push(...effects)
620+
for (let i = 0; i < effects.length; i++) {
621+
parent.effects.push(effects[i])
622+
}
621623
hasUnresolvedAncestor = true
622624
break
623625
}

‎packages/runtime-core/src/scheduler.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,13 @@ export function queuePostFlushCb(cb: SchedulerJobs): void {
133133
} else {
134134
// if cb is an array, it is a component lifecycle hook which can only be
135135
// triggered by a job, which is already deduped in the main queue, so
136-
// we can skip duplicate check here to improve perf
137-
pendingPostFlushCbs.push(...cb)
136+
// we can skip duplicate check here to improve perf.
137+
// use a loop instead of `push(...cb)`: the array can be arbitrarily large
138+
// (e.g. effects buffered by a suspense boundary) and spreading it would
139+
// overflow the call stack (#15142)
140+
for (let i = 0; i < cb.length; i++) {
141+
pendingPostFlushCbs.push(cb[i])
142+
}
138143
}
139144
queueFlush()
140145
}
@@ -179,7 +184,9 @@ export function flushPostFlushCbs(seen?: CountMap): void {
179184

180185
// #1947 already has active queue, nested flushPostFlushCbs call
181186
if (activePostFlushCbs) {
182-
activePostFlushCbs.push(...deduped)
187+
for (let i = 0; i < deduped.length; i++) {
188+
activePostFlushCbs.push(deduped[i])
189+
}
183190
return
184191
}
185192

0 commit comments

Comments
 (0)