fix(issues): Gitea issue sync lists once per scan and keeps at most two requests in flight per instance #548

Merged
sharang merged 1 commits from fix/gitea-issue-sync-load into main 2026-10-09 14:25:30 +00:00
Owner

Why

Incident K-00001 (2026-10-09 13:38 UTC): a customer's managed Gitea (1 GiB) was OOM-killed while a scan filed findings as issues on one repository. The scan fired up to two GET /repos/{o}/{r}/issues?type=issues&state=all&q=... searches per finding (fingerprint, then title), plus label listings and creates, while the background status sync (IssueSync::spawn) hit the same instance in parallel.

What changes

1. Issue sync lists once per scan

  • GiteaTracker::list_issues: all issues of the repository, type=issues&state=all, paged with limit=50. Only an empty page ends the list (an instance may cap the page size), with an upper bound of 200 pages that is an error, not a partial list.
  • create_tracker_issues on Gitea builds an index from that one listing and matches each finding locally: fingerprint from the body marker (*Fingerprint:* \...`, present since #10) first, exact title as fallback (not for a group, as before). Issues opened during the run join the index, so the run's own duplicates are caught too. Existing issues (open or closed) are skipped as before; reopening/closing stays with IssueSync`, CVE issues stay keyed in the database (no searches).
  • A failed listing fails issue creation for that repository with an error (logged by the orchestrator). No fallback to per-finding searches.
  • IssueSync resolves the risk:accepted label once per target per run instead of once per accepted-risk issue.

2. At most two requests in flight per Gitea instance

  • trackers::gitea_limit: a process-wide LazyLock<DashMap<host:port, Arc<Semaphore(2)>>>. Every request of GiteaTracker and of the managed git source's SourceClient takes a permit and keeps it until the response body is read, across all scans of that instance in the agent.
  • 250 ms pause after each issue or label create (after the permit is released).
  • The tracker's eight hand-written request blocks go through one helper; side effect: Gitea's find_existing_issue now returns HTTP/parse errors instead of reading them as "no issue" (which used to create duplicates).

Deliberate deviations from the plan

  • No IssueTracker trait change. Jira's project key spans repositories, so a full listing there is unbounded; GitHub and GitLab are hosted, use rate-limited search APIs and were not involved. The listing lives behind TrackerDispatch::Gitea; other trackers keep the per-finding search.
  • The listing is not filtered by the pruefwerk label. The label is attached only on managed repositories and only when ensure_labels succeeded; the fallback create_issue and user-configured Gitea trackers produce unlabelled issues, which a label filter would miss and re-open as duplicates. One unfiltered paged listing per scan is still far less than two searches per finding.
  • Title matching is exact; the old q= was a keyword search.

Open point

Issue creation only covers a scan's new findings (write_back). When the listing fails, those findings get no issue on a later scan either (the same already holds for a failed create). A catch-up (file issues for open medium+ findings without tracker_issue_url) would fix both; not in this PR.

Tests

  • trackers::gitea: list_issues_reads_every_page_of_open_and_closed_issues, list_issues_does_not_stop_at_a_capped_page, a_failed_listing_is_an_error_not_an_empty_list, never_more_than_two_requests_in_flight_per_instance (local axum server counts concurrent requests from 10 calls over two trackers), existing label tests.
  • trackers::gitea_limit: one_key_per_host_and_port, a_third_permit_waits_for_one_of_the_first_two.
  • pipeline::issue_creation: the_fingerprint_is_read_back_from_the_body, an_issue_is_matched_by_its_fingerprint_marker, an_issue_without_the_marker_is_matched_by_title_unless_grouped, no_match_means_a_new_issue_and_it_is_found_afterwards.
  • pipeline::issue_sync: the_risk_label_is_resolved_once_per_target; Mongo-backed an_issue_follows_its_findings_status and cve_issues tests pass against a local Mongo.
  • The fake Gitea label endpoints in the tests now end their list with an empty page like Gitea does.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

## Why Incident K-00001 (2026-10-09 13:38 UTC): a customer's managed Gitea (1 GiB) was OOM-killed while a scan filed findings as issues on one repository. The scan fired up to two `GET /repos/{o}/{r}/issues?type=issues&state=all&q=...` searches per finding (fingerprint, then title), plus label listings and creates, while the background status sync (`IssueSync::spawn`) hit the same instance in parallel. ## What changes **1. Issue sync lists once per scan** - `GiteaTracker::list_issues`: all issues of the repository, `type=issues&state=all`, paged with `limit=50`. Only an empty page ends the list (an instance may cap the page size), with an upper bound of 200 pages that is an error, not a partial list. - `create_tracker_issues` on Gitea builds an index from that one listing and matches each finding locally: fingerprint from the body marker (`*Fingerprint:* \`...\``, present since #10) first, exact title as fallback (not for a group, as before). Issues opened during the run join the index, so the run's own duplicates are caught too. Existing issues (open or closed) are skipped as before; reopening/closing stays with `IssueSync`, CVE issues stay keyed in the database (no searches). - A failed listing fails issue creation for that repository with an error (logged by the orchestrator). No fallback to per-finding searches. - `IssueSync` resolves the `risk:accepted` label once per target per run instead of once per accepted-risk issue. **2. At most two requests in flight per Gitea instance** - `trackers::gitea_limit`: a process-wide `LazyLock<DashMap<host:port, Arc<Semaphore(2)>>>`. Every request of `GiteaTracker` and of the managed git source's `SourceClient` takes a permit and keeps it until the response body is read, across all scans of that instance in the agent. - 250 ms pause after each issue or label create (after the permit is released). - The tracker's eight hand-written request blocks go through one helper; side effect: Gitea's `find_existing_issue` now returns HTTP/parse errors instead of reading them as "no issue" (which used to create duplicates). ## Deliberate deviations from the plan - **No `IssueTracker` trait change.** Jira's project key spans repositories, so a full listing there is unbounded; GitHub and GitLab are hosted, use rate-limited search APIs and were not involved. The listing lives behind `TrackerDispatch::Gitea`; other trackers keep the per-finding search. - **The listing is not filtered by the `pruefwerk` label.** The label is attached only on managed repositories and only when `ensure_labels` succeeded; the fallback `create_issue` and user-configured Gitea trackers produce unlabelled issues, which a label filter would miss and re-open as duplicates. One unfiltered paged listing per scan is still far less than two searches per finding. - Title matching is exact; the old `q=` was a keyword search. ## Open point Issue creation only covers a scan's *new* findings (`write_back`). When the listing fails, those findings get no issue on a later scan either (the same already holds for a failed create). A catch-up (file issues for open medium+ findings without `tracker_issue_url`) would fix both; not in this PR. ## Tests - `trackers::gitea`: `list_issues_reads_every_page_of_open_and_closed_issues`, `list_issues_does_not_stop_at_a_capped_page`, `a_failed_listing_is_an_error_not_an_empty_list`, `never_more_than_two_requests_in_flight_per_instance` (local axum server counts concurrent requests from 10 calls over two trackers), existing label tests. - `trackers::gitea_limit`: `one_key_per_host_and_port`, `a_third_permit_waits_for_one_of_the_first_two`. - `pipeline::issue_creation`: `the_fingerprint_is_read_back_from_the_body`, `an_issue_is_matched_by_its_fingerprint_marker`, `an_issue_without_the_marker_is_matched_by_title_unless_grouped`, `no_match_means_a_new_issue_and_it_is_found_afterwards`. - `pipeline::issue_sync`: `the_risk_label_is_resolved_once_per_target`; Mongo-backed `an_issue_follows_its_findings_status` and `cve_issues` tests pass against a local Mongo. - The fake Gitea label endpoints in the tests now end their list with an empty page like Gitea does. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 1 commit 2026-10-09 14:14:07 +00:00
fix(issues): Gitea issue sync lists once per scan and keeps at most two requests in flight per instance
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 9m24s
9882d48d3d
A managed Gitea (1 GiB) was OOM-killed while a scan filed its findings as
issues: up to two q= searches per finding, label listings and creates hit it
in parallel with the background status sync.

- Issue creation on Gitea lists the repository's issues once (paged, 50 per
  page, state=all) and matches each finding locally: by the fingerprint
  marker in the body, then by exact title (not for a group). Issues opened
  during the run join the index. A listing that fails stops issue creation for
  that repository with an error (logged), instead of searching per finding.
  Issue creation only covers a scan's new findings, so those of a failed
  run get no issue later (as with a failed create today). Other trackers keep their search.
- The status sync resolves the risk:accepted label once per target, not per
  issue.
- Every request to a Gitea, from the tracker and the managed git source, takes
  one of two permits of its instance (host:port), shared across all scans of
  the process; creates are followed by a 250 ms pause. The tracker's request
  code goes through one helper that holds the permit until the body is read,
  and a failed search no longer reads as "no issue".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang merged commit 11e3b98c99 into main 2026-10-09 14:25:30 +00:00
Sign in to join this conversation.