Flask turns a WSGI environ into an application context, runs preprocessors, dispatches the matched view, catches user exceptions, and finalizes a response.
3dispatch phases
1request signal
1auto OPTIONS path
2finalization modes
01
1Enter
WSGI
Build Context
Flask starts at wsgi_app, creates a request context, and binds URL state before the view function is called.
entry contract
ENV
WSGI environ arrives
The Flask object is callable; __call__ delegates to wsgi_app with environ and start_response.
CTX
Context is pushed
wsgi_app creates the request context so current_app, request, and URL matching are available.
URL
Routing state is attached
The request carries url_rule, view_args, and possible routing_exception for dispatch_request.
entry contract
OK
Context first View dispatch happens only after Flask has request-local state.
i
WSGI remains replaceable The docs note wsgi_app can be wrapped by middleware.
02
2Dispatch
VIEW
Run The View
full_dispatch_request wraps preprocessing, view dispatch, and user exception handling into the central request path.
dispatch gates
1
request_started fires
Flask emits request_started before preprocessing begins.
SIG
2
preprocess_request may short-circuit
If a before-request function returns a value, Flask uses it instead of calling the matched view.
PRE
3
dispatch_request handles OPTIONS
Automatic OPTIONS responses can be returned before a normal endpoint function is invoked.
OPT
Views can be async-safe
ensure_sync wraps the selected view function before calling it.
Exceptions are user-facing
full_dispatch_request catches exceptions and routes them through handle_user_exception.
03
3Finalize
RESP
Return Response
Flask converts the view return value into a Response and runs postprocessing before the WSGI server receives bytes.
response result
MAKE01
Convert return value
finalize_request turns a view return value or HTTPException into a Response object.
POST02
Run response processors
After-request behavior is applied during finalization for both normal and error-handler paths.
POP03
Close the context
wsgi_app eventually pops the request context after response handling completes.
response result
Response payload
OK
One response path
Normal dispatch and error handlers both call finalize_request.
i
Errors can still be logged
Flask logs exceptions before default error handling when debugging is disabled.