Skip to content

Conversation

@davidism
Copy link
Member

@davidism davidism commented Jul 1, 2022

Uses the new mechanism introduced by pallets/werkzeug#2439 to preserve the app and request context. If the werkzeug.debug.preserve_context key is in the environ, it is a callable. It is called to record context managers that should be entered again after the request ends. Rather than keeping the contexts on the stack for longer, this allows preserving the contexts independently of the context-local stack and subsequent requests.

Because request, g, etc. are context-local, they would never be available in the debugger when using the default threaded server. Now they will point to the correct data in the debugger. The same mechanism is used by the test client.

The contexts are now always popped at the end of the request, even if they are preserved. This means teardown functions will run to clean up resources. The previous implementation would pop the preserved context from the stack on the next, but that would never happen when using the threaded server because each request would be in a new thread with its own stack. I have a feeling this was causing issues especially with Flask-SQLAlchemy, where connections would never be released for requests caught by the debugger.

The contexts are restored after the request finishes in the test client, or when code is executed in the debugger. This means that signals will run each time, and teardown functions will run each time.

Only the contexts directly preserved by werkzeug.debug.preserve_context are popped. This reverts #3157, which caused every instance of the same context to be popped. This means that the context preserved with stream_with_context will not be popped when a with client block ends, it will only be popped once response.data is read (or response.close() is called).

closes #2836

@davidism davidism added this to the 2.2.0 milestone Jul 1, 2022
@davidism davidism merged commit 6916697 into main Jul 1, 2022
@davidism davidism deleted the debug-context branch July 1, 2022 19:35
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

request points to wrong object in debugger when threading is enabled

2 participants