Backtestsvuerun-20261007-145226#15083

Issue #15083 Merged fix PR #15084 Run 7 Oct 2026 · run-20261007-145226 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.

Both patches make the same source change in effectScope.ts. Each one iterates over a `.slice()` copy of `this.scopes` in pause(), resume() and stop(), and of `this.effects` in resume(). So both fix the root cause equally: the cached-length loop breaks when a child removes itself from the parent's array during traversal. They also harden the same adjacent paths, so correctness is a wash.

The difference is in tests and craft.

Patch A adds two tests, and both would fail before the fix:
- a sibling scope stopped from a cleanup during `parent.stop()`;
- a sync watcher that stops a sibling watcher during `resume()`.
They are minimal, mostly checking that nothing throws plus a couple of state checks.

Patch B adds three tests covering all three mutation paths the fix touches:
- `stop()` with a child cleanup stopping a sibling;
- scope `resume()` where a resumed effect stops a sibling scope;
- effects `resume()` where a `once` watcher removes itself.
Its assertions are stronger. They check that every child (including the third, which must not be skipped) ends up inactive, that each cleanup runs exactly once, that `parent.scopes` is emptied, and that the remaining effects actually re-run with the right values. That verifies the traversal still reaches every element, not just that it no longer crashes.

Patch B also adds brief comments explaining why the copies are needed. This helps future readers who might otherwise 'optimize' the slice away.

With identical fixes, Patch B's more thorough and more meaningful tests and its clearer intent give it the edge.

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

4m 46s

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

117vs 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+109 −8
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..0e8c556ee 100644@@ -9,6 +9,7 @@ import {   onScopeDispose,   reactive,   ref,+  watch as baseWatch, } from '../src'  describe('reactivity/effect/scope', () => {@@ -435,4 +436,94 @@ 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', () => {+    const parent = effectScope()+    const cleanupSpies: ReturnType<typeof vi.fn>[] = []+    let first: EffectScope+    let second: EffectScope+    let third: EffectScope+    parent.run(() => {+      first = effectScope()+      second = effectScope()+      third = effectScope()+      first.run(() => {+        onScopeDispose(() => second.stop())+      })+      for (const s of [second, third]) {+        const spy = vi.fn()+        cleanupSpies.push(spy)+        s.run(() => onScopeDispose(spy))+      }+    })++    expect(() => parent.stop()).not.toThrow()+    expect(parent.active).toBe(false)+    expect(first!.active).toBe(false)+    expect(second!.active).toBe(false)+    expect(third!.active).toBe(false)+    expect(cleanupSpies[0]).toHaveBeenCalledTimes(1)+    expect(cleanupSpies[1]).toHaveBeenCalledTimes(1)+    expect(parent.scopes!.length).toBe(0)+  })++  it('should resume all child scopes when resuming an effect stops a sibling', () => {+    const parent = effectScope()+    const counter = ref(0)+    let first: EffectScope+    let second: EffectScope+    let third: EffectScope+    const thirdSpy = vi.fn()+    parent.run(() => {+      first = effectScope()+      second = effectScope()+      third = effectScope()+      first.run(() => {+        effect(() => {+          if (counter.value > 0) second.stop()+        })+      })+      third.run(() => {+        effect(() => {+          thirdSpy(counter.value)+        })+      })+    })+    expect(thirdSpy).toHaveBeenCalledTimes(1)++    parent.pause()+    counter.value+++    expect(thirdSpy).toHaveBeenCalledTimes(1)++    expect(() => parent.resume()).not.toThrow()+    expect(second!.active).toBe(false)+    expect(third!.active).toBe(true)+    expect(thirdSpy).toHaveBeenCalledTimes(2)+    expect(thirdSpy).toHaveBeenLastCalledWith(1)+  })++  it('should resume all effects when a resumed watcher removes itself from the scope', () => {+    const scope = effectScope()+    const counter = ref(0)+    const onceSpy = vi.fn()+    const effectSpy = vi.fn()+    scope.run(() => {+      baseWatch(counter, onceSpy, { once: true })+      effect(() => {+        effectSpy(counter.value)+      })+    })+    expect(scope.effects.length).toBe(2)++    scope.pause()+    counter.value+++    expect(onceSpy).toHaveBeenCalledTimes(0)+    expect(effectSpy).toHaveBeenCalledTimes(1)++    expect(() => scope.resume()).not.toThrow()+    expect(onceSpy).toHaveBeenCalledTimes(1)+    expect(effectSpy).toHaveBeenCalledTimes(2)+    expect(effectSpy).toHaveBeenLastCalledWith(1)+    expect(scope.effects.length).toBe(1)+  }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..f356f7e1d 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: children may be removed from the array during+        // 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++) {@@ -88,13 +91,17 @@ export class EffectScope {       if (this._isPaused) {         this._isPaused = false         let i, l+        // iterate over copies: resuming an effect may trigger user code that+        // stops this scope or its children, mutating these arrays         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 +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)+        // 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       }