Backtestsvuerun-20261002-115633#15083

Issue #15083 Merged fix PR #15084 Run 2 Oct 2026 · run-20261002-115633 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 A = our agent, Patch B = the merged fix.

The source changes are functionally identical. Both patches iterate over a `slice()` copy of `this.scopes` in `pause()`, `resume()` and `stop()`, and over a copy of `this.effects` in `resume()`. That fixes the root cause: the sibling removes itself from `parent.scopes` (via pop/swap) while the loop still uses a cached length. The only source difference is that A adds short comments explaining why each copy is needed, which helps maintainability.

The tests are where the patches differ. Both add a stop test that would crash before the fix and pass after. In both, the stopped sibling is the last element, so it is popped and the cached index lands on undefined. Both also add a resume test where a watcher stops a sibling watcher, covering the effects-copy path.

A's tests are more thorough. Its stop test uses three children and checks that every child is inactive, every dispose spy ran exactly once, and `parent.scopes` ends empty. That confirms no child was skipped, not just that nothing threw. A also adds a test for the child-scope loop in `resume()`: the first child's effect stops the last sibling on resume. It checks the resulting state and that later reactivity still works. B has no test for the scopes branch of `resume()` and asserts less in its stop test.

With identical fixes, A wins on stronger test coverage and clearer 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

5m 27s

Wall-clock time in the container

Full test suite

No new failures

3,617 passed · 6 skipped · 3,625 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

105vs 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+97 −8
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..f74fb5624 100644@@ -435,4 +435,81 @@ describe('reactivity/effect/scope', () => {     expect(rs.value.stage).toBe(1)     expect(renderCount).toBe(5)   })++  it('should stop all child scopes when a child cleanup stops a sibling scope', () => {+    const parent = effectScope()+    const spies: (() => void)[] = []+    const children: EffectScope[] = []+    parent.run(() => {+      for (let i = 0; i < 3; i++) {+        const child = effectScope()+        const spy = vi.fn()+        spies.push(spy)+        children.push(child)+        child.run(() => onScopeDispose(spy))+      }+    })+    // first child's cleanup stops the last sibling+    children[0].run(() => onScopeDispose(() => children[2].stop()))++    expect(() => parent.stop()).not.toThrow()+    children.forEach(c => expect(c.active).toBe(false))+    spies.forEach(spy => expect(spy).toHaveBeenCalledTimes(1))+    expect(parent.scopes!.length).toBe(0)+  })++  it('should resume all child scopes when resuming stops a sibling scope', () => {+    const counter = ref(0)+    const parent = effectScope()+    const children: EffectScope[] = []+    const dummies = [0, 0, 0]+    parent.run(() => {+      for (let i = 0; i < 3; i++) {+        const child = effectScope()+        children.push(child)+        child.run(() => {+          effect(() => {+            dummies[i] = counter.value+            // resuming the first child's effect stops the last sibling+            if (i === 0 && counter.value === 1) children[2].stop()+          })+        })+      }+    })++    parent.pause()+    counter.value = 1+    expect(() => parent.resume()).not.toThrow()+    expect(dummies).toEqual([1, 1, 0])+    expect(children[1].active).toBe(true)+    expect(children[2].active).toBe(false)++    counter.value = 2+    expect(dummies).toEqual([2, 2, 0])+  })++  it('should resume all effects when a resumed effect stops a sibling watcher', () => {+    const counter = ref(0)+    const scope = effectScope()+    const calls = [0, 0, 0]+    scope.run(() => {+      let stopLast: () => void+      watch(+        counter,+        () => {+          calls[0]+++          stopLast()+        },+        { flush: 'sync' },+      )+      watch(counter, () => calls[1]++, { flush: 'sync' })+      stopLast = watch(counter, () => calls[2]++, { flush: 'sync' })+    })++    scope.pause()+    counter.value+++    expect(() => scope.resume()).not.toThrow()+    expect(calls).toEqual([1, 1, 0])+    expect(scope.effects.length).toBe(2)+  }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..dc66ccc15 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 copy: child scopes may be stopped (and thus removed+        // from this.scopes) as a side effect of 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,18 @@ 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()+          // iterate over a copy: resuming may run user code that stops child+          // scopes, which removes them from this.scopes+          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 and stop sibling watchers, which removes+        // them from this.effects+        const effects = this.effects.slice()+        for (i = 0, l = effects.length; i < l; i++) {+          effects[i].resume()         }       }     }@@ -171,8 +180,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       } 
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       }