oremanj / greenback

Reenter an asyncio or Trio event loop from synchronous code
https://greenback.readthedocs.io/
Other
80 stars 2 forks source link

Work around greenlet 0.4.17's misguided contextvars support #5

Closed oremanj closed 4 years ago

oremanj commented 4 years ago

greenlet 0.4.17 added support for switching contextvars when switching greenlets. Great! Except... each greenlet now starts with an empty context, and trying to work around this with Context.run() tends to result in problems not keeping the implicit stack of Context.run() calls properly nested (since if you switch away from a greenlet, any Context.run() it was in the middle of still appears to be active). See https://github.com/python-greenlet/greenlet/issues/196 for more details.

Work around this by using ctypes to change the new greenlet's context pointer to be the same as the old one before switching. This probably comes with some performance cost, but I don't see a better alternative.

codecov[bot] commented 4 years ago

Codecov Report

Merging #5 into master will increase coverage by 0.40%. The diff coverage is 98.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #5      +/-   ##
==========================================
+ Coverage   99.42%   99.82%   +0.40%     
==========================================
  Files           7        7              
  Lines         518      585      +67     
  Branches       37       42       +5     
==========================================
+ Hits          515      584      +69     
+ Misses          2        1       -1     
+ Partials        1        0       -1     
Flag Coverage Δ
#cpython 99.14% <98.71%> (+0.49%) :arrow_up:
#pypy36nightly 89.23% <45.45%> (-4.49%) :arrow_down:
#pypy36release 89.23% <45.45%> (-4.49%) :arrow_down:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
greenback/_impl.py 99.40% <97.95%> (+1.71%) :arrow_up:
greenback/_tests/test_impl.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 00f885f...5626f9f. Read the comment docs.