Backtestsvuerun-20261003-231210#15083

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

Both patches fix the root cause the same way. They iterate over a shallow copy of `this.scopes` in `pause()`, `resume()` and `stop()`, and over a copy of `this.effects` in `resume()`. This stops a sibling's self-removal from the parent's array from shifting indices or causing out-of-bounds access. On correctness they are effectively equal for the reported crash and for the resume/watcher variant.

Patch A also copies `this.effects` in `pause()` and `stop()`. In `stop()`, `_active` is already false, so watch handles don't remove themselves from `scope.effects`, and pausing an effect doesn't run user code. These extra copies are therefore unnecessary but harmless defensive hardening, and they neither add risk nor obscure the fix.

On tests, both include a regression test for the issue's scenario (a child cleanup stops a sibling during `parent.stop()`) that would throw before the fix. Both also include a test where a watcher stopping a sibling watcher during `resume()` would hit an undefined entry before the fix. Patch A's tests are more thorough:
- The stop test uses three children, stops the last sibling, and asserts that every child is inactive, every cleanup ran exactly once, and `parent.scopes` is emptied.
- It adds a nested-scope resume test, where an effect in child A stops sibling B during resume and the test checks that C still resumes and stays reactive afterward.
- Its watcher resume test checks that a third watcher still resumes and keeps firing.

Patch A's final onStop test likely passes even without the fix, so it adds little, but it does no harm. Patch B's tests are leaner and check fewer outcomes.

On craft, Patch A adds a short explanatory comment at the key site; otherwise the two are stylistically equivalent. With equal correctness, Patch A wins on stronger tests.

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

6m 19s

Wall-clock time in the container

Full test suite

No new failures

3,618 passed · 6 skipped · 3,626 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

126vs 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+114 −12
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..86702a017 100644@@ -9,6 +9,7 @@ import {   onScopeDispose,   reactive,   ref,+  watch as baseWatch, } from '../src'  describe('reactivity/effect/scope', () => {@@ -435,4 +436,97 @@ 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>[] = []+    const children: EffectScope[] = []+    parent.run(() => {+      for (let i = 0; i < 3; i++) {+        const child = effectScope()+        const spy = vi.fn()+        cleanupSpies.push(spy)+        child.run(() => onScopeDispose(spy))+        children.push(child)+      }+    })+    // the first child stops its last sibling during cleanup+    children[0].run(() => onScopeDispose(() => children[2].stop()))++    expect(() => parent.stop()).not.toThrow()+    children.forEach(child => expect(child.active).toBe(false))+    cleanupSpies.forEach(spy => expect(spy).toHaveBeenCalledTimes(1))+    expect(parent.scopes!.length).toBe(0)+  })++  it('should resume all child scopes when a resumed effect stops a sibling scope', () => {+    const counter = ref(0)+    const parent = effectScope()+    let a!: EffectScope, b!: EffectScope, c!: EffectScope+    let cDummy: number | undefined+    parent.run(() => {+      a = effectScope()+      b = effectScope()+      c = effectScope()+    })+    a.run(() => {+      effect(() => {+        if (counter.value > 0) b.stop()+      })+    })+    c.run(() => {+      effect(() => {+        cDummy = counter.value+      })+    })++    parent.pause()+    counter.value+++    expect(cDummy).toBe(0)++    expect(() => parent.resume()).not.toThrow()+    expect(b.active).toBe(false)+    expect(cDummy).toBe(1)++    counter.value+++    expect(cDummy).toBe(2)+  })++  it('should resume all effects when a resumed watcher stops a sibling watcher', () => {+    const counter = ref(0)+    const scope = effectScope()+    const spy = vi.fn()+    let stopSecond!: () => void+    scope.run(() => {+      baseWatch(counter, () => stopSecond())+      stopSecond = baseWatch(counter, () => {})+      baseWatch(counter, spy)+    })++    scope.pause()+    counter.value+++    expect(spy).toHaveBeenCalledTimes(0)++    expect(() => scope.resume()).not.toThrow()+    expect(spy).toHaveBeenCalledTimes(1)++    counter.value+++    expect(spy).toHaveBeenCalledTimes(2)+  })++  it('should stop all effects when an effect onStop stops a sibling watcher', () => {+    const scope = effectScope()+    const counter = ref(0)+    const spy = vi.fn()+    let stopSecond!: () => void+    scope.run(() => {+      effect(() => counter.value, { onStop: () => stopSecond() })+      stopSecond = baseWatch(counter, () => {})+      baseWatch(counter, spy)+    })++    expect(() => scope.stop()).not.toThrow()+    counter.value+++    expect(spy).not.toHaveBeenCalled()+  }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..24b7f55a7 100644@@ -70,12 +70,14 @@ 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++) {-        this.effects[i].pause()+      const effects = this.effects.slice()+      for (i = 0, l = effects.length; i < l; i++) {+        effects[i].pause()       }     }   }@@ -89,12 +91,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()         }       }     }@@ -160,8 +164,9 @@ export class EffectScope {     if (this._active) {       this._active = false       let i, l-      for (i = 0, l = this.effects.length; i < l; i++) {-        this.effects[i].stop()+      const effects = this.effects.slice()+      for (i = 0, l = effects.length; i < l; i++) {+        effects[i].stop()       }       this.effects.length = 0 @@ -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 itself 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       }