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

Replace the entire observers list when adding/removing observers in o… #796

Merged
merged 1 commit into from
Sep 9, 2020

Conversation

kstenerud
Copy link
Contributor

Goal

Fixes https://bugsnag.atlassian.net/browse/PLAT-5007

Design

Instead of adding/removing observers from the observer list directly, make a copy of the list, modify the copy, then replace the original. This makes concurrency issues (where an observer gets added/removed while the observer list is being walked) impossible.

So long as the list remains small (100 or so entries), the CPU cost is negligible. We don't expect to ever see more than around 10 observers here no matter what future features we add.

Copy link
Contributor

@kattrali kattrali left a comment

Choose a reason for hiding this comment

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

Is there a test we can add here to demonstrate the problem in the previous implementation?

@kstenerud
Copy link
Contributor Author

kstenerud commented Sep 9, 2020

Added a test that deliberately accesses on two threads. This crashes the old implementation.

…rder to completely avoid concurrency issues (adding/removing an observer while observer notifications are happening).
Copy link
Contributor

@fractalwrench fractalwrench left a comment

Choose a reason for hiding this comment

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

LGTM

@tomlongridge tomlongridge merged commit f398608 into next Sep 9, 2020
@tomlongridge tomlongridge deleted the 5007-observer-thread-safety branch September 9, 2020 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants