fix race condition in service plugin loading#7968
Merged
Conversation
whummer
approved these changes
Mar 25, 2023
localstack/services/plugins.py
Outdated
| return None | ||
|
|
||
| with self._mutex: | ||
| if plugin.service.name() not in self._services: |
Member
There was a problem hiding this comment.
Wondering if there can be a situation where this condition is not true, i.e., where an entry for this service name would already exist in self._services (since we're now locking on the service name in this critical section, and _load_service_plugin(..) in line 565 above is side-effect free).
The only case where this could happen is if plugin.service.name() returns a string that is different from name - can this be the case?
If that's something we're not expecting to happen, maybe we could proactively raise an error, to avoid such cases from going unnoticed:
with self._mutex:
service_name = plugin.service.name()
if service_name in self._services:
raise ServiceStateException(
"Service {service_name} unexpectedly already in list of loaded services")
)
super().add_service(plugin.service)
Member
Author
There was a problem hiding this comment.
good point, can't happen anymore. fixed!
091dcf8 to
b155b49
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a race condition in the service plugin loading, where multiple concurrent calls to
service_manager.require(...)could lead to Service plugins being instantiated more than once, and subsequentlyon_after_initbeing called multiple times, whose implementation often mutates global state.We haven't seen this manifest yet, other an exception in the logs when running
tests/integration/test_terraform.py::TestTerraform::test_bucket_exists:which was an obscure indicator that
on_after_initwas called twice.The PR introduces a locking mechanism that makes sure that, while the service is being loaded and instantiated, other calls to
requireof the same service block. TheSynchronizedDefaultDict(RLock)pattern is helpful here - and we make sure we delete the locks after using them (we only need them once).