Repository navigation
fix(api): stop blocking the event loop and bound scan concurrency (BUG-013) - #138
Merged
Merged
Conversation
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.
Description
Fixes the event-loop blocking and unbounded concurrency of the scan endpoints (internal report
BUG-013).Problem:
/api/analyze,/api/attack,/api/scrape,/api/scrape/batchand/api/quickwere plaindefhandlers dispatched by FastAPI onto the shared AnyIO threadpool (40 tokens). Each scan held one token for its whole (seconds-long) duration, so a burst larger than the pool built an unbounded FIFO queue with no signal to the client and no deadline — and scan work competed with synchronous dependencies (auth) and streaming responses for the same tokens. Measured on the real app: 45 concurrent scans all answered200after ~8–12s (40 queued in one batch), while async endpoints waited behind them. Download streaming and the download cleanup scheduler also ran blocking work directly on the event loop, and every scan paid fresh SSL context setup (420–1300 ms vs ~0.3 ms cached).Changes:
src/ciberwebscan/api/concurrency.py): a per-process in-flight counter rejects excess scans immediately with 429 +Retry-Afterinstead of queueing indefinitely; an optional deadline (api.scan_timeout_seconds) answers 504 on time while the non-cancellable worker keeps its admission slot until the thread truly ends (released by a done callback on the loop, never by request teardown). Scan work runs on a dedicated AnyIOCapacityLimitersized fromapi.max_concurrent_scans, separate from the shared threadpool, so scans cannot crowd out auth or streaming responses.asyncwrappers that callrun_scan()around the unchanged synchronous implementations (service logic untouched).run_in_threadpool.(http2, SSL_CERT_FILE, SSL_CERT_DIR)and gated bytrust_env(httpcore mutates ALPN on the shared context).api.max_concurrent_scans(default32),api.scan_timeout_seconds(defaultnull= no deadline),api.sync_threadpool_tokens(default40).The cancellation semantics were verified with isolated probes and integration tests against a real uvicorn server on
127.0.0.1: after a 504 the slot stays held while the worker runs (in_flight=1) and is released only when the worker thread truly finishes — capacity always tracks real resource usage, not the client's willingness to wait.Related Issue
N/A — internal report
BUG-013(no tracking GitHub issue).Type of Change
Checklist
ruff checkclean,pyright0 errors, prettier-formatted docsapi/concurrency.pytests/unit/api/test_concurrency_control.py(admission, 429, deadline/held slot),tests/integration/api/test_concurrency.py(504 under a real server),tests/integration/api/test_concurrency_limits.py(429 + health responsiveness under saturation),tests/unit/services/test_cleanup_scheduler.py,tests/unit/core/client/test_ssl_context_cache.pymaindocs/API.md(Scan concurrency section, status codes) anddocs/CONFIGURATION.md(new fields, restart vs runtime semantics)Screenshots (if applicable)
N/A — server-side behaviour; observable via
429/504status codes, theRetry-Afterheader and responsive/healthunder scan saturation (covered by integration tests).