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

ReactiveController: adding/removing controllers in lifecycle methods affects other controllers #4266

Closed
sorvell opened this issue Oct 6, 2023 · 1 comment · Fixed by #4388

Comments

@sorvell
Copy link
Member

sorvell commented Oct 6, 2023

Which package(s) are affected?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

If a ReactiveController removes itself or adds another controller during a lifecycle method like hostUpdate or hostUpdated, this can prevent other controllers from properly running.

Reproduction

The initial text of the button in this repro should be the following and then clicking the button should not change the text.

Expected:

controller1.count: 1
controller2.count: 1

Actual:

controller1.count: 1
controller2.count: 0

(after clicking once)

controller1.count: 1
controller2.count: 1

Workaround

The issue is that the controllers are stored in an Array and to process the controllers' lifecycle, that array is forEach'd. Mutating the array while it's iterating unexpectedly alters the iteration.

The only workaround is not to alter the controllers list inside a controller lifecycle method.

This could possibly be addressed by (1) copying the controllers array before iterating, or (2) making the controllers a Set.

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

All

Browser/OS/Node environment

Browser: All

@justinfagnani
Copy link
Collaborator

Ouch. Yeah, it's a concurrent modification bug. Set seems like the right way to go.

sorvell pushed a commit to sorvell/lit that referenced this issue Nov 10, 2023
Fixes lit#4266. Controllers are maintained via a Set
instead of an Arry, allowing them to be iterated
stably while being modified.
AndrewJakubowicz pushed a commit that referenced this issue Nov 13, 2023
…callbacks (#4388)

Fixes #4266. Controllers are maintained via a Set
instead of an Array, allowing them to be iterated
stably while being modified.

Co-authored-by: Augustine Kim <augustinekim@google.com>
This was referenced Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants