Skip to content

fix: run atif-scan off the event loop - #35

Merged
younesbelkada merged 1 commit into
mainfrom
fix/scan-off-event-loop
Nov 17, 2026
Merged

younesbelkada merged 1 commit into
mainfrom
fix/scan-off-event-loop

Conversation

@younesbelkada

Copy link
Copy Markdown
Collaborator

Implements #26, upstreaming vchaplygim#5 (squashed onto current main). Co-authored by @vchaplygim.

Guard._scan called the synchronous, CPU-bound scanner.signals() on the event loop, and every proxied request is served on that loop, so one slow scan stalled every other in-flight request (~170 ms per reviewed call on a large trace, per #26). The scan now runs through asyncio.to_thread; step building and signals() move into a _signals helper. Verdicts are unchanged.

Change from the fork PR: the pending calls are copied on the loop (list(self._calls)) and passed to the thread, so the thread never reads a list the loop appends to. Today calls on one Guard are awaited one at a time (both the JSON and streaming paths in rewrite.py), so this is defensive.

Notes:

  • Under the GIL the scan is not parallel; the loop gets the GIL back every switch interval instead of waiting for the whole scan.
  • Plugin detectors (including Hub plugins from feat: load plugins from HF Hub #31) now run in a worker thread too. atif-scan's built-ins keep no shared mutable state on this path.
  • If the client disconnects mid-scan, the thread finishes its scan anyway; harmless.

Tests: tests/test_guard.py runs a CPU-bound fake scanner and measures the longest gap between ticks of a loop timer. It fails on main (0.40 s gap) and passes here (a few ms; 5/5 runs). Full suite: 134 passed, 33 skipped.

Closes #26

🤖 Generated with Claude Code

Guard._scan called the synchronous, CPU-bound scanner.signals() on the event
loop, and every proxied request is served on that loop, so one slow scan
stalled every other in-flight request. Run it through asyncio.to_thread. The
calls are copied on the loop before the thread starts, so the thread never
reads a list the loop appends to. Verdicts are unchanged.

Under the GIL the scan is not parallel; the loop gets the GIL back every
switch interval instead of waiting for the whole scan. tests/test_guard.py
measures the longest gap between ticks of a loop timer during a CPU-bound
scan: 0.40 s on main, a few ms here.

Closes #26

Co-authored-by: v.chaplygin <chaplygim@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@younesbelkada
younesbelkada merged commit e45f6d2 into main Nov 17, 2026
10 checks passed
@younesbelkada
younesbelkada deleted the fix/scan-off-event-loop branch November 17, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

atif-scan runs on the proxy event loop: one slow scan stalls every concurrent request

1 participant