-
Notifications
You must be signed in to change notification settings - Fork 0
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
Make minject
thread safe
#8
Comments
Seems like potential race conditions can occur when we are concurrently registering objects. There are mainly two ways of registering objects in
Therefore, we should (1) lock access to shared data structures and (2) synchronize the lazy initialization procedure. An Since reads are typically much more frequent than writes—reading may occur frequently throughout execution and writing usually occurs during the application startup or occasionally for lazy registrations—we may also want to make the lock a RWLock to allow a number of readers or at most one writer. This will ensure that readers are not blocked by each other. TODO:
|
Hi @xiyan128! This looks great. I think you are correct in needing to add a lock to Could you help me understand what you mean by "lock access to shared data structures?" I think it makes sense to lock the Thanks for taking this task! |
@biniona by locking shared data structures I mean locking the
Reading the registry should always be fine as long as there's no concurrent writer! However, we do want to prevent a reader from reading the registry while there are concurrent writers. My plan is to solve this using a RWLock, so that readers don't block each other. |
For now, focusing on making lazy initialization thread safe: Added a test case to show that sometimes different instances of the singleton class might be returned. minject/tests/test_registry.py Lines 465 to 483 in 892e0d5
How to run this test?To trigger the race condition more reliably, turn on this flag Line 17 in 892e0d5
Run
Now, turn on the flag for the fix Line 18 in 892e0d5
All tests should reliably pass now. |
Re #20 : Let's not relax locking logic for startup initialization. |
Sounds good. I'll put together a PR that includes test cases for both startup initialization and lazy initialization thread safety. |
Currently, if you try to instantiate two objects with
minject
in different threads, this can lead to undefined behavior.This issue captures the task of locking
minject
(possibly with a reentrant lock) during object instantiation.The text was updated successfully, but these errors were encountered: