Skip to content

Commit 26b6853

Browse files
authored
fix(metrics): Only start thread on demand (#2727)
1 parent 2772dde commit 26b6853

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

sentry_sdk/metrics.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ def __init__(
445445

446446
self._flusher = None # type: Optional[Union[threading.Thread, ThreadPool]]
447447
self._flusher_pid = None # type: Optional[int]
448-
self._ensure_thread()
449448

450449
def _ensure_thread(self):
451450
# type: (...) -> bool
@@ -460,6 +459,11 @@ def _ensure_thread(self):
460459
return True
461460

462461
with self._lock:
462+
# Recheck to make sure another thread didn't get here and start the
463+
# the flusher in the meantime
464+
if self._flusher_pid == pid:
465+
return True
466+
463467
self._flusher_pid = pid
464468

465469
if not is_gevent():
@@ -484,9 +488,9 @@ def _flush_loop(self):
484488
# type: (...) -> None
485489
_in_metrics.set(True)
486490
while self._running or self._force_flush:
487-
self._flush()
488491
if self._running:
489492
self._flush_event.wait(self.FLUSHER_SLEEP_TIME)
493+
self._flush()
490494

491495
def _flush(self):
492496
# type: (...) -> None

tests/test_metrics.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ def test_timing_basic(sentry_init, capture_envelopes, maybe_monkeypatched_thread
268268
metrics.timing("timing", 3.0, tags={"a": "b"}, timestamp=ts)
269269
Hub.current.flush()
270270

271-
(envelope,) = envelopes
272271
(envelope,) = envelopes
273272
statsd_item, meta_item = envelope.items
274273

0 commit comments

Comments
 (0)