Repository navigation
fix: run atif-scan off the event loop - #35
Merged
Merged
Conversation
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>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Implements #26, upstreaming vchaplygim#5 (squashed onto current
main). Co-authored by @vchaplygim.Guard._scancalled the synchronous, CPU-boundscanner.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 throughasyncio.to_thread; step building andsignals()move into a_signalshelper. 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 oneGuardare awaited one at a time (both the JSON and streaming paths inrewrite.py), so this is defensive.Notes:
Tests:
tests/test_guard.pyruns a CPU-bound fake scanner and measures the longest gap between ticks of a loop timer. It fails onmain(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