Open
Conversation
P403n1x87
reviewed
Apr 10, 2026
|
|
||
| _global_loop = None | ||
| atexit.register(partial(_cleanup, _global_loop)) | ||
| atexit.register(lambda: _cleanup(_global_loop)) |
There was a problem hiding this comment.
If lambdas are not of taste
Suggested change
| atexit.register(lambda: _cleanup(_global_loop)) | |
| @atexit.register | |
| def _(): | |
| global _global_loop | |
| _cleanup(_global_loop) |
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.
What is this PR?
This PR attempts to fix a bug we are seeing in our services and CI and that we have identified as coming from the Python Cassandra driver (
cassandra-python-driveris the only Python package in our environments that haslibevas a dependency).Stack traces typically look like the following, and crashes happen around exit.
This looks a lot like a problem we have identified in our own https://github.com/DataDog/dd-trace-py repository, where we need to make sure the interpreter hasn't finalised before trying to acquire the GIL -- otherwise it's not legal to do so and it typically just crashes.
However I think in the case of this code, this may actually be caused by another issue: the current
atexithook is invalid.libevreactor.pydoes this:This "generates" the
atexithook function when it is called so with the current_global_loopvalue as opposed to evaluating_global_loopat exit.As an illustration, take this:
Running this prints
Noneand notsomething.The right way to do this (if we want to keep it at the top level) would be to use a lambda:
... which does print
somethingat exit.My proposed fix is thus in two parts:
atexithook registration frompartialto alambda_cleanupasks the thread to stop so it shouldn't keep on trying to execute CPython code -- the fact thatjoinis called with a timeout of 1 second means it can fail, in which case the existing code is not safe (this is why we need the same mechanism inddtrace).Let me know if that makes sense!