Skip to content

pass context internally instead of using contextvars #5815

Closed

Description

@davidism

Currently, there are a bunch of different methods on the Flask class that run to dispatch each request. Many of these access request and other context proxies. We should update them to pass the AppContext everywhere instead. This is more convenient after #5812 with only one context object instead of two. As @pgjones pointed out in #5229, not having to access the contextvar is a significant speedup to ASGI Quart, although it doesn't appear to affect WSGI Flask as much. Perhaps if we were serving with greenlets and so contexts were switching more, it would be more noticeable in Flask too.

The obvious problem is that it is a breaking change to the signatures of all these methods. I'm unsure how many methods are affected, but I'm also unsure how many projects are even subclassing Flask to override any of the methods. The fact that the methods are public seems unintentional, compared to the much more common ways of configuring and customizing an app.

We could continue to support both signatures, showing a deprecation warning for the old one, by adding some detection to Flask.__init__. This would add some amount of time during app setup, but wouldn't affect runtime performance of the app.


Another idea I had for the future was to have @route and other decorators inspect the signature of the decorated function, to allow injecting request, app, etc. if users would rather use that pattern than import the proxies. Having the request object directly available in all the dispatch methods would make this more straightforward. User code would be able to choose what pattern they want, the current proxy pattern would never be deprecated for user code.

Activity

  1. self-assigned this
    on Sep 13, 2025
  2. ThiefMaster commented on Sep 13, 2025

    @ThiefMaster
    Member

    Not a fan of deprecating it, for 99% of all apps the performance difference is unnecessary so the convenience wins IMHO.

    I think nobody wants to update their application to a change like this, also because it would imply having to propagate the vars deeper into application code, util functions, etc. that currently use the profies.

  3. davidism commented on Sep 13, 2025

    @davidism
    MemberAuthor

    This would only be for methods on Flask (Scaffold, etc), it would not require passing them throughout your own code. #5229 gives an idea of the scope of that.

    I've been playing around with the injector a bit, it could be used to re-add the arguments from the proxies further down the chain if some code wanted to use that pattern but other didn't.

    The deprecation can be unversioned for a bit, just to see if anyone brings it up. If we do this internally, I don't want to support signature inspection and wrappers forever.

  4. ThiefMaster commented on Sep 13, 2025

    @ThiefMaster
    Member

    ah, internals only. not such a big deal then, as it would just affect people subclassing...

  5. davidism commented on Sep 18, 2025

    @davidism
    MemberAuthor

    It may be very difficult to do this in a way that existing overridden code will not fail and will show a deprecation warning.

    We want to upgrade an overridden method that does not take the new ctx: AppContext parameter. We can detect this in __init_subclass__ with inspect.signature, and wrap the function to take the correct signature, so that other parts of the dispatch code will call it correctly. But that's the "simple" part.

    What if other user code is still calling the method, overridden or not, without the new argument? The most obvious way this happens is with super().method within the overridden method. The wrapper could perhaps be clever enough to check the type of the first argument, and decide to make a new or old signature call. The wrapper would have to be applied to every base method to handle the super call case.

    I haven't tried to write this more complex wrapper yet. I have a feeling it will be difficult to write. I know it will add overhead to every request, since multiple methods will now have to do detection on the arguments passed to them. This overhead will mostly be waste, as the vast majority of code will not be overridden.

    Perhaps we can just say this is a breaking change in 4.0? I still think the result is worth it. We could still show a better error message by doing some detection in __init_subclass__, but showing an error instead of applying a wrapper and showing a warning.

  6. davidism commented on Sep 18, 2025

    @davidism
    MemberAuthor

    The following method signatures would change to add ctx: AppContext as the first parameter:

    • update_template_context
    • handle_http_exception
    • handle_user_exception
    • handle_exception
    • log_exception
    • dispatch_request
    • full_dispatch_request
    • finalize_request
    • make_default_options_response
    • preprocess_request
    • process_response
    • do_teardown_request
    • do_teardown_appcontext

    I'd be somewhat surprised to find any of these overridden. They're either really internal, or they're the machinery that calls registered callback functions.

  7. davidism commented on Sep 19, 2025

    @davidism
    MemberAuthor

    I've managed to write some code that can detect overridden methods with the old signature, and wrap them and the corresponding base method so they'll continue to work during the deprecation period.

  8. added this to the 3.2.0 milestone on Nov 17, 2025
  9. locked as resolved and limited conversation to collaborators on Dec 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions