Backtestsvuerun-20261021-133314#15083
EffectScope stop() causes runtime crash (Cannot read properties of undefined) when dynamic scope cleanup mutates parent.scopes array during traversal
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches apply the same root-cause fix: snapshot this.scopes (and this.effects in resume) before iterating in pause()/resume()/stop(), so a child's cleanup or a resumed sync watcher that stops a sibling cannot shift indices under the cached-length loop. All four independent tests pass on both, so correctness is equivalent for the reported scenarios. The only source difference is that Patch A also copies this.effects in pause(); effect.pause() runs no user callbacks, so this is unneeded but harmless, and A's comments explain why each copy exists, which is a small craft plus. The real separation is in tests. Patch A adds three tests: parent stop where a child disposes a sibling (asserting parent and all children inactive and every dispose spy called exactly once), parent resume where a child's sync watcher stops a sibling scope while asserting the other siblings resume and still fire, and resume where a watcher stops a sibling watcher. Patch B adds two: the stop case (only asserting no throw and the sibling inactive) and the sibling-watcher resume case; it does not exercise the resume() path over this.scopes, which the issue explicitly names as affected and which the independent harness tested. A's tests cover more of the issue's stated surface with stronger assertions. Neither patch has unrelated churn. A wins narrowly on test coverage and clarity.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
5m 44s
Wall-clock time in the container
Full test suite
No new failures
2,166 passed · 5 skipped · 2,173 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
93vs 56 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.28
Link to minimal reproduction
Steps to reproduce
Open the reproduction link.
Click the "Trigger сrash" button.
Observe the output status on the screen or check the browser console.
What is expected?
The parent scope should stop all its children successfully. When a child scope stops a sibling scope during the cleanup phase, the parent traversal loop should handle the array mutation gracefully without throwing errors.
What is actually happening?
The application crashes with the following error: TypeError: Cannot read properties of undefined (reading 'stop')
This happens because EffectScope.stop loops over this.scopes using a cached length l. During iteration, one child's onScopeDispose calls stop() on its sibling. The sibling removes itself from the parent's scopes array, reducing the array size. When the parent loop continues to the cached index l, it tries to access an element out of bounds (which is undefined), causing the runtime crash.
System Info
System: OS: macOS, Browser: Chrome, Node: v20.19.5
Any additional comments?
The bug lies in packages/reactivity/src/effectScope.ts.
Inside EffectScope, the methods pause(), resume(), and stop() loop over this.scopes using a cached length without accounting for array mutations. Creating a shallow copy of the array before iterating (e.g. const scopes = this.scopes.slice()) safely resolves the issue.
The two patches
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..17d69428e 100644@@ -435,4 +435,67 @@ describe('reactivity/effect/scope', () => { expect(rs.value.stage).toBe(1) expect(renderCount).toBe(5) })++ test('stopping a sibling scope during parent stop should not crash', () => {+ const parent = effectScope()+ const spies = [vi.fn(), vi.fn(), vi.fn()]+ let scopes: EffectScope[] = []+ parent.run(() => {+ scopes = spies.map(spy =>+ effectScope().run(() => {+ onScopeDispose(spy)+ return getCurrentScope()!+ })!,+ )+ // first child stops its sibling while being stopped by the parent+ scopes[0].run(() => onScopeDispose(() => scopes[1].stop()))+ })++ expect(() => parent.stop()).not.toThrow()+ expect(parent.active).toBe(false)+ scopes.forEach(s => expect(s.active).toBe(false))+ spies.forEach(spy => expect(spy).toHaveBeenCalledTimes(1))+ })++ test('stopping a sibling scope during parent resume should not crash', () => {+ const parent = effectScope()+ const counter = ref(0)+ const cb = vi.fn()+ let scopes: EffectScope[] = []+ parent.run(() => {+ scopes = [0, 1, 2].map(() => effectScope())+ scopes[0].run(() => {+ watch(counter, () => scopes[1].stop(), { flush: 'sync' })+ })+ scopes[2].run(() => {+ watch(counter, cb, { flush: 'sync' })+ })+ })++ parent.pause()+ counter.value+++ expect(() => parent.resume()).not.toThrow()+ expect(scopes[1].active).toBe(false)+ expect(scopes[0].active).toBe(true)+ expect(scopes[2].active).toBe(true)+ expect(cb).toHaveBeenCalledTimes(1)+ })++ test('stopping a sibling watcher during scope resume should not crash', () => {+ const scope = effectScope()+ const counter = ref(0)+ const cb = vi.fn()+ scope.run(() => {+ let stopB: () => void = () => {}+ watch(counter, () => stopB(), { flush: 'sync' })+ stopB = watch(counter, () => {}, { flush: 'sync' })+ watch(counter, cb, { flush: 'sync' })+ })++ scope.pause()+ counter.value+++ expect(() => scope.resume()).not.toThrow()+ expect(scope.effects.length).toBe(2)+ expect(cb).toHaveBeenCalledTimes(1)+ }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..362b2a73a 100644@@ -70,12 +70,15 @@ export class EffectScope { this._isPaused = true let i, l if (this.scopes) {- for (i = 0, l = this.scopes.length; i < l; i++) {- this.scopes[i].pause()+ // copy before iterating, consistent with resume()/stop()+ const scopes = this.scopes.slice()+ for (i = 0, l = scopes.length; i < l; i++) {+ scopes[i].pause() } }- for (i = 0, l = this.effects.length; i < l; i++) {- this.effects[i].pause()+ const effects = this.effects.slice()+ for (i = 0, l = effects.length; i < l; i++) {+ effects[i].pause() } } }@@ -89,12 +92,16 @@ export class EffectScope { this._isPaused = false let i, l if (this.scopes) {- for (i = 0, l = this.scopes.length; i < l; i++) {- this.scopes[i].resume()+ // copy before iterating: resuming may trigger effects whose+ // callbacks stop (and thus remove) sibling scopes/effects+ const scopes = this.scopes.slice()+ for (i = 0, l = scopes.length; i < l; i++) {+ scopes[i].resume() } }- for (i = 0, l = this.effects.length; i < l; i++) {- this.effects[i].resume()+ const effects = this.effects.slice()+ for (i = 0, l = effects.length; i < l; i++) {+ effects[i].resume() } } }@@ -171,8 +178,11 @@ export class EffectScope { this.cleanups.length = 0 if (this.scopes) {- for (i = 0, l = this.scopes.length; i < l; i++) {- this.scopes[i].stop(true)+ // copy before iterating: a child's cleanup may stop a sibling scope,+ // which removes it from this.scopes+ const scopes = this.scopes.slice()+ for (i = 0, l = scopes.length; i < l; i++) {+ scopes[i].stop(true) } this.scopes.length = 0 }
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..0bb752659 100644@@ -435,4 +435,40 @@ describe('reactivity/effect/scope', () => { expect(rs.value.stage).toBe(1) expect(renderCount).toBe(5) })++ it('should stop child scopes when cleanup stops a sibling scope', () => {+ const parent = effectScope()+ let sibling!: EffectScope++ parent.run(() => {+ effectScope().run(() => {+ onScopeDispose(() => sibling.stop())+ })+ sibling = effectScope()+ })++ expect(() => parent.stop()).not.toThrow()+ expect(sibling.active).toBe(false)+ })++ it('should resume effects when a watcher stops a sibling watcher', () => {+ const count = ref(0)+ const scope = effectScope()+ let stopSecond = () => {}+ const firstSpy = vi.fn(() => stopSecond())+ const secondSpy = vi.fn()++ scope.run(() => {+ watch(count, firstSpy, { flush: 'sync' })+ stopSecond = watch(count, secondSpy, { flush: 'sync' })+ })++ scope.pause()+ count.value++++ expect(() => scope.resume()).not.toThrow()+ expect(firstSpy).toHaveBeenCalledTimes(1)+ expect(secondSpy).not.toHaveBeenCalled()+ expect(scope.effects).toHaveLength(1)+ }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..6fc587de1 100644@@ -70,8 +70,9 @@ export class EffectScope { this._isPaused = true let i, l if (this.scopes) {- for (i = 0, l = this.scopes.length; i < l; i++) {- this.scopes[i].pause()+ const scopes = this.scopes.slice()+ for (i = 0, l = scopes.length; i < l; i++) {+ scopes[i].pause() } } for (i = 0, l = this.effects.length; i < l; i++) {@@ -89,12 +90,14 @@ export class EffectScope { this._isPaused = false let i, l if (this.scopes) {- for (i = 0, l = this.scopes.length; i < l; i++) {- this.scopes[i].resume()+ const scopes = this.scopes.slice()+ for (i = 0, l = scopes.length; i < l; i++) {+ scopes[i].resume() } }- for (i = 0, l = this.effects.length; i < l; i++) {- this.effects[i].resume()+ const effects = this.effects.slice()+ for (i = 0, l = effects.length; i < l; i++) {+ effects[i].resume() } } }@@ -171,8 +174,9 @@ export class EffectScope { this.cleanups.length = 0 if (this.scopes) {- for (i = 0, l = this.scopes.length; i < l; i++) {- this.scopes[i].stop(true)+ const scopes = this.scopes.slice()+ for (i = 0, l = scopes.length; i < l; i++) {+ scopes[i].stop(true) } this.scopes.length = 0 }