Skip to content

Commit

Permalink
πŸ› Fix detection of contextvars compatibility with Gevent 20.9.0+ (#1157)
Browse files Browse the repository at this point in the history
* πŸ› Fix detection of contextvars compatibility with Gevent 20.9.0+

* πŸ› Improve implementation of version detection and account for Python versions

* πŸ”₯ Remove duplicated sys import

* 🚨 Fix linter warnings
  • Loading branch information
tiangolo authored Jul 27, 2021
1 parent 06f0265 commit e8d4587
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,24 @@ def _is_contextvars_broken():
Returns whether gevent/eventlet have patched the stdlib in a way where thread locals are now more "correct" than contextvars.
"""
try:
import gevent # type: ignore
from gevent.monkey import is_object_patched # type: ignore

# Get the MAJOR and MINOR version numbers of Gevent
version_tuple = tuple([int(part) for part in gevent.__version__.split(".")[:2]])
if is_object_patched("threading", "local"):
# Gevent 20.5 is able to patch both thread locals and contextvars,
# in that case all is good.
if is_object_patched("contextvars", "ContextVar"):
# Gevent 20.9.0 depends on Greenlet 0.4.17 which natively handles switching
# context vars when greenlets are switched, so, Gevent 20.9.0+ is all fine.
# Ref: https://github.com/gevent/gevent/blob/83c9e2ae5b0834b8f84233760aabe82c3ba065b4/src/gevent/monkey.py#L604-L609
# Gevent 20.5, that doesn't depend on Greenlet 0.4.17 with native support
# for contextvars, is able to patch both thread locals and contextvars, in
# that case, check if contextvars are effectively patched.
if (
# Gevent 20.9.0+
(sys.version_info >= (3, 7) and version_tuple >= (20, 9))
# Gevent 20.5.0+ or Python < 3.7
or (is_object_patched("contextvars", "ContextVar"))
):
return False

return True
Expand Down

0 comments on commit e8d4587

Please sign in to comment.