Skip to content

fix(api): stop blocking the event loop and bound scan concurrency (BUG-013) - #138

Merged
HC-ONLINE merged 6 commits into
mainfrom
fix/bug-async-concurrency
Nov 6, 2026
Merged

HC-ONLINE merged 6 commits into
mainfrom
fix/bug-async-concurrency

Conversation

@HC-ONLINE

Copy link
Copy Markdown
Owner

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/batch and /api/quick were plain def handlers 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 answered 200 after ~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:

  • Admission control with honest cancellation (src/ciberwebscan/api/concurrency.py): a per-process in-flight counter rejects excess scans immediately with 429 + Retry-After instead 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 AnyIO CapacityLimiter sized from api.max_concurrent_scans, separate from the shared threadpool, so scans cannot crowd out auth or streaming responses.
  • Scan routes became async wrappers that call run_scan() around the unchanged synchronous implementations (service logic untouched).
  • Download streaming and the cleanup scheduler are offloaded with run_in_threadpool.
  • Shared SSL context cache keyed by (http2, SSL_CERT_FILE, SSL_CERT_DIR) and gated by trust_env (httpcore mutates ALPN on the shared context).
  • New configuration: api.max_concurrent_scans (default 32), api.scan_timeout_seconds (default null = no deadline), api.sync_threadpool_tokens (default 40).

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have followed the style guidelines of this project (Ruff & Pyright) — ruff check clean, pyright 0 errors, prettier-formatted docs
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas — the cancellation/token-ownership semantics are documented in api/concurrency.py
  • I have added tests that prove my fix is effective or that my feature works — tests/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.py
  • New and existing unit tests pass locally with my changes — full suite green locally (2123 passed, 3 skipped) plus concurrency suites re-run clean after merging main
  • I have updated the documentation accordingly — docs/API.md (Scan concurrency section, status codes) and docs/CONFIGURATION.md (new fields, restart vs runtime semantics)

Screenshots (if applicable)

N/A — server-side behaviour; observable via 429/504 status codes, the Retry-After header and responsive /health under scan saturation (covered by integration tests).

@HC-ONLINE
HC-ONLINE merged commit fb17699 into main Nov 6, 2026
7 checks passed
@HC-ONLINE
HC-ONLINE deleted the fix/bug-async-concurrency branch November 6, 2026 19:10
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.

1 participant