Backtestsvuerun-20261009-161443#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 B = our agent, Patch A = the merged fix.
The source changes are functionally identical. Both patches copy `this.scopes` with `slice()` before iterating in `pause()`, `resume()` and `stop()`, and both also copy `this.effects` in `resume()`. That fixes the root cause the issue describes: the parent loop used a cached length while a child's cleanup removed a sibling from `parent.scopes`. The only source difference is that Patch B adds brief comments explaining why the copies are needed, which helps future readers. On correctness, they tie. The tests are where the patches differ. Patch A adds two tests, and both would fail before the fix: - A child's cleanup stops a sibling scope, which hits the undefined `.stop` crash in `stop()`. - A sync watcher stops a sibling watcher during `resume()`, which hits the undefined `.resume` crash on effects. Patch B covers the same core scenario with stronger assertions: - It checks that both children's dispose callbacks ran exactly once. - It checks that all scopes are inactive and that `parent.scopes` is emptied. Patch B also adds `resume()` scenarios Patch A does not cover: - A resumed effect stops a sibling child scope. This exercises the scopes loop in `resume()` and verifies that a later sibling, `child3`, is still resumed and runs its queued effect. - A resumed effect stops its own scope. - A `once` watcher stops itself during resume. This verifies that the remaining effects are still resumed and stay reactive afterwards. These tests exercise each changed loop more thoroughly. They also assert that no sibling is skipped, not just that nothing throws. Scope is equivalent. Patch B wins on test thoroughness and slightly on craft (explanatory comments).
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
7m 5s
Wall-clock time in the container
Full test suite
No new failures
2,167 passed · 5 skipped · 2,174 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
134vs 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..912b7fe35 100644@@ -342,6 +342,116 @@ describe('reactivity/effect/scope', () => { expect(fnSpy).toHaveBeenCalledTimes(3) }) + it('should stop all children when a child cleanup stops a sibling scope', () => {+ const parent = effectScope()+ let child1!: EffectScope+ let child2!: EffectScope+ const dispose1 = vi.fn()+ const dispose2 = vi.fn()+ parent.run(() => {+ child1 = effectScope()+ child1.run(() => {+ onScopeDispose(() => {+ dispose1()+ child2.stop()+ })+ })+ child2 = effectScope()+ child2.run(() => {+ onScopeDispose(dispose2)+ })+ })++ expect(() => parent.stop()).not.toThrow()+ expect(parent.active).toBe(false)+ expect(child1.active).toBe(false)+ expect(child2.active).toBe(false)+ expect(dispose1).toHaveBeenCalledTimes(1)+ expect(dispose2).toHaveBeenCalledTimes(1)+ expect(parent.scopes!.length).toBe(0)+ })++ it('should resume all children when a resumed effect stops a sibling scope', () => {+ const counter = ref(0)+ const parent = effectScope()+ let child1!: EffectScope+ let child2!: EffectScope+ let child3!: EffectScope+ const spy3 = vi.fn(() => counter.value)+ parent.run(() => {+ child1 = effectScope()+ child1.run(() => {+ effect(() => {+ if (counter.value > 0) child2.stop()+ })+ })+ child2 = effectScope()+ child3 = effectScope()+ child3.run(() => {+ effect(spy3)+ })+ })+ expect(spy3).toHaveBeenCalledTimes(1)++ parent.pause()+ counter.value+++ expect(spy3).toHaveBeenCalledTimes(1)++ expect(() => parent.resume()).not.toThrow()+ expect(child2.active).toBe(false)+ expect(child3.active).toBe(true)+ // child3 must still be resumed and run its queued effect+ expect(spy3).toHaveBeenCalledTimes(2)+ counter.value+++ expect(spy3).toHaveBeenCalledTimes(3)+ })++ it('should not throw when a resumed effect stops its own scope', () => {+ const counter = ref(0)+ const scope = effectScope()+ const spy2 = vi.fn(() => counter.value)+ scope.run(() => {+ effect(() => {+ if (counter.value > 0) scope.stop()+ })+ effect(spy2)+ })+ expect(spy2).toHaveBeenCalledTimes(1)++ scope.pause()+ counter.value+++ expect(() => scope.resume()).not.toThrow()+ expect(scope.active).toBe(false)+ // the stopped sibling effect must not re-run+ expect(spy2).toHaveBeenCalledTimes(1)+ counter.value+++ expect(spy2).toHaveBeenCalledTimes(1)+ })++ it('should resume all effects when a resumed watcher stops itself', () => {+ const counter = ref(0)+ const scope = effectScope()+ const cb = vi.fn()+ const spy2 = vi.fn(() => counter.value)+ const spy3 = vi.fn(() => counter.value)+ scope.run(() => {+ watch(counter, cb, { flush: 'sync', once: true })+ effect(spy2)+ effect(spy3)+ })++ scope.pause()+ counter.value+++ expect(() => scope.resume()).not.toThrow()+ expect(cb).toHaveBeenCalledTimes(1)+ expect(spy2).toHaveBeenCalledTimes(2)+ expect(spy3).toHaveBeenCalledTimes(2)+ counter.value+++ expect(cb).toHaveBeenCalledTimes(1)+ expect(spy2).toHaveBeenCalledTimes(3)+ expect(spy3).toHaveBeenCalledTimes(3)+ })+ test('removing a watcher while stopping its effectScope', async () => { const count = ref(0) const scope = effectScope()packages/reactivity/src/effectScope.tsindex 03c15ed2f..aaa1ae6b7 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,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()+ 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()+ // resumed effects may run user code that stops this scope or its+ // effects, mutating this.effects during traversal+ const effects = this.effects.slice()+ for (i = 0, l = effects.length; i < l; i++) {+ effects[i].resume() } } }@@ -171,8 +176,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)+ // iterate over a copy: a child's cleanup may stop a sibling scope,+ // which removes the sibling from this.scopes during traversal+ 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 }