From 4f3b1653de74989f0954397eaea55b5ac2c23e86 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Tue, 19 Oct 2021 06:40:49 +0000 Subject: [PATCH] [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 --- odoo/tools/profiler.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/odoo/tools/profiler.py b/odoo/tools/profiler.py index 1ed51804533..838e813e634 100644 --- a/odoo/tools/profiler.py +++ b/odoo/tools/profiler.py @@ -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