Backtestsvuerun-20261013-093701#15083

Issue #15083 Merged fix PR #15084 Run 13 Oct 2026 · run-20261013-093701 vuejs/core

EffectScope stop() causes runtime crash (Cannot read properties of undefined) when dynamic scope cleanup mutates parent.scopes array during traversal

Better

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 snapshot `this.scopes` with `slice()` in pause(), resume() and stop(), and also snapshot `this.effects` in resume(). Both fix the root cause: the parent loop iterates over a cached length while children remove themselves through pop/swap. Correctness is therefore equal.

The difference is in the tests. Patch A's two tests do fail before the fix: a child's cleanup stops the last sibling, and a resumed watcher stops a sibling watcher. However, they mostly assert `not.toThrow()` plus minimal state.

Patch B's tests are more thorough:
- **Two removal positions.** They cover a middle sibling (where swap-remove moves the last child into the vacated slot) and the last sibling.
- **Completeness, not just no crash.** They assert every child ends up inactive and the exact disposal order. This guards against silently skipped children, not just the crash.
- **Resume through child scopes and watchers.** They check that the remaining siblings are actually resumed and keep reacting on later updates (run counts [2,1,2] then [3,1,3]).

Patch B also adds brief comments explaining why each loop iterates over a snapshot, which helps future maintainers without adding risk. With the fixes identical, Patch B's stronger, behavior-verifying tests and clearer intent make it the better patch.

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

5m 53s

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

135vs 56 merged

Changed lines, ours vs merged

Vue version

3.5.28

Link to minimal reproduction

https://play.vuejs.org/#eNqFVN1u0zAUfpUjCymdtKVsBYRCW37GLuAC0DrBTS6WOaeJN9e2bKftVPWCR+SNOHaStmzTkNrY8fnOd36+E2/YR2PSZYMsY2PHrTAeHPrGTHMlFkZbDxvA+Ry5n3Ft8Bi0ipvPwhnt6N3iHLYwt3oBCfEkucoV18oRjy9842ASIIPkXAp+B96KqkILf37bwtXgNdhGga8RPDqfJkd79w56HoETGBzBZAqbXEFHnC4L2SBZkstGKaGqSEHxAcK/JTGFReUJdFDDIAaBzpZSAoMD8t6T10KWp489HyDO/osYPR2dMDHC4/itt5aYSl0NdtW18NChFvSvED1FrL1jifmlzmtDxuGQ2iwcrISUsemrGlXfgxbTu267XbuGZ1i9ve8zfMLroSazhnN0LsqxBV54XsMArT3qOR44XPOoc4meGoVlBi82hE4XxFFUuL3etzU0hkzaRrrInyv6jYft/NLk0ovHhZGFR3oDGJdiSQHvJU5yZoqypH5mcPbSrN/lLCIIU4+mPymVEVzsxaKiiyVaV0ho87NorC4b7oWmgOTSOd803msFH3iYcgpyOLw5exz7rVnD6RuKD7yxTtsMjBbKo90nBHDVfSwtSxto2Ebq45od9aKwlVAnJEkGp68D81wrf7JCUdU+gxsty0PuWWx/BmNnCgVZz7KhFsuQTicPLda7X8LXgyR2IDmC95BYLBPIIKksokpIADbdbPoPfrslKYi1z3EYLpOwkgq0Gw932rBj5h1pOhdVeuu0oksoTkfOuF4YIdF+N6HTLmdZPzc5K6TUq6/xzNsGj/tzXiO/e+L81q3DWc5+WHRol5iznY3Kq9C35ovZN1zTfmdckNKS0M8YL5HmsQk5trBPjSop7QNczPZLvEpJ+St3sfaoXF9USDSOcMTnjG7Q82dK36c7Sl/1o8+2fwEpfeZ+

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

Our patch+127 −8
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..3dcdcf2c1 100644@@ -435,4 +435,113 @@ describe('reactivity/effect/scope', () => {     expect(rs.value.stage).toBe(1)     expect(renderCount).toBe(5)   })++  it('should stop all children when a child cleanup stops a later sibling', () => {+    const parent = effectScope()+    const children: EffectScope[] = []+    const disposed: number[] = []+    parent.run(() => {+      for (let i = 0; i < 3; i++) {+        const child = effectScope()+        children.push(child)+        child.run(() => {+          onScopeDispose(() => {+            disposed.push(i)+            if (i === 0) children[1].stop()+          })+        })+      }+    })++    expect(() => parent.stop()).not.toThrow()+    expect(parent.active).toBe(false)+    expect(children.every(c => !c.active)).toBe(true)+    expect(disposed).toEqual([0, 1, 2])+  })++  it('should stop all children when a child cleanup stops the last sibling', () => {+    const parent = effectScope()+    const children: EffectScope[] = []+    const disposed: number[] = []+    parent.run(() => {+      for (let i = 0; i < 3; i++) {+        const child = effectScope()+        children.push(child)+        child.run(() => {+          onScopeDispose(() => {+            disposed.push(i)+            if (i === 0) children[2].stop()+          })+        })+      }+    })++    expect(() => parent.stop()).not.toThrow()+    expect(children.every(c => !c.active)).toBe(true)+    expect(disposed).toEqual([0, 2, 1])+  })++  it('should resume all children when a resumed effect stops a sibling scope', () => {+    const parent = effectScope()+    const counter = ref(0)+    const children: EffectScope[] = []+    const runs = [0, 0, 0]+    parent.run(() => {+      for (let i = 0; i < 3; i++) {+        const child = effectScope()+        children.push(child)+        child.run(() => {+          effect(() => {+            counter.value+            runs[i]+++            if (i === 0 && counter.value > 0) children[1].stop()+          })+        })+      }+    })+    expect(runs).toEqual([1, 1, 1])++    parent.pause()+    counter.value+++    expect(runs).toEqual([1, 1, 1])++    expect(() => parent.resume()).not.toThrow()+    expect(children[1].active).toBe(false)+    // the remaining sibling must still be resumed and re-run+    expect(runs).toEqual([2, 1, 2])++    counter.value+++    expect(runs).toEqual([3, 1, 3])+  })++  it('should resume all effects when a resumed effect stops a sibling watcher', () => {+    const scope = effectScope()+    const counter = ref(0)+    const runs = [0, 0, 0]+    let stopSecond: (() => void) | undefined+    scope.run(() => {+      for (let i = 0; i < 3; i++) {+        const stop = watchEffect(+          () => {+            counter.value+            runs[i]+++            if (i === 0 && counter.value > 0) stopSecond!()+          },+          { flush: 'sync' },+        )+        if (i === 1) stopSecond = stop+      }+    })+    expect(runs).toEqual([1, 1, 1])++    scope.pause()+    counter.value+++    expect(runs).toEqual([1, 1, 1])++    expect(() => scope.resume()).not.toThrow()+    expect(runs).toEqual([2, 1, 2])++    counter.value+++    expect(runs).toEqual([3, 1, 3])+  }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..a6077e8a5 100644@@ -70,8 +70,11 @@ 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()+        // iterate over a snapshot, consistent with resume() and stop(), so a+        // child removing itself from this.scopes cannot break the traversal+        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 +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()+          // resuming may run queued effects whose user code can stop sibling+          // scopes or watchers, mutating these arrays: iterate over snapshots+          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)+        // a child's cleanup may stop a sibling scope, which removes itself+        // from this.scopes: iterate over a snapshot+        const scopes = this.scopes.slice()+        for (i = 0, l = scopes.length; i < l; i++) {+          scopes[i].stop(true)         }         this.scopes.length = 0       } 
Merged fix · PR #15084+48 −8
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       }