Backtestsvuerun-20261001-022054#15083
EffectScope stop() causes runtime crash (Cannot read properties of undefined) when dynamic scope cleanup mutates parent.scopes array during traversal
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the root cause the same way: they iterate over a shallow copy (`slice()`) of `this.scopes` in `pause()`, `resume()` and `stop()`. Both `stop()` regression tests are valid; each would throw before the fix and pass after. Patch B's test asserts a little more, such as the sibling's dispose spy firing once and the scopes being cleared. Patch A goes further on the issue's own complaint about the `resume()` loop: it also copies `this.effects` before calling `effect.resume()`. That loop has the same crash. A resumed sync watcher can run its callback, which stops a sibling watcher, and the watcher handle's stop removes that effect from `scope.effects`. The cached-length loop then reads past the end of the array. Patch A adds a test for exactly this case (pause, trigger, resume with `flush: 'sync'` watchers), and that test would fail without the change. So Patch A fixes a second real path to the same mutation-during-traversal crash, with low risk and code consistent with the rest of the fix. Patch B leaves that path vulnerable. Apart from this, the two patches are essentially identical in craft and scope, so the extra resume coverage gives A 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
2m 26s8 turns
Wall-clock time in the container
Full test suite
No new failures
3,615 passed · 6 skipped · 3,623 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
39vs 56 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.28
Link to minimal reproduction
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
packages/reactivity/__tests__/effectScope.spec.tsindex 071c8286c..ebf37acbf 100644@@ -229,6 +229,30 @@ describe('reactivity/effect/scope', () => { expect(parent.scopes!.includes(child)).toBe(false) }) + it('should not throw when a child scope stops a sibling scope during parent stop', () => {+ const parent = effectScope()+ const spy = vi.fn()+ let child1!: EffectScope+ let child2!: EffectScope+ parent.run(() => {+ child1 = effectScope()+ child2 = effectScope()+ child1.run(() => {+ onScopeDispose(() => child2.stop())+ })+ child2.run(() => {+ onScopeDispose(spy)+ })+ })++ expect(() => parent.stop()).not.toThrow()+ expect(parent.active).toBe(false)+ expect(child1.active).toBe(false)+ expect(child2.active).toBe(false)+ expect(spy).toHaveBeenCalledTimes(1)+ expect(parent.scopes?.length ?? 0).toBe(0)+ })+ it('test with higher level APIs', async () => { const r = ref(1) packages/reactivity/src/effectScope.tsindex 03c15ed2f..549972a5c 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,8 +90,9 @@ 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++) {@@ -171,8 +173,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 }
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 }
What the agent reported
Calling parent.stop() no longer crashes when one child scope stops a sibling during cleanup. The new regression test failed before the fix and passes after it, and the wider test run passes.
What was wrong: In packages/reactivity/src/effectScope.ts, stop() walks this.scopes using the length it read before the loop started. Child scopes are stopped with stop(true), so they normally stay in the parent's list. But when a child's onScopeDispose calls stop() on a sibling without that flag, the sibling removes itself from the parent's scopes array. The array gets shorter, the loop reaches an index past the end, gets undefined, and throws TypeError: Cannot read properties of undefined (reading 'stop').
What changed:
- Fix:
pause(),resume()andstop()now loop over a shallow copy (const scopes = this.scopes.slice()) instead of the live array. Instop(), a sibling that already stopped itself is still in the copy, but stopping it again does nothing because it is already inactive.stop()still emptiesthis.scopesafterwards. The loops over effects and cleanups are unchanged. - Test: I added one test to
packages/reactivity/__tests__/effectScope.spec.ts, "should not throw when a child scope stops a sibling scope during parent stop". It builds a parent withchild1andchild2, wherechild1'sonScopeDisposecallschild2.stop(). It then checks thatparent.stop()doesn't throw, that all three scopes are inactive, thatchild2's cleanup ran exactly once, and thatparent.scopesends up empty.
Test evidence:
- Before the fix: the new test failed with exactly the reported error. The other 20 tests in the file passed.
- After the fix: the file passes, 21 of 21.
- Wider check: every component and watcher uses
EffectScope, so I also ran./bt-test packages/reactivity packages/runtime-core: 59 test files passed and 1 was skipped; 1475 tests passed and 4 were skipped. No existing test was changed. - Review: a read-only review found the fix correct and in scope.
git diff --statshows only the two files above changed.
Requirements:
- The parent stops all its children without throwing when one child stops a sibling during cleanup: done, and covered by the new test (failed before, passes after).
- Fix the loop in
stop()with a shallow copy, as the report suggests: done. - Apply the same fix to
pause()andresume(), which the report also names: done, without dedicated tests. resume()can run queued effects, which is user code that could stop a sibling scope, so the copy protects a real path. I didn't write a test for that path.pause()never runs user code, so its copy is only for consistency with the other two methods and the report's suggestion.- The existing pause/resume tests still pass.
Everything is left as uncommitted edits in the working tree.