From 81cf1a2cc060c020627e774119db0c200aced777 Mon Sep 17 00:00:00 2001 From: Buck Evan Date: Tue, 8 Jul 2025 15:53:19 -0500 Subject: [PATCH] fix: shut down "session flusher" more promptly --- sentry_sdk/sessions.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/sentry_sdk/sessions.py b/sentry_sdk/sessions.py index eaeb915e7b..a5dd589ee9 100644 --- a/sentry_sdk/sessions.py +++ b/sentry_sdk/sessions.py @@ -1,7 +1,6 @@ import os -import time import warnings -from threading import Thread, Lock +from threading import Thread, Lock, Event from contextlib import contextmanager import sentry_sdk @@ -162,7 +161,7 @@ def __init__( self._thread_lock = Lock() self._aggregate_lock = Lock() self._thread_for_pid = None # type: Optional[int] - self._running = True + self.__shutdown_requested = Event() def flush(self): # type: (...) -> None @@ -208,10 +207,10 @@ def _ensure_running(self): def _thread(): # type: (...) -> None - while self._running: - time.sleep(self.flush_interval) - if self._running: - self.flush() + running = True + while running: + running = not self.__shutdown_requested.wait(self.flush_interval) + self.flush() thread = Thread(target=_thread) thread.daemon = True @@ -220,7 +219,7 @@ def _thread(): except RuntimeError: # Unfortunately at this point the interpreter is in a state that no # longer allows us to spawn a thread and we have to bail. - self._running = False + self.__shutdown_requested.set() return None self._thread = thread @@ -271,7 +270,7 @@ def add_session( def kill(self): # type: (...) -> None - self._running = False + self.__shutdown_requested.set() def __del__(self): # type: (...) -> None