Skip to content

Comments

ensure context cleanup runs even if teardown raises#5927

Closed
bysiber wants to merge 1 commit intopallets:mainfrom
bysiber:fix/ensure-context-cleanup-on-teardown-error
Closed

ensure context cleanup runs even if teardown raises#5927
bysiber wants to merge 1 commit intopallets:mainfrom
bysiber:fix/ensure-context-cleanup-on-teardown-error

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

In AppContext.pop(), if do_teardown_appcontext raises an exception, _cv_app.reset() and the context token cleanup are skipped. This leaves the context variable in a corrupted state, which can cause subsequent operations to see stale context data.

Similarly, if do_teardown_request raises, _request.close() is not called, potentially leaking file handles from the request body.

This adds nested try/finally blocks to ensure:

  • _request.close() runs even if do_teardown_request raises
  • _cv_app.reset() and token cleanup run even if do_teardown_appcontext raises
  • The appcontext_popped signal is always sent

While teardown functions should avoid raising exceptions per the documentation, the framework-level cleanup should still be guaranteed regardless.

@davidism davidism closed this Feb 20, 2026
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.

2 participants