Backtestsvuerun-20261005-155915#15083

Issue #15083 Merged fix PR #15084 Run 5 Oct 2026 · run-20261005-155915 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 fix the reported crash correctly; B wins on test coverage and slightly broader hardening.

**Patch A** iterates over shallow copies in `stop()`, `pause()` and `resume()` (scopes and, in `resume`, effects). When a child's cleanup stops a sibling, the sibling still swap-removes itself from the live `parent.scopes`. The copy is unaffected, and the later `stop(true)` on the already-inactive sibling is a no-op. This fixes the crash exactly as the issue suggests.

**Patch B** slices in `pause`/`resume` for both scopes and effects. For `stop`, it prevents the mutation itself rather than copying: a child skips removing itself from the parent when `parent._active` is false. `_active` is cleared at the start of `stop()`, so any parent currently traversing its children is already inactive. No array that is mid-traversal gets mutated, and the parent clears its array afterwards anyway. This addresses the root cause, avoids pointless O(1) swap work during teardown, and carries little risk. It also covers the 'parent cleanup stops a child' ordering and the effects array in `pause`.

**Tests.** A has one minimal stop test that only asserts no throw and the sibling being inactive, plus one resume test with watchers. B's tests are more rigorous:
- three sibling scopes, asserting each cleanup is called exactly once and the parent's scopes are emptied;
- the parent-cleanup ordering case;
- a resume-triggered sibling stop that checks the remaining scope count and that later teardown still works.

B's tests would fail before the fix and verify behavioral correctness beyond 'does not throw'.

**Craft.** Both are clean, and B adds helpful comments explaining the invariant. Since correctness is roughly equal, B's stronger tests and more thorough coverage of the mutation paths 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

5m 6s

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

100vs 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+90 −10
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..05991e28a 100644@@ -435,4 +435,75 @@ describe('reactivity/effect/scope', () => {     expect(rs.value.stage).toBe(1)     expect(renderCount).toBe(5)   })++  it('should not crash when a child cleanup stops a sibling scope during parent stop', () => {+    const parent = effectScope()+    const cleanupB = vi.fn()+    const cleanupC = vi.fn()+    let b!: EffectScope+    let c!: EffectScope+    parent.run(() => {+      const a = effectScope()+      b = effectScope()+      c = effectScope()+      a.run(() => onScopeDispose(() => b.stop()))+      b.run(() => onScopeDispose(cleanupB))+      c.run(() => onScopeDispose(cleanupC))+    })+    expect(parent.scopes!.length).toBe(3)++    expect(() => parent.stop()).not.toThrow()+    expect(cleanupB).toHaveBeenCalledTimes(1)+    expect(cleanupC).toHaveBeenCalledTimes(1)+    expect(b.active).toBe(false)+    expect(c.active).toBe(false)+    expect(parent.scopes!.length).toBe(0)+  })++  it('should not crash when a parent cleanup stops a child scope during parent stop', () => {+    const parent = effectScope()+    const cleanupB = vi.fn()+    let a!: EffectScope+    let b!: EffectScope+    parent.run(() => {+      a = effectScope()+      b = effectScope()+      b.run(() => onScopeDispose(cleanupB))+      onScopeDispose(() => a.stop())+    })++    expect(() => parent.stop()).not.toThrow()+    expect(a.active).toBe(false)+    expect(b.active).toBe(false)+    expect(cleanupB).toHaveBeenCalledTimes(1)+  })++  it('should not crash when resuming a child triggers stopping a sibling scope', () => {+    const parent = effectScope()+    const source = ref(0)+    const cleanupC = vi.fn()+    let b!: EffectScope+    let c!: EffectScope+    parent.run(() => {+      const a = effectScope()+      b = effectScope()+      c = effectScope()+      a.run(() => {+        watch(source, () => b.stop(), { flush: 'sync' })+      })+      c.run(() => onScopeDispose(cleanupC))+    })++    parent.pause()+    source.value+++    expect(b.active).toBe(true)++    expect(() => parent.resume()).not.toThrow()+    expect(b.active).toBe(false)+    expect(c.active).toBe(true)+    expect(parent.scopes!.length).toBe(2)++    parent.stop()+    expect(cleanupC).toHaveBeenCalledTimes(1)+  }) })packages/reactivity/src/effectScope.tsindex 03c15ed2f..7ae2a0559 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()       }     }   }@@ -88,13 +90,17 @@ export class EffectScope {       if (this._isPaused) {         this._isPaused = false         let i, l+        // iterate over copies: resuming an effect may synchronously trigger+        // user code that stops sibling scopes / effects and mutates the 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()         }       }     }@@ -177,8 +183,11 @@ export class EffectScope {         this.scopes.length = 0       } -      // nested scope, dereference from parent to avoid memory leaks-      if (!this.detached && this.parent && !fromParent) {+      // nested scope, dereference from parent to avoid memory leaks.+      // skip if the parent is itself stopping: it will clear its scopes array+      // after the traversal, and mutating it mid-traversal (e.g. a sibling+      // stopped from a cleanup) would break the parent's loop.+      if (!this.detached && this.parent && this.parent._active && !fromParent) {         // optimized O(1) removal         const last = this.parent.scopes!.pop()         if (last && last !== this) { 
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       }