Creating a vm in a detached EffectScope before any other vm is initialized breaks tracking of the activeEffectScope

This issue has been created since 2022-10-12.



Reproduction link

Super slim repro on stackblitz

Steps to reproduce

Just open the stackblitz reproduction and check the console. You'll see:

[Vue warn]: onScopeDispose() is called when there is no active effect scope to be associated with.

In general though, you can cause the issue like so:

  1. Create a detached effect scope before creating a regular vue VM
  2. Do something in the run block of that scope (like lazily import a file with a store definition) that causes a Vue VM to be created
  3. Call onScopeDispose()

What is expected?

The active scope tracking should be maintained, and not get "tricked" by not having an ancestor VM.

What is actually happening?

When setCurrentInstance(vm) is called, it passes prev as the vm argument, which is null. This leads to .off being called on the detached EffectScope that Vue 2.7 creates when a VM is initialized. This is not the detached scope that we created manually, mind you. And since .off is not supposed to be called on a detached EffectScope (it says this in explicitly in code comments), bad stuff happens and Vue's global activeEffectScope becomes undefined.

This doesn't currently happen in our app at runtime (though I think it easily could), it happens during our test suite. We try to be as lazy as possible in our test environment when it comes to the code that each test imports, because it substantially helps jest run time, especially when running individual test files. Because of this, we use the lazy option of the commonjs babel plugin. This means the Vuex store that would normally be instantiated before the detached effect scope ends up getting created after the detached effect scope. But I haven't seen anything in the Vue docs to say I shouldn't be able to create a detached effect scope before any other Vue VM, so I think this is a legitimate bug?

I'd be willing to work on a PR to fix this, but I'd need guidance, since my limited knowledge base of Vue's internals would probably mean any solutions I would try might break other things.

Update: Just adding a reference to related issue in Vue 3. Not sure how similar the internals of the effect scope implementation/tracking are, but right here @posva makes a comment that "you would never call off on a detached effect scope". But that does, indeed seem to be exactly what Vue 2.7 does. It seems like the buggy behavior the person who brought this up was worried about does indeed occur in some cases.

Aaron-Pool wrote this answer on 2022-10-15

Update: I don't actually see the reason why .off() is ever called on currentInstance._scope. From what I can see _scope is only ever assigned to a detached scope. The only assignment to that field I can find is in initMixin, where vm._scope = new EffectScope(true) is called.

Aaron-Pool wrote this answer on 2022-10-15

Appreciate the quick fix @yyx990803 ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰

More Details About Repo
Owner Name vuejs
Repo Name vue
Full Name vuejs/vue
Language TypeScript
Created Date 2013-07-29
Updated Date 2022-12-10
Star Count 201155
Watcher Count 6070
Fork Count 33134
Issue Count 595


Issue Title Created Date Comment Count Updated Date
Problems with mark signs 6 2022-04-10 2022-04-26
Recaptcha slows down loading pages and decreases SEO 1 2021-04-17 2022-10-02
Some transfer values 0 0 2021-08-18 2022-10-31
Animation<T>ใฎstructๅˆถ้™ใ‚’ๅค–ใ™ 0 2022-01-14 2022-10-29
Steamworks API integration 2 2022-02-20 2022-09-24
[BUG] Multiple cable blocks in SIMULINK 5 2022-03-13 2022-10-02
Are emojis supported ? Black squares are appearing 0 2022-05-25 2022-11-20
use detailed_itineraries on Frequency-based GTFS feeds 9 2021-07-15 2022-12-03
How to sort a column with combined values as accessor? 2 2022-01-14 2022-10-19
IncomingUploadType โ€”โ€” Build Error 1 2022-04-08 2022-10-06
Generating right side image 3 2021-11-17 2022-11-20
Dataclasses not supported 3 2021-06-08 2022-12-02
Add image to horizontal dropdown 5 2022-07-16 2022-12-01
"The task cannot be run because the user account that is assigned to run the task has been altered" 0 2021-01-29 2022-10-28
Can't set selected option just using ID 1 2022-07-14 2022-12-08
[question] how to use with express? 0 2021-07-19 2022-11-29
Meshcentral Assistant not working properly 3 2021-07-29 2022-11-13
Unstable USB connection with iOS Devices 3 2021-12-17 2022-10-19
Button with automatic height looks like fill parent on iOS 0 2022-10-17 2022-10-12
[Feature request] a node command to swap children 0 2021-06-27 2022-11-22
--skip-integrity is broken after new year commits 4 2022-01-03 2022-11-28
Custom Mappings for Which Key Popup? 0 2021-08-27 2022-11-26
Add an ability to switch to full screen video call by double clicking on the top of the window 0 2022-11-16 2022-11-16
Getting 502 Bad Gateway error in conda install 2 2022-06-15 2022-11-26
Realtime generates duplicate events on multiple computers when one is signed in and other is not. 1 2022-02-24 2022-11-08
Consider allowing boolean tests of arbitrary values to fail 1 2022-02-10 2022-11-22
Changing the resolution 1 2021-09-03 2022-10-11
wordpress_initialize is not atomic 3 2022-02-02 2022-10-22
Add ability to customize the pod security context and use readOnlyRootFilesystem 5 2022-05-03 2022-10-24
Permission problems with SSL private key 1 2022-07-19 2022-11-29
failed to write request: The pipe is being closed. (os error 232) 6 2021-11-23 2022-08-17 0 2021-09-28 2021-10-10
Serial port performs a reset to attached UNO on startup and shutdown of IDE 2.0.0 4 2022-10-07 2022-11-06
AppImage shows default icon on Debian 11 / Gnome 3.38.5 2 2021-11-13 2022-11-06
Merge guidance view sub-type components into a single one 4 2022-08-31 2022-11-20
iOS Simulator FaceID never popups 9 2022-04-23 2022-11-14
Error during --etpmode 0 2022-03-10 2022-12-01
Expired API token for R check 2 2022-01-01 2022-11-27
Coredump during Scylla Manager backup 14 2021-08-11 2022-11-21
Sharing: Access requests 0 2020-10-05 2022-10-05
pfring_get_interface_speed support on Silicom FPGA (Fiberblaze) 1 2021-03-16 2022-12-01
[Bug]: matplotlib.pyplot.annotate with bbox: text is not centered vertically in svg 1 2022-05-07 2022-11-17
TUN/TAP interfaces support 1 2020-11-23 2022-11-22
Cargo install panics on some non-ASCII input 0 2022-08-15 2022-12-10
Tool pylint failed to run because of django.db.models.DecimalField 4 2019-05-04 2022-12-09
Support hiding buffer encoding when at default 1 2021-04-27 2022-08-07
TypeError at /api/cases/counterfactual/daily/absolute/ 4 2021-04-06 2022-10-23
Invalid read syntax ")" 1 2021-07-26 2022-10-27
Unsafe Jquery Plugin - Potential False Negative 7 2021-12-28 2022-10-09
Manual test run on macOS (arm64) for 1.34.x - Release #2 1 2022-01-20 2022-10-31