opentelemetry-python-contrib
opentelemetry-python-contrib copied to clipboard
Prevent RuntimeError for Flask on context.detach with the same token
Description
This happens for me when using werkzeug's debug=True. In this case, the token was already detached. But when I enter an expression into the debug console, we attempt to detach the token once more.
This causes a RuntimeError, as expected: https://peps.python.org/pep-0567/#contextvars-contextvar.
But if we set _ENVIRON_TOKEN to None after we detach, this won't happen.
Type of change
Please delete options that are not relevant.
- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
- Create a simple Flask app with instrumented spans
- Raise an error in one of the routes and open the debugger web page
- Click the shell icon and enter the debugger PIN
- Try to examine any variables in the context
- doing so will produce a RuntimeError in the logs, since the token was already used to detach the context on the original request
Does This PR Require a Core Repo Change?
- [ ] Yes. - Link to PR:
- [X] No.
The committers listed above are authorized under a signed CLA.
- :white_check_mark: login: abo-abo / name: Oleh Krehel (5a2fc16c2d764db53b1ec294f5244cc6ac866d55)
- :white_check_mark: login: emdneto / name: Emídio Neto (8ab1064a31764fcb88ace0f857589846155b8c3f)
We need the CLA signed in order to get things accepted.
Just signed the CLA.
Friendly ping, can we merge this?
@abo-abo would it be possible to write a test for asserting that the fix work?