Fix background callback context serialisation for non-dict request args#3817
Open
i-murray wants to merge 3 commits into
Open
Fix background callback context serialisation for non-dict request args#3817i-murray wants to merge 3 commits into
i-murray wants to merge 3 commits into
Conversation
Background callbacks dispatch the callback context to a worker (e.g. Celery via kombu), where the context must be JSON serialisable. The request ``args`` (query-string parameters) carried on the context can be a backend-specific multi-dict such as ``starlette.datastructures.QueryParams`` (FastAPI) or a ``werkzeug``/``quart`` ``MultiDict`` (Flask/Quart). These types are not JSON serialisable, so dispatching a background callback raised a serialisation error (e.g. "QueryParams is not JSON serializable"). Coerce ``args`` to a plain ``dict`` on the worker-bound copy of the context in ``_setup_background_callback``. The synchronous HTTP request path is left untouched, so code relying on ``args.getlist(...)`` continues to work.
Cover the coercion of a non-dict request args (e.g. a starlette QueryParams / multi-dict) to a JSON-serialisable plain dict on the worker-bound context copy in _setup_background_callback, and assert that existing dict / None values are left unchanged.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
Fixes a serialisation failure that breaks all
background=Truecallbacks on the FastAPI backend (and, for the same reason, Quart).When a background callback is dispatched, Dash copies the callback context and hands it to the background worker (Celery via kombu, or Diskcache), where it must be serialised. The request
argsstored on the context is a backend-specific multi-dictstarlette.datastructures.QueryParamson FastAPI, awerkzeug/quartMultiDicton Quart — which is not JSON serialisable, so dispatch fails with:Root cause
During request setup,
Dash._initialize_context(dash/dash.py) stores the request data on the context. Unlikecookiesandheaders, which are coerced to plain dicts,argsis stored as-is:This is intentional for the synchronous HTTP path, which relies on the multi-dict interface (e.g.
adapter.args.getlist("oldJob")andcallback_context.args.getlist("cancelJob")). The problem only appears when that object is carried into the background worker and serialised.Fix
_setup_background_callback(dash/_callback.py) builds a copy of the context for the worker. We coerceargsto a plaindictonly on that copy, leaving the live request context (and itsgetlistinterface) untouched:Because this mutates only the worker-bound copy:
adapter.args.getlist("oldJob")(in_get_callback_manager) is unaffected, it reads the request adapter directly.callback_context.args.getlist("cancelJob")(the synchronous HTTP cancel path) is unaffected, it reads the livecontext_value, not the copy.dictis the background worker, which does not usegetlistand otherwise could not receive the object at all.Closes #3816
Contributor Checklist
argsto a plaindicton the worker-bound context copy in_setup_background_callbackoptionals
CHANGELOG.md