Skip to content
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

feat: Detect interpreter in shutdown state on thread spawn #2468

Merged
merged 4 commits into from Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions sentry_sdk/metrics.py
Expand Up @@ -332,18 +332,27 @@
self._ensure_thread()

def _ensure_thread(self):
# type: (...) -> None
# type: (...) -> bool
"""For forking processes we might need to restart this thread.
This ensures that our process actually has that thread running.
"""
if not self._running:
return False

Check warning on line 340 in sentry_sdk/metrics.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/metrics.py#L340

Added line #L340 was not covered by tests
pid = os.getpid()
if self._flusher_pid == pid:
return
return True

Check warning on line 343 in sentry_sdk/metrics.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/metrics.py#L343

Added line #L343 was not covered by tests
with self._lock:
self._flusher_pid = pid
self._flusher = Thread(target=self._flush_loop)
self._flusher.daemon = True
self._flusher.start()
try:
self._flusher.start()
except RuntimeError:

Check warning on line 350 in sentry_sdk/metrics.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/metrics.py#L348-L350

Added lines #L348 - L350 were not covered by tests
# Unfortunately at this point the interpreter is in a start that no
# longer allows us to spawn a thread and we have to bail.
self._running = False
return False
return True

Check warning on line 355 in sentry_sdk/metrics.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/metrics.py#L353-L355

Added lines #L353 - L355 were not covered by tests

def _flush_loop(self):
# type: (...) -> None
Expand Down Expand Up @@ -400,9 +409,7 @@
timestamp=None, # type: Optional[float]
):
# type: (...) -> None
self._ensure_thread()

if self._flusher is None:
if not self._ensure_thread() or self._flusher is None:
return

if timestamp is None:
Expand Down
10 changes: 8 additions & 2 deletions sentry_sdk/worker.py
Expand Up @@ -67,8 +67,14 @@
target=self._target, name="raven-sentry.BackgroundWorker"
)
self._thread.daemon = True
self._thread.start()
self._thread_for_pid = os.getpid()
try:
self._thread.start()
self._thread_for_pid = os.getpid()
except RuntimeError:

Check warning on line 73 in sentry_sdk/worker.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/worker.py#L70-L73

Added lines #L70 - L73 were not covered by tests
# At this point we can no longer start because the interpreter
# is already shutting down. Sadly at this point we can no longer
antonpirker marked this conversation as resolved.
Show resolved Hide resolved
# send out events.
self._thread = None

Check warning on line 77 in sentry_sdk/worker.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/worker.py#L77

Added line #L77 was not covered by tests

def kill(self):
# type: () -> None
Expand Down