pallets-eco / flask-debugtoolbar

A toolbar overlay for debugging Flask applications
https://flask-debugtoolbar.readthedocs.io
BSD 3-Clause "New" or "Revised" License
952 stars 146 forks source link

Support applications using flask.copy_current_request_context #231

Closed glic3rinu closed 10 months ago

glic3rinu commented 10 months ago

Toolbar doesn't currently work if an application calls flask.copy_current_request_context at any point during request handling.

Note that this decorator is typically used when an application uses threads that need access to request context.

flask.copy_current_request_context calls teardown_request on exit, resulting on real_request being pop from debug_toolbars dict ahead of time, and not being present when debug toolbar processes the response. Ultimately resulting on the toolbar not being displayed.

This PR solves this problem by allocation debug_toolbars dict inside a contextvar, so threads/coroutines will leave toolbar state from the main/parent request alone.