fix(utils): handle non-ImportError exceptions when checking eventlet contextvars - #7219
Open
agu2347 wants to merge 1 commit into
Open
fix(utils): handle non-ImportError exceptions when checking eventlet contextvars#7219agu2347 wants to merge 1 commit into
agu2347 wants to merge 1 commit into
Conversation
…contextvars _is_contextvars_broken() only caught ImportError when trying to import greenlet/eventlet to detect monkeypatching. Depending on which combination of eventlet, greenlet, and other monkeypatched modules (e.g. dnspython, httpcore) happen to be installed, importing these can raise other exceptions (e.g. AttributeError), crashing the SDK on import. Also clean up any partially-imported modules left behind in sys.modules when such an import fails, so a subsequent import of the same module doesn't silently reuse the broken cached module. Fixes getsentry#7202
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.
Problem
_is_contextvars_broken()insentry_sdk/utils.pyonly catchesImportErrorwhen checking whether eventlet has monkeypatched the stdlib:Depending on the exact combination of
eventlet,greenlet, and other monkeypatched packages installed (e.g.dnspython,httpcore), importinggreenlet/eventletcan raise something other thanImportError(e.g.AttributeError) while eventlet's monkeypatching machinery is initializing. Since_is_contextvars_broken()is called once at import time to setHAS_REAL_CONTEXTVARS, ContextVar = _get_contextvars(), an uncaught exception here crashes the SDK on import for anyone hitting one of these combinations.Additionally, when an import fails partway through, Python still leaves a (broken) module object registered in
sys.modules, so a laterimport eventlet/import greenletfrom application code would silently reuse the broken cached module instead of retrying cleanly.Closes #7202
Fix
except ImportErroraround the eventlet/greenlet import to also catchException, so an unexpected error there degrades gracefully (treated as "not broken") instead of crashing the SDK.sys.modulesduring the failed import attempt, so a subsequent import isn't stuck with a broken partial module.Testing
Added
test_is_contextvars_broken_survives_eventlet_attributeerror, which mocksbuiltins.__import__to simulate the exact crash reported in #7202 (anAttributeErrorraised while importinggreenlet, with a partial module left insys.modules), without needing the actual fragile eventlet+trio+httpcore+dnspython version combination installed.AttributeErrorwhen the fix is reverted (stashedsentry_sdk/utils.pyonly, test unchanged).tests/utils/test_contextvars.pypass with the fix applied.ruff format --checkandruff checkpass on both changed files.