You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As request.user is a lazy object, it'll be retrieved during the show_toolbar execution. As some panels are asynchronous now, Django Debug Toolbar will try to access to the database from an asynchronous context.
Converting the callback to asynchronous does not work either, because:
The DebugToolbarMiddleware.__acall__ method does not await for the show_toolbar(request) call.
The DebugToolbarMiddleware.__call__ method calls show_toolbar(request), which will always be true, as Python will return a coroutine object.
Possible solution
Allow both sync and async SHOW_TOOLBAR_CALLBACK functions, and:
In DebugToolbarMiddleware.__call__ method, cast the callback using async_to_sync if the provided function is asynchronous.
In DebugToolbarMiddleware.__acall__ method, cast the callback using sync_to_async if the provided function is synchronous, and add the await to its execution.
The text was updated successfully, but these errors were encountered:
Yes, I can confirm it's not reproducible on my end when running runserver with a WSGI application, but it is reproducible when using Django's get_asgi_application(), even without ASGI_APPLICATION setting explicitly set.
Description
Since version 5.0, setting
SHOW_TOOLBAR_CALLBACK
to a function that uses the ORM to access the database raises the following exception:An example function that triggers this issue:
As
request.user
is a lazy object, it'll be retrieved during theshow_toolbar
execution. As some panels are asynchronous now, Django Debug Toolbar will try to access to the database from an asynchronous context.Converting the callback to asynchronous does not work either, because:
DebugToolbarMiddleware.__acall__
method does not await for theshow_toolbar(request)
call.DebugToolbarMiddleware.__call__
method callsshow_toolbar(request)
, which will always be true, as Python will return a coroutine object.Possible solution
Allow both sync and async
SHOW_TOOLBAR_CALLBACK
functions, and:DebugToolbarMiddleware.__call__
method, cast the callback usingasync_to_sync
if the provided function is asynchronous.DebugToolbarMiddleware.__acall__
method, cast the callback usingsync_to_async
if the provided function is synchronous, and add theawait
to its execution.The text was updated successfully, but these errors were encountered: