Skip to content

Commit d5bdcc3

Browse files
committed
remove used_static_files contextvar and its dependencies allow on app ready monkey patching
1 parent 4726e0f commit d5bdcc3

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

debug_toolbar/panels/staticfiles.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def url(self):
3030
return storage.staticfiles_storage.url(self.path)
3131

3232

33-
# This will collect the StaticFile instances across threads.
34-
used_static_files = ContextVar("djdt_static_used_static_files", default=[])
33+
# This will record and map the StaticFile instances with its associated
34+
# request across threads and async concurrent requests state.
3535
request_id_context_var = ContextVar("djdt_request_id_store")
3636
record_static_file_signal = Signal()
3737

@@ -99,23 +99,26 @@ def __init__(self, *args, **kwargs):
9999
self.used_paths = []
100100
self.request_id = str(uuid.uuid4())
101101

102+
@classmethod
103+
def ready(cls):
104+
storage.staticfiles_storage = DebugConfiguredStorage()
105+
102106
def _store_static_files_signal_handler(self, sender, staticfile, **kwargs):
103-
with contextlib.suppress(LookupError):
104-
# Only record the static file if the request_id matches the one
105-
# that was used to create the panel.
106-
# as sender of the signal and this handler will have multiple
107-
# concurrent connections and we want to avoid storing of same
108-
# staticfile from other connections as well.
109-
if request_id_context_var.get() == self.request_id:
110-
used_static_files.get().append(staticfile)
107+
# Only record the static file if the request_id matches the one
108+
# that was used to create the panel.
109+
# as sender of the signal and this handler will have multiple
110+
# concurrent connections and we want to avoid storing of same
111+
# staticfile from other connections as well.
112+
if request_id_context_var.get() == self.request_id:
113+
self.used_paths.append(staticfile)
111114

112115
def enable_instrumentation(self):
113-
storage.staticfiles_storage = DebugConfiguredStorage()
114116
record_static_file_signal.connect(self._store_static_files_signal_handler)
115-
request_id_context_var.set(self.request_id)
117+
self.ctx_token = request_id_context_var.set(self.request_id)
116118

117119
def disable_instrumentation(self):
118120
record_static_file_signal.disconnect(self._store_static_files_signal_handler)
121+
request_id_context_var.reset(self.ctx_token)
119122

120123
@property
121124
def num_used(self):
@@ -131,20 +134,7 @@ def nav_subtitle(self):
131134
"%(num_used)s file used", "%(num_used)s files used", num_used
132135
) % {"num_used": num_used}
133136

134-
# def process_request(self, request):
135-
# reset_token = used_static_files.set([])
136-
# response = super().process_request(request)
137-
# # Make a copy of the used paths so that when the
138-
# # ContextVar is reset, our panel still has the data.
139-
# self.used_paths = used_static_files.get().copy()
140-
# # Reset the ContextVar to be empty again, removing the reference
141-
# # to the list of used files.
142-
# used_static_files.reset(reset_token)
143-
# return response
144-
145137
def generate_stats(self, request, response):
146-
self.used_paths = used_static_files.get().copy()
147-
used_static_files.get().clear()
148138
self.record_stats(
149139
{
150140
"num_found": self.num_found,

0 commit comments

Comments
 (0)