From 932ea72361d09049672cf88c66cb32b12c20d7ee Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Fri, 27 Sep 2024 13:21:54 +0200 Subject: [PATCH 1/4] Wait for events in the queue --- backtracepython/client.py | 9 ++++++++- backtracepython/report_queue.py | 14 +++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/backtracepython/client.py b/backtracepython/client.py index 7d1b587..5c2145b 100644 --- a/backtracepython/client.py +++ b/backtracepython/client.py @@ -1,3 +1,4 @@ +import atexit import sys from backtracepython.attributes.attribute_manager import attribute_manager @@ -89,6 +90,7 @@ def initialize( context_line_count=200, collect_source_code=True, disable_global_handler=False, + exit_timeout=5, ): globs.endpoint = construct_submission_url(endpoint, token) globs.debug_backtrace = debug_backtrace @@ -102,6 +104,7 @@ def initialize( ignore_ssl_certificate, globs.debug_backtrace, ), + exit_timeout, ( SourceCodeHandler(tab_width, context_line_count) if collect_source_code @@ -113,6 +116,9 @@ def initialize( globs.next_except_hook = sys.excepthook sys.excepthook = bt_except_hook + if exit_timeout != 0: + atexit.register(finalize) + def construct_submission_url(endpoint, token): if "submit.backtrace.io" in endpoint or token is None: @@ -132,7 +138,8 @@ def construct_submission_url(endpoint, token): def finalize(): if globs.handler is None: return - globs.handler.dispose() + globs.handler.finish() + globs.handler = None def send_last_exception(**kwargs): diff --git a/backtracepython/report_queue.py b/backtracepython/report_queue.py index a42048d..07282dc 100644 --- a/backtracepython/report_queue.py +++ b/backtracepython/report_queue.py @@ -8,9 +8,10 @@ class ReportQueue: - def __init__(self, request_handler, source_code_handler=None): + def __init__(self, request_handler, exit_timeout=None, source_code_handler=None): self.request_handler = request_handler self.source_code_handler = source_code_handler + self.exit_timeout = exit_timeout # report submission tasks queue self.report_queue = queue.Queue() @@ -24,7 +25,7 @@ def __init__(self, request_handler, source_code_handler=None): def _worker(self): while True: report_data = self.report_queue.get() - if report_data is None or self.active == False: + if report_data is None: self.report_queue.task_done() break report, attachments = report_data @@ -41,12 +42,7 @@ def process(self, report, attachments): self.source_code_handler.collect(report) self.request_handler.send(report, attachments) - def __del__(self): - self.dispose() - - def dispose(self): - # Put a sentinel value to stop the worker thread - self.active = False + def finish(self): self.report_queue.put_nowait(None) self.report_queue.join() - self.worker_thread.join() + self.worker_thread.join(timeout=self.exit_timeout) From b3cf3de08d03c958bcb061d867dccd121b030c4e Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Fri, 27 Sep 2024 20:36:08 +0200 Subject: [PATCH 2/4] Update backtracepython/client.py Co-authored-by: Sebastian Alex --- backtracepython/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracepython/client.py b/backtracepython/client.py index 5c2145b..18b46a0 100644 --- a/backtracepython/client.py +++ b/backtracepython/client.py @@ -116,7 +116,7 @@ def initialize( globs.next_except_hook = sys.excepthook sys.excepthook = bt_except_hook - if exit_timeout != 0: + if exit_timeout > 0: atexit.register(finalize) From 0943d3488231beed9264d325cce57a5f3bfe7d1e Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Fri, 27 Sep 2024 21:28:55 +0200 Subject: [PATCH 3/4] Update report_queue.py Add a comment about the thread exit parameter --- backtracepython/report_queue.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backtracepython/report_queue.py b/backtracepython/report_queue.py index 07282dc..ec7f454 100644 --- a/backtracepython/report_queue.py +++ b/backtracepython/report_queue.py @@ -43,6 +43,7 @@ def process(self, report, attachments): self.request_handler.send(report, attachments) def finish(self): + # Put a sentinel value to stop the worker thread self.report_queue.put_nowait(None) self.report_queue.join() self.worker_thread.join(timeout=self.exit_timeout) From 78a10b90aa3031d03ab271cb5157d285b641e94b Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Mon, 30 Sep 2024 16:39:15 +0200 Subject: [PATCH 4/4] Customize exit timeout --- backtracepython/client.py | 2 +- backtracepython/report_queue.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backtracepython/client.py b/backtracepython/client.py index 18b46a0..e034946 100644 --- a/backtracepython/client.py +++ b/backtracepython/client.py @@ -90,7 +90,7 @@ def initialize( context_line_count=200, collect_source_code=True, disable_global_handler=False, - exit_timeout=5, + exit_timeout=4, ): globs.endpoint = construct_submission_url(endpoint, token) globs.debug_backtrace = debug_backtrace diff --git a/backtracepython/report_queue.py b/backtracepython/report_queue.py index ec7f454..a29201a 100644 --- a/backtracepython/report_queue.py +++ b/backtracepython/report_queue.py @@ -8,7 +8,7 @@ class ReportQueue: - def __init__(self, request_handler, exit_timeout=None, source_code_handler=None): + def __init__(self, request_handler, exit_timeout, source_code_handler=None): self.request_handler = request_handler self.source_code_handler = source_code_handler self.exit_timeout = exit_timeout @@ -25,7 +25,7 @@ def __init__(self, request_handler, exit_timeout=None, source_code_handler=None) def _worker(self): while True: report_data = self.report_queue.get() - if report_data is None: + if report_data is None or self.active == False: self.report_queue.task_done() break report, attachments = report_data @@ -45,5 +45,5 @@ def process(self, report, attachments): def finish(self): # Put a sentinel value to stop the worker thread self.report_queue.put_nowait(None) - self.report_queue.join() self.worker_thread.join(timeout=self.exit_timeout) + self.active = False