-
Notifications
You must be signed in to change notification settings - Fork 308
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
avoid creating asyncio.Lock at import time #935
Conversation
asyncio.Lock references the current event loop. There usually isn't one at import time. Instead, create lock and attach it to the Handler class upon initializing the first handler of the class
for exporter_name in exporters: | ||
try: | ||
async with LOCK: | ||
async with self._exporter_lock: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved acquisition of the lock outside the loop so we aren't acquiring and re-acquiring the same lock for each exporter.
Codecov Report
@@ Coverage Diff @@
## main #935 +/- ##
==========================================
- Coverage 72.26% 72.20% -0.06%
==========================================
Files 65 65
Lines 7997 8002 +5
Branches 1335 1333 -2
==========================================
- Hits 5779 5778 -1
- Misses 1811 1814 +3
- Partials 407 410 +3
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
@meeseeksdev please backport to 1.x |
Co-authored-by: Min RK <[email protected]>
asyncio.Lock references the current event loop, but there usually isn't one at import time. The result is that importing this handler always instantiates a current loop and attaches a lock to it, which may or may not be the loop that ultimately runs (app.io_loop).
Instead, create lock and attach it to the Handler class upon initializing the first handler of the class, which is always run inside the loop.
Also related to #876