-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(telemetry): stop finding() telemetry from blocking the asyncio event loop #677
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
Open
SEPURI-SAI-KRISHNA
wants to merge
2
commits into
usestrix:main
Choose a base branch
from
SEPURI-SAI-KRISHNA:fix/telemetry-nonblocking-672
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+281
−36
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| """Telemetry must never block the caller (regression test for #672). | ||
|
|
||
| ``ReportState.add_vulnerability_report`` runs synchronously inside an async | ||
| tool, so a blocking ``urlopen`` in ``scarf.finding`` / ``posthog.finding`` | ||
| stalled the asyncio event loop — and every concurrent agent — for up to the | ||
| full 10s network timeout per finding. Delivery now runs on a background | ||
| worker; these tests pin that the emit call returns immediately while the | ||
| send still happens off-thread. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import queue | ||
| import time | ||
| from typing import Any, Self | ||
|
|
||
| import pytest | ||
|
|
||
| from strix.telemetry import _common, posthog, scarf | ||
|
|
||
|
|
||
| class _FakeResponse: | ||
| def __enter__(self) -> Self: | ||
| return self | ||
|
|
||
| def __exit__(self, *_: object) -> bool: | ||
| return False | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _drain_queue() -> Any: | ||
| # Keep tests independent: let the background worker finish before the next. | ||
| yield | ||
| _common.flush(timeout=5.0) | ||
|
|
||
|
|
||
| def _install_slow_urlopen(monkeypatch: pytest.MonkeyPatch, module: Any, delay: float) -> list[str]: | ||
| """Patch ``module``'s urlopen to record calls after sleeping ``delay``.""" | ||
| calls: list[str] = [] | ||
|
|
||
| def _slow_urlopen(req: Any, timeout: float | None = None) -> _FakeResponse: # noqa: ARG001 | ||
| time.sleep(delay) | ||
| calls.append(req.full_url) | ||
| return _FakeResponse() | ||
|
|
||
| monkeypatch.setattr(module, "_is_enabled", lambda: True) | ||
| monkeypatch.setattr(module.urllib.request, "urlopen", _slow_urlopen) | ||
| return calls | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("module", [scarf, posthog]) | ||
| def test_finding_returns_without_waiting_for_network( | ||
| monkeypatch: pytest.MonkeyPatch, module: Any | ||
| ) -> None: | ||
| calls = _install_slow_urlopen(monkeypatch, module, delay=1.0) | ||
|
|
||
| start = time.perf_counter() | ||
| module.finding("high") | ||
| elapsed = time.perf_counter() - start | ||
|
|
||
| # The 1s "network" call must not be on the caller's critical path. | ||
| assert elapsed < 0.2, f"{module.__name__}.finding blocked the caller for {elapsed:.3f}s" | ||
|
|
||
| _common.flush(timeout=5.0) | ||
| assert calls, f"{module.__name__}.finding never delivered the event off-thread" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("module", [scarf, posthog]) | ||
| def test_disabled_telemetry_sends_nothing( | ||
| monkeypatch: pytest.MonkeyPatch, module: Any | ||
| ) -> None: | ||
| calls: list[str] = [] | ||
|
|
||
| def _urlopen(req: Any, timeout: float | None = None) -> _FakeResponse: # noqa: ARG001 | ||
| calls.append(req.full_url) | ||
| return _FakeResponse() | ||
|
|
||
| monkeypatch.setattr(module, "_is_enabled", lambda: False) | ||
| monkeypatch.setattr(module.urllib.request, "urlopen", _urlopen) | ||
|
|
||
| module.finding("high") | ||
| _common.flush(timeout=1.0) | ||
| assert not calls | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("module", [scarf, posthog]) | ||
| def test_terminal_events_deliver_after_shutdown_flush( | ||
| monkeypatch: pytest.MonkeyPatch, module: Any | ||
| ) -> None: | ||
| # end()/error() were synchronous before; they must still deliver once the | ||
| # queue is drained (the atexit hook uses this same flush at shutdown). | ||
| calls = _install_slow_urlopen(monkeypatch, module, delay=0.0) | ||
|
|
||
| module.error("unhandled_exception") | ||
| _common.flush(timeout=5.0) | ||
| assert calls, f"{module.__name__}.error was lost instead of delivered on flush" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("module", [scarf, posthog]) | ||
| def test_unserializable_property_does_not_raise_to_caller( | ||
| monkeypatch: pytest.MonkeyPatch, module: Any | ||
| ) -> None: | ||
| # A property whose str()/JSON encoding blows up must be swallowed by the | ||
| # guarded delivery closure, never propagate through the public call. | ||
| class _Explosive: | ||
| def __str__(self) -> str: | ||
| raise RuntimeError("boom") | ||
|
|
||
| monkeypatch.setattr(module, "_is_enabled", lambda: True) | ||
| monkeypatch.setattr( | ||
| module.urllib.request, | ||
| "urlopen", | ||
| lambda *_a, **_k: (_ for _ in ()).throw(AssertionError("should not reach network")), | ||
| ) | ||
|
|
||
| # Must not raise even though the property can't be serialized/encoded. | ||
| module._send("scan_ended", {**_common.base_props(), "bad": _Explosive()}) | ||
| _common.flush(timeout=2.0) | ||
|
|
||
|
|
||
| def test_dispatch_drops_when_queue_full(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| # A hung endpoint must not let the queue grow without bound or block enqueue. | ||
| full_queue: queue.Queue[Any] = queue.Queue(maxsize=1) | ||
| full_queue.put(lambda: None) # occupy the only slot; nothing drains it here | ||
| monkeypatch.setattr(_common, "_telemetry_queue", full_queue) | ||
| monkeypatch.setattr(_common, "_ensure_worker", lambda: None) # don't spawn a real worker | ||
|
|
||
| start = time.perf_counter() | ||
| _common.dispatch(lambda: None) # must drop, not raise or block | ||
| assert time.perf_counter() - start < 0.1 | ||
|
|
||
| # Slot still holds only the original task; the overflow task was dropped. | ||
| assert full_queue.qsize() == 1 | ||
| full_queue.get_nowait() | ||
| with pytest.raises(queue.Empty): | ||
| full_queue.get_nowait() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the CLI shutdown path calls
posthog.end(...)andscarf.end(...), those calls now only enqueue work on this daemon thread. The process can exit before the worker dequeues the terminal events, so end/error telemetry that was previously sent synchronously can be silently lost.Prompt To Fix With AI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a bounded atexit flush (3s) that drains the queue at interpreter shutdown. end/error now deliver in the normal case; the bound means a hung endpoint still can't stall process exit (strictly better than the old per-call 10s synchronous block). Verified at real shutdown the process waits for an in-flight send to finish before exiting.