[FIX] profiling: avoid crash with thread pools

When using a profiler manually inside the code, and longpolling uses
this part of the code, an error will appear on runbot since gevent
server cannot be profiled properly.
This fix mitigate the issue by disabling the profiler automatically in
this case.

Part-of: odoo/odoo#78514
This commit is contained in:
Xavier-Do
2021-10-22 17:34:17 +00:00
parent 0e2b2c9c8a
commit 4f3b1653de
+15 -2
View File
@@ -501,8 +501,21 @@ class Profiler:
def __enter__(self):
self.init_thread = threading.current_thread()
self.init_frame = get_current_frame(self.init_thread)
self.init_stack_trace = _get_stack_trace(self.init_frame)
try:
self.init_frame = get_current_frame(self.init_thread)
self.init_stack_trace = _get_stack_trace(self.init_frame)
except KeyError:
# when using thread pools (gevent) the thread won't exist in the current_frames
# this case is managed by http.py but will still fail when adding a profiler
# inside a piece of code that may be called by a longpolling route.
# in this case, avoid crashing the caller and disable all collectors
self.init_frame = self.init_stack_trace = self.collectors = []
self.db = self.params = None
message = "Cannot start profiler, thread not found. Is the thread part of a thread pool?"
if not self.description:
self.description = message
_logger.warning(message)
if self.description is None:
frame = self.init_frame
code = frame.f_code