Backtestsvuerun-20261005-124535#15083

Issue #15083 Merged fix PR #15084 Run 5 Oct 2026 · run-20261005-124535 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 snapshot `this.scopes` with `.slice()` before iterating in `stop()`, `pause()` and `resume()`, and also snapshot `this.effects` in `resume()`. That covers the reported crash and the analogous crash during resume, where a resumed effect stops a sibling or removes itself. Patch A also copies `effects` in `pause()`. This is harmless hardening and earns no extra credit, since pausing does not run user code.

The tests decide it. Both patches add regression tests that would fail before the change.

- **Patch A, stop:** with three children, child `a` stops sibling `c`. The test then checks that every cleanup ran, every child is inactive, and `parent.scopes` is empty.
- **Patch A, resume:** two tests. In one, a resumed effect stops a sibling scope. In the other, a `once` watcher removes its own effect. Both assert that the element after the removed one is still resumed (`bSpy` and `otherSpy` call counts, including after a later trigger). This is the semantic point of iterating over a snapshot: no sibling is skipped.
- **Patch B, stop:** reproduces the crash and checks the sibling is inactive.
- **Patch B, resume:** a sync watcher stops another watcher. It checks only no-throw and call counts. It does not verify that remaining effects are still correctly resumed.

Correctness is equivalent and scope is comparable. Patch A wins on more thorough, behaviour-verifying tests and clear 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

8m 9s

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

114vs 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+104 −10
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..5f69bb9d7 100644@@ -9,6 +9,7 @@ import {   onScopeDispose,   reactive,   ref,+  watch as baseWatch, } from '../src'  describe('reactivity/effect/scope', () => {@@ -435,4 +436,87 @@ 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 sibling scope', () => {+    const parent = effectScope()+    const stopped: string[] = []+    let a!: EffectScope, b!: EffectScope, c!: EffectScope+    parent.run(() => {+      a = effectScope()+      b = effectScope()+      c = effectScope()+      a.run(() => {+        onScopeDispose(() => {+          stopped.push('a')+          // stopping a sibling removes it from parent.scopes mid-traversal+          c.stop()+        })+      })+      b.run(() => onScopeDispose(() => stopped.push('b')))+      c.run(() => onScopeDispose(() => stopped.push('c')))+    })++    expect(() => parent.stop()).not.toThrow()+    expect(stopped.sort()).toEqual(['a', 'b', 'c'])+    expect(a.active).toBe(false)+    expect(b.active).toBe(false)+    expect(c.active).toBe(false)+    expect(parent.scopes!.length).toBe(0)+  })++  it('should resume all children when a resumed effect stops a sibling scope', () => {+    const parent = effectScope()+    const counter = ref(0)+    const bSpy = vi.fn(() => counter.value)+    let a!: EffectScope, b!: EffectScope, c!: EffectScope+    parent.run(() => {+      a = effectScope()+      b = effectScope()+      c = effectScope()+      a.run(() => {+        effect(() => {+          if (counter.value > 0) c.stop()+        })+      })+      b.run(() => effect(bSpy))+      c.run(() => effect(() => counter.value))+    })+    expect(bSpy).toHaveBeenCalledTimes(1)++    parent.pause()+    counter.value+++    expect(bSpy).toHaveBeenCalledTimes(1)++    expect(() => parent.resume()).not.toThrow()+    expect(c.active).toBe(false)+    // sibling scope after the stopped one must still be resumed+    expect(bSpy).toHaveBeenCalledTimes(2)+    counter.value+++    expect(bSpy).toHaveBeenCalledTimes(3)+  })++  it('should resume all effects when a resumed effect removes itself from the scope', () => {+    const scope = effectScope()+    const counter = ref(0)+    const onceSpy = vi.fn()+    const otherSpy = vi.fn(() => counter.value)+    scope.run(() => {+      // a `once` watcher removes its effect from scope.effects when it fires+      baseWatch(counter, onceSpy, { once: true })+      effect(otherSpy)+    })+    expect(otherSpy).toHaveBeenCalledTimes(1)++    scope.pause()+    counter.value+++    expect(onceSpy).toHaveBeenCalledTimes(0)++    expect(() => scope.resume()).not.toThrow()+    expect(onceSpy).toHaveBeenCalledTimes(1)+    expect(scope.effects.length).toBe(1)+    // the effect shifted into the removed one's slot must still be resumed+    expect(otherSpy).toHaveBeenCalledTimes(2)+    counter.value+++    expect(otherSpy).toHaveBeenCalledTimes(3)+  }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..0228cc166 100644@@ -69,13 +69,16 @@ export class EffectScope {     if (this._active) {       this._isPaused = true       let i, l+      // iterate over copies: children may be stopped / removed while looping       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()       }     }   }@@ -88,13 +91,17 @@ export class EffectScope {       if (this._isPaused) {         this._isPaused = false         let i, l+        // iterate over copies: resuming may run effects that stop sibling+        // scopes or remove effects (e.g. `once` watchers) from this scope         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 it from this.scopes while we are looping+        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       }