-
Notifications
You must be signed in to change notification settings - Fork 54
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
Race condition in case of multiple event loops #611
Comments
Note that even if the individual dict operations were protected by the GIL, there are read-modify-write sequences in the code that IIUC are not: async-lru/async_lru/__init__.py Lines 199 to 214 in dc601e2
|
Not sure if multiple loops is something we really want to care about, seems like a bad idea overall. But, could maybe prefix the cache per loop or something? |
Having multiple loops seems more-or-less inevitable if someone is trying to incrementally adopt I would be reluctant to use a per-loop cache: it would depress the hit rate, and I'm not sure how you'd safely evict a loop from the cache to prevent a memory leak if it terminates. I suppose you could use a |
I believe we're running into this problem or something related after having recently adopted We use a ProcessPoolExecutor to spin up processes which each have their own event loop. The event loop gets created and destroyed multiple times throughout the lifecycle of the process. We're seeing an issue where the cache result is sometimes empty for existing processes:
|
Well, feel free to try the solution I mentioned and open a PR. |
It appears that the
alru_cache
decorator is using an unsynchronizedOrderedDict
instance for its cache:async-lru/async_lru/__init__.py
Line 103 in dc601e2
If I am not mistaken, that will lead to a data race if the decorated function is called simultaneously from different
asyncio
event loops running on different threads — which seems to be a realistic (if rare) situation (compare python/cpython#93462 (comment)).If I understand correctly, other
asyncio
wrappers generally bind the event loop during initialization and fail explicitly if called from a different event loop. Unfortunately, since thealru_cache
decorator generally runs at module init time there is no such loop on the thread.I don't have a clear idea of how this problem could be fixed; I just wanted to point it out as a (significant and subtle) risk for users of this module.
The text was updated successfully, but these errors were encountered: