-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
Description
Description
Since version 5.0, setting SHOW_TOOLBAR_CALLBACK to a function that uses the ORM to access the database raises the following exception:
SynchronousOnlyOperation
You cannot call this from an async context - use a thread or sync_to_async.
An example function that triggers this issue:
def show_toolbar(request):
return request.user.is_staffAs 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 theshow_toolbar(request)call. - The
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:
- In
DebugToolbarMiddleware.__call__method, cast the callback usingasync_to_syncif the provided function is asynchronous. - In
DebugToolbarMiddleware.__acall__method, cast the callback usingsync_to_asyncif the provided function is synchronous, and add theawaitto its execution.
tyilo