Skip to content

fix: shut down "session flusher" more promptly #4561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions sentry_sdk/sessions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -271,7 +270,7 @@ def add_session(

def kill(self):
# type: (...) -> None
self._running = False
self.__shutdown_requested.set()

def __del__(self):
# type: (...) -> None
Expand Down
Loading