Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): avoid error during nested watch stop #5806

Closed
wants to merge 11 commits into from

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Apr 26, 2022

fix #5783
this because effects may be changed when an effect stoped.

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for vuejs-coverage failed.

Name Link
🔨 Latest commit 7b1c191
🔍 Latest deploy log https://app.netlify.com/sites/vuejs-coverage/deploys/6296bb411289c000084714ec

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for vue-sfc-playground ready!

Name Link
🔨 Latest commit 5164e99
🔍 Latest deploy log https://app.netlify.com/sites/vue-sfc-playground/deploys/6268d6d2b74d8e000861ac3e
😎 Deploy Preview https://deploy-preview-5806--vue-sfc-playground.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Apr 26, 2022

Deploy Preview for vue-next-template-explorer ready!

Name Link
🔨 Latest commit 5164e99
🔍 Latest deploy log https://app.netlify.com/sites/vue-next-template-explorer/deploys/6268d6d20d72cc000a13bf59
😎 Deploy Preview https://deploy-preview-5806--vue-next-template-explorer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@edison1105 edison1105 marked this pull request as draft April 26, 2022 02:02
@edison1105 edison1105 marked this pull request as ready for review April 26, 2022 02:09
@liulinboyi
Copy link
Member

liulinboyi commented Apr 26, 2022

@edison1105
Because of this, I feel this can use:

while (this.effects.length) {
    this.effects[0].stop()
}

@edison1105
Copy link
Member Author

@edison1105 Because of this, I feel this can use:

while (this.effects.length) {
    this.effects[0].stop()
}

but, Not every effect.stop() will change the effects

@liulinboyi
Copy link
Member

@edison1105 Because of this, I feel this can use:

while (this.effects.length) {
    this.effects[0].stop()
}

but, Not every effect.stop() will change the effects

You are right, let me have a think other methods.

@liulinboyi
Copy link
Member

liulinboyi commented Apr 26, 2022

@edison1105 Because of this, I feel this can use:

while (this.effects.length) {
    this.effects[0].stop()
}

but, Not every effect.stop() will change the effects

You are right, let me have a think other methods.

I feel this can use

const effects = this.effects ? [...this.effects] : []
for (i = 0, l = effects.length; i < l; i++) {
   effects[i].stop()
}

or

const effects = this.effects ? [...this.effects] : []
effects.forEach(e => e.stop())

or

(this.effects ? [...this.effects] : []).forEach(e => e.stop())

@github-actions
Copy link

github-actions bot commented Oct 20, 2023

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 98.7 kB (+5 B) 37.4 kB (+8 B) 33.7 kB (+17 B)
vue.global.prod.js 156 kB (+5 B) 57.2 kB (+7 B) 50.9 kB (+110 B)

Usages

Name Size Gzip Brotli
createApp 54.2 kB (+5 B) 21 kB (+7 B) 19.1 kB (+6 B)
createSSRApp 58.1 kB (+5 B) 22.6 kB (+9 B) 20.6 kB (-5 B)
defineCustomElement 58.8 kB (+5 B) 22.5 kB (+6 B) 20.5 kB (+64 B)
overall 67.8 kB (+5 B) 26 kB (+9 B) 23.7 kB (-34 B)

Copy link
Contributor

@skirtles-code skirtles-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I think this is a good change, but I'm not sure about the test.

I did an experiment where I 'fixed' the problem with the following crude hack:

      for (i = 0, l = this.effects.length; i < l; i++) {
        if (this.effects[i]) {
          this.effects[i].stop()
        }
      }

I don't think this is the right fix, but the proposed test does still pass with this code. I feel like there should be a test that fails with my dodgy fix.

packages/reactivity/src/effectScope.ts Outdated Show resolved Hide resolved
packages/runtime-core/__tests__/apiWatch.spec.ts Outdated Show resolved Hide resolved
@edison1105 edison1105 changed the title fix(reactivity): improve effectScope stop fix(reactivity): avoid error during nested watch stop Aug 13, 2024
@edison1105 edison1105 added the ready for review This PR requires more reviews label Aug 13, 2024
@skirtles-code
Copy link
Contributor

I still think there's a problem with the testing here.

The test confirms that stopping the effectScope doesn't throw an error, but it doesn't confirm that the watchers actually get stopped.

The current code in this PR is:

const effectsToStop = this.effects.slice()
for (i = 0, l = effectsToStop.length; i < l; i++) {
  effectsToStop[i].stop()
}

I believe this does stop the watchers correctly, but the test doesn't really check that.

Consider the code below. All it does is introduce an if check, rather than copying the array. This code also fixes the original error and passes the tests:

for (i = 0, l = this.effects.length; i < l; i++) {
  // This 'fix' would be incorrect, but it looks plausible
  if (this.effects[i]) {
    this.effects[i].stop()
  }
}

The code above looks like a plausible fix, but it's actually flawed. If an earlier effect is removed while the loop is being processed it will cause the next effect to be skipped.

Currently we don't have a test to protect against an incorrect implementation like this.

The test below would catch that problem. It passes for the change proposed on this PR but fails (correctly) for the incorrect fix I showed above:

  test('removing a watcher while stopping its effectScope', async () => {
    const count = ref(0)
    const scope = effectScope()
    let watcherCalls = 0
    let cleanupCalls = 0

    scope.run(() => {
      const stop1 = watch(count, () => {
        watcherCalls++
      })
      watch(count, (val, old, onCleanup) => {
        watcherCalls++
        onCleanup(() => {
          cleanupCalls++
          stop1()
        })
      })
      watch(count, () => {
        watcherCalls++
      })
    })

    expect(watcherCalls).toBe(0)
    expect(cleanupCalls).toBe(0)

    count.value++
    await nextTick()
    expect(watcherCalls).toBe(3)
    expect(cleanupCalls).toBe(0)

    scope.stop()
    count.value++
    await nextTick()
    expect(watcherCalls).toBe(3)
    expect(cleanupCalls).toBe(1)
  })

@edison1105
Copy link
Member Author

@skirtles-code
Thank you so much.

@yyx990803
Copy link
Member

2193284 avoids the extra array copy and also avoid unnecessary remove call when the effectScope is already stopped.

noootwo pushed a commit to noootwo/core that referenced this pull request Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready for review This PR requires more reviews scope: reactivity
Projects
Status: Rejected
Development

Successfully merging this pull request may close these issues.

throw Cannot read properties of undefined (reading 'stop') on component unmount, and use nested watch.
5 participants