@@ -785,12 +785,24 @@ def _is_contextvars_broken():
785
785
Returns whether gevent/eventlet have patched the stdlib in a way where thread locals are now more "correct" than contextvars.
786
786
"""
787
787
try :
788
+ import gevent # type: ignore
788
789
from gevent .monkey import is_object_patched # type: ignore
789
790
791
+ # Get the MAJOR and MINOR version numbers of Gevent
792
+ version_tuple = tuple ([int (part ) for part in gevent .__version__ .split ("." )[:2 ]])
790
793
if is_object_patched ("threading" , "local" ):
791
- # Gevent 20.5 is able to patch both thread locals and contextvars,
792
- # in that case all is good.
793
- if is_object_patched ("contextvars" , "ContextVar" ):
794
+ # Gevent 20.9.0 depends on Greenlet 0.4.17 which natively handles switching
795
+ # context vars when greenlets are switched, so, Gevent 20.9.0+ is all fine.
796
+ # Ref: https://github.com/gevent/gevent/blob/83c9e2ae5b0834b8f84233760aabe82c3ba065b4/src/gevent/monkey.py#L604-L609
797
+ # Gevent 20.5, that doesn't depend on Greenlet 0.4.17 with native support
798
+ # for contextvars, is able to patch both thread locals and contextvars, in
799
+ # that case, check if contextvars are effectively patched.
800
+ if (
801
+ # Gevent 20.9.0+
802
+ (sys .version_info >= (3 , 7 ) and version_tuple >= (20 , 9 ))
803
+ # Gevent 20.5.0+ or Python < 3.7
804
+ or (is_object_patched ("contextvars" , "ContextVar" ))
805
+ ):
794
806
return False
795
807
796
808
return True
0 commit comments