|
4 | 4 | import json |
5 | 5 | import os |
6 | 6 | import socket |
| 7 | +import threading |
7 | 8 | import warnings |
8 | 9 | from collections import namedtuple |
9 | 10 | from contextlib import contextmanager |
@@ -293,11 +294,71 @@ def inner(identifier): |
293 | 294 | return inner |
294 | 295 |
|
295 | 296 |
|
| 297 | +def _install_flush_completion_handshake(client: "sentry_sdk.Client") -> None: |
| 298 | + """Make batcher.flush() wait for the flusher thread to drain. |
| 299 | +
|
| 300 | + Otherwise, test assertions can run before envelopes are captured. |
| 301 | + The span batcher flushes pending items asynchronously with the main thread. |
| 302 | + Flushes triggered by segments finishing are asynchronous, and can collect buckets |
| 303 | + that would have otherwise been flushed synchronously by `sentry_sdk.flush()`. |
| 304 | + """ |
| 305 | + batcher = client.span_batcher |
| 306 | + if batcher is None: |
| 307 | + return |
| 308 | + |
| 309 | + orig_flush_raw = batcher._flush |
| 310 | + orig_flush = batcher.flush |
| 311 | + lock = threading.Lock() |
| 312 | + drained_count = 0 |
| 313 | + wake = threading.Event() |
| 314 | + |
| 315 | + def _flush(*args: "Any", **kwargs: "Any") -> "Any": |
| 316 | + nonlocal drained_count |
| 317 | + try: |
| 318 | + return orig_flush_raw(*args, **kwargs) |
| 319 | + finally: |
| 320 | + with lock: |
| 321 | + drained_count += 1 |
| 322 | + wake.set() |
| 323 | + |
| 324 | + def flush() -> None: |
| 325 | + nonlocal drained_count |
| 326 | + # Re-entrancy guard: if `flush()` is invoked from within a drain (e.g. a |
| 327 | + # custom transport), waiting on the flusher thread would deadlock, because |
| 328 | + # the flusher is blocked inside our own handler. |
| 329 | + if getattr(getattr(batcher, "_active", None), "flag", False): |
| 330 | + orig_flush() |
| 331 | + return |
| 332 | + |
| 333 | + # If the background flusher thread was never started (no spans have |
| 334 | + # been added), there is no thread to drain and the counter will never |
| 335 | + # advance. Fall back to the original synchronous flush. |
| 336 | + if batcher._flusher is None or not batcher._flusher.is_alive(): |
| 337 | + orig_flush() |
| 338 | + return |
| 339 | + |
| 340 | + with lock: |
| 341 | + target = drained_count |
| 342 | + |
| 343 | + batcher._flush_event.set() |
| 344 | + while True: |
| 345 | + with lock: |
| 346 | + if drained_count > target: |
| 347 | + break |
| 348 | + wake.wait() |
| 349 | + wake.clear() |
| 350 | + orig_flush() |
| 351 | + |
| 352 | + object.__setattr__(batcher, "_flush", _flush) |
| 353 | + object.__setattr__(batcher, "flush", flush) |
| 354 | + |
| 355 | + |
296 | 356 | @pytest.fixture |
297 | 357 | def sentry_init(request): |
298 | 358 | def inner(*a, **kw): |
299 | 359 | kw.setdefault("transport", TestTransport()) |
300 | 360 | client = sentry_sdk.Client(*a, **kw) |
| 361 | + _install_flush_completion_handshake(client) |
301 | 362 | sentry_sdk.get_global_scope().set_client(client) |
302 | 363 |
|
303 | 364 | if request.node.get_closest_marker("forked"): |
|
0 commit comments