Skip to content

Commit af7d15f

Browse files
If wsgi.multiprocess isn't set, render panels on each request.
* Avoid an exception with Django-channels If you apply Django middlewares on a HttpRequest without "wsgi.multiprocess" in META, an exception is raised by DJT. This simple patch avoids this bug. * If wsgi.multiprocess isn't set, render panels on each request. The likely cause of this is that the application is using ASGI since wsgi.multiprocess is a required key for a WSGI application. Since the toolbar currently doesn't support async applications, it's pretty likely that it won't work for this request. If you're a developer reading this and you think this is wrong, you can set the RENDER_PANELS setting to forcibly control this setting. --------- Co-authored-by: tschilling <[email protected]>
1 parent 1960ca3 commit af7d15f

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

debug_toolbar/toolbar.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,13 @@ def should_render_panels(self):
9595
9696
If False, the panels will be loaded via Ajax.
9797
"""
98-
render_panels = self.config["RENDER_PANELS"]
99-
if render_panels is None:
100-
render_panels = self.request.META["wsgi.multiprocess"]
98+
if (render_panels := self.config["RENDER_PANELS"]) is None:
99+
# If wsgi.multiprocess isn't in the headers, then it's likely
100+
# being served by ASGI. This type of set up is most likely
101+
# incompatible with the toolbar until
102+
# https://github.com/jazzband/django-debug-toolbar/issues/1430
103+
# is resolved.
104+
render_panels = self.request.META.get("wsgi.multiprocess", True)
101105
return render_panels
102106

103107
# Handle storing toolbars in memory and fetching them later on

docs/changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Pending
1414
presence of other code which also monkey patches those methods.
1515
* Update all timing code that used :py:func:`time.time()` to use
1616
:py:func:`time.perf_counter()` instead.
17+
* Made the check on ``request.META["wsgi.multiprocess"]`` optional, but
18+
defaults to forcing the toolbar to render the panels on each request. This
19+
is because it's likely an ASGI application that's serving the responses
20+
and that's more likely to be an incompatible setup. If you find that this
21+
is incorrect for you in particular, you can use the ``RENDER_PANELS``
22+
setting to forcibly control this logic.
1723

1824
4.0.0 (2023-04-03)
1925
------------------

tests/test_integration.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ def test_show_toolbar_INTERNAL_IPS(self):
6565
with self.settings(INTERNAL_IPS=[]):
6666
self.assertFalse(show_toolbar(self.request))
6767

68+
def test_should_render_panels_RENDER_PANELS(self):
69+
"""
70+
The toolbar should force rendering panels on each request
71+
based on the RENDER_PANELS setting.
72+
"""
73+
toolbar = DebugToolbar(self.request, self.get_response)
74+
self.assertFalse(toolbar.should_render_panels())
75+
toolbar.config["RENDER_PANELS"] = True
76+
self.assertTrue(toolbar.should_render_panels())
77+
toolbar.config["RENDER_PANELS"] = None
78+
self.assertTrue(toolbar.should_render_panels())
79+
80+
def test_should_render_panels_multiprocess(self):
81+
"""
82+
The toolbar should render the panels on each request when wsgi.multiprocess
83+
is True or missing.
84+
"""
85+
request = rf.get("/")
86+
request.META["wsgi.multiprocess"] = True
87+
toolbar = DebugToolbar(request, self.get_response)
88+
toolbar.config["RENDER_PANELS"] = None
89+
self.assertTrue(toolbar.should_render_panels())
90+
91+
request.META["wsgi.multiprocess"] = False
92+
self.assertFalse(toolbar.should_render_panels())
93+
94+
request.META.pop("wsgi.multiprocess")
95+
self.assertTrue(toolbar.should_render_panels())
96+
6897
def _resolve_stats(self, path):
6998
# takes stats from Request panel
7099
self.request.path = path

0 commit comments

Comments
 (0)