Skip to content

Commit

Permalink
Wait for events in the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
konraddysput committed Sep 27, 2024
1 parent e19dfac commit 932ea72
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 8 additions & 1 deletion backtracepython/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import atexit
import sys

from backtracepython.attributes.attribute_manager import attribute_manager
Expand Down Expand Up @@ -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
Expand All @@ -102,6 +104,7 @@ def initialize(
ignore_ssl_certificate,
globs.debug_backtrace,
),
exit_timeout,
(
SourceCodeHandler(tab_width, context_line_count)
if collect_source_code
Expand All @@ -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:
Expand All @@ -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):
Expand Down
14 changes: 5 additions & 9 deletions backtracepython/report_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 932ea72

Please sign in to comment.