Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix detection of contextvars compatibility with Gevent 20.9.0+ #1157

Merged
merged 4 commits into from
Jul 27, 2021
Merged

🐛 Fix detection of contextvars compatibility with Gevent 20.9.0+ #1157

merged 4 commits into from
Jul 27, 2021

Conversation

tiangolo
Copy link
Contributor

🐛 Fix detection of contextvars compatibility with Gevent 20.9.0+

This is described in #1063, and this PR would fix it (it should at least).

Description

The current implementation checks if Gevent patched contextvars, but:

Gevent 20.9.0 depends on Greenlet 0.4.17 which natively handles switching context vars when greenlets are switched.
Older versions of Python [older than 3.7] that have the backport installed will still be patched.

Ref: https://github.com/gevent/gevent/blob/83c9e2ae5b0834b8f84233760aabe82c3ba065b4/src/gevent/monkey.py#L604-L609

This PR updates the implementation to check the version of Gevent and Python to account for that.

Use Case

I'm migrating a code base with a couple of apps from Flask to FastAPI. During the migration both Flask and FastAPI are available at the same time, sharing a lot of the code and tests.

The Flask app is currently deployed with Gevent, so the tests use Gevent's monkey patching.

The FastAPI app uses the SentryAsgiMiddleware, which internally uses contextvars to store and handle all the Sentry stuff per request. And that in turn uses this utility function to check if the current environment has working contextvars available.

So, with Python 3.7+ and a recent installation of Gevent monkey patching stuff, Sentry errors out complaining about broken contextvars, but it seems it's actually just a false positive.

Copy link
Member

@untitaker untitaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I don't think this can have tests unless you want to play around with the version matrix in tox, barely worth it

@tiangolo
Copy link
Contributor Author

Thanks for the reviews @untitaker and @ahmedetefy! 🚀

Dang it, sorry for the linter warnings, I just ran them locally and fixed them (hopefully 🤞).

I agree that all the extra complexity for testing different versions of Gevent with different versions of Python doesn't seem worth it as it's probably more of an edge case.

Nevertheless, let me know if they are needed and I can give that a try.

@ahmedetefy ahmedetefy merged commit e8d4587 into getsentry:master Jul 27, 2021
@ahmedetefy
Copy link
Contributor

Thanks @tiangolo !

@tiangolo tiangolo deleted the detect-contextvars-new-gevent branch July 27, 2021 09:34
@tiangolo
Copy link
Contributor Author

Awesome! Thanks for the quick response/review and merge! 🚀

Do you have any estimate of when this could be included in a release?

@ahmedetefy
Copy link
Contributor

Anytime! Yep will cut a patch today

@tiangolo
Copy link
Contributor Author

Woohoo! Awesome, thanks a lot! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants