Skip to content

Commit 2bbb6d2

Browse files
committed
fix(scheduler): recover nextTick from error in post flush cb
1 parent 2b05c1e commit 2bbb6d2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,4 +844,12 @@ describe('scheduler', () => {
844844
await nextTick()
845845
expect(calls).toEqual(['cb2', 'cb1'])
846846
})
847+
848+
test('error in postFlush cb should not cause nextTick to stuck in rejected state forever', async () => {
849+
queuePostFlushCb(() => {
850+
throw 'err'
851+
})
852+
await expect(nextTick).rejects.toThrow('err')
853+
await expect(nextTick()).resolves.toBeUndefined()
854+
})
847855
})

packages/runtime-core/src/scheduler.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ export function queueJob(job: SchedulerJob): void {
119119

120120
function queueFlush() {
121121
if (!currentFlushPromise) {
122-
currentFlushPromise = resolvedPromise.then(flushJobs)
122+
currentFlushPromise = resolvedPromise.then(flushJobs).catch(e => {
123+
currentFlushPromise = null
124+
throw e
125+
})
123126
}
124127
}
125128

@@ -201,8 +204,13 @@ export function flushPostFlushCbs(seen?: CountMap): void {
201204
if (cb.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
202205
cb.flags! &= ~SchedulerJobFlags.QUEUED
203206
}
204-
if (!(cb.flags! & SchedulerJobFlags.DISPOSED)) cb()
205-
cb.flags! &= ~SchedulerJobFlags.QUEUED
207+
if (!(cb.flags! & SchedulerJobFlags.DISPOSED)) {
208+
try {
209+
cb()
210+
} finally {
211+
cb.flags! &= ~SchedulerJobFlags.QUEUED
212+
}
213+
}
206214
}
207215
activePostFlushCbs = null
208216
postFlushIndex = 0

0 commit comments

Comments
 (0)