fix(scheduler): scheduled scans are spread out and at most two clones run per git host #547

Merged
sharang merged 2 commits from fix/scheduled-scan-spread into main 2026-10-09 14:37:23 +00:00
Owner

Follow-up to the 2026-10-09 incident (a tenant's managed Gitea was OOM-killed; six scheduled scans failed with 502). Companion to #546 (retry on 5xx).

Finding

scheduler::scan_all_repos awaits run_target_scan one target at a time, so the scheduler tick itself never cloned a tenant's repos in parallel: the six failures were six sub-second 502s in a serial loop. The real concurrent Scheduled fan-outs are git_source::spawn_scan (targets the git sync creates) and lifecycle::resume_interrupted_scans (after a restart), plus webhooks/clicks across targets.

What changes

  • Jitter: scan_gate::start_delay(trigger): a random 0 to 30 s (SCHEDULED_SCAN_JITTER) for Scheduled, zero for Webhook/Manual/Support. run_target_scan waits it (tokio sleep) before taking the target and a scan slot, so the wait costs no slot. Covers all three Scheduled entry points. Cost: the serial scheduler loop adds a mean of 15 s per target per tick (6 h schedule).
  • Per-host clone limit: pipeline/clone_limit.rs: process-wide CLONE_SLOTS, a counting semaphore per git host (std Mutex + Condvar, 2 per host), taken at the top of GitOps::clone_or_fetch and held to its end, i.e. around the clone/fetch only, not the scan. This is the one function every path goes through (orchestrator, ingest, PR review, graph/chat builds); it is sync, so the wait runs under block_in_place on the multi-thread runtime. Hosts are keyed lower-case; a refused remote has no host and fails as before. The global SCAN_CONCURRENCY semaphore is unchanged.
  • Not limited: submodule fetches (kept small). Once #546 is in, the host slot is also held across its retry waits, which keeps a restarting host from being hammered.

Tests

  • clone_limit::tests::a_host_runs_at_most_two_while_another_host_proceeds: two permits on host A, a third waits (channel times out), host B is granted meanwhile, dropping one A permit lets the waiter through.
  • clone_limit::tests::a_dropped_permit_frees_its_slot
  • scan_gate::tests::a_scheduled_scan_waits_within_the_jitter (1000 samples within 0 to 30 s, not constant), scan_gate::tests::webhook_manual_and_support_scans_start_at_once

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

Follow-up to the 2026-10-09 incident (a tenant's managed Gitea was OOM-killed; six scheduled scans failed with 502). Companion to #546 (retry on 5xx). ## Finding `scheduler::scan_all_repos` awaits `run_target_scan` one target at a time, so the scheduler tick itself never cloned a tenant's repos in parallel: the six failures were six sub-second 502s in a serial loop. The real concurrent `Scheduled` fan-outs are `git_source::spawn_scan` (targets the git sync creates) and `lifecycle::resume_interrupted_scans` (after a restart), plus webhooks/clicks across targets. ## What changes - **Jitter:** `scan_gate::start_delay(trigger)`: a random 0 to 30 s (`SCHEDULED_SCAN_JITTER`) for `Scheduled`, zero for Webhook/Manual/Support. `run_target_scan` waits it (tokio sleep) before taking the target and a scan slot, so the wait costs no slot. Covers all three Scheduled entry points. Cost: the serial scheduler loop adds a mean of 15 s per target per tick (6 h schedule). - **Per-host clone limit:** `pipeline/clone_limit.rs`: process-wide `CLONE_SLOTS`, a counting semaphore per git host (std Mutex + Condvar, 2 per host), taken at the top of `GitOps::clone_or_fetch` and held to its end, i.e. around the clone/fetch only, not the scan. This is the one function every path goes through (orchestrator, ingest, PR review, graph/chat builds); it is sync, so the wait runs under `block_in_place` on the multi-thread runtime. Hosts are keyed lower-case; a refused remote has no host and fails as before. The global `SCAN_CONCURRENCY` semaphore is unchanged. - Not limited: submodule fetches (kept small). Once #546 is in, the host slot is also held across its retry waits, which keeps a restarting host from being hammered. ## Tests - `clone_limit::tests::a_host_runs_at_most_two_while_another_host_proceeds`: two permits on host A, a third waits (channel times out), host B is granted meanwhile, dropping one A permit lets the waiter through. - `clone_limit::tests::a_dropped_permit_frees_its_slot` - `scan_gate::tests::a_scheduled_scan_waits_within_the_jitter` (1000 samples within 0 to 30 s, not constant), `scan_gate::tests::webhook_manual_and_support_scans_start_at_once` 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 2 commits 2026-10-09 14:25:14 +00:00
A scheduled scan now starts after a random delay of up to 30 s
(SCHEDULED_SCAN_JITTER), applied in run_target_scan before the target and
a scan slot are taken; webhook, manual and support scans start at once.
Every clone or fetch takes one of two slots of its git host
(pipeline/clone_limit.rs, process-wide), held only around clone_or_fetch,
so a tenant's scans no longer all hit one self-hosted Gitea together,
while another host's clones go ahead. The global scan semaphore stays.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
refactor(git): the retry wait and the clone limit share one off-the-runtime wrapper
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 11m19s
a5720f9fa3
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang force-pushed fix/scheduled-scan-spread from 0bf714d580 to a5720f9fa3 2026-10-09 14:25:14 +00:00 Compare
sharang merged commit cc5b667870 into main 2026-10-09 14:37:23 +00:00
Sign in to join this conversation.