Skip to content

fix(scanner): bound source staging enumeration and run it under the scan timeout - #1628

Merged
Dumbris merged 2 commits into
mainfrom
fix/1602-scanner-staging-bounds
Oct 16, 2026
Merged

Dumbris merged 2 commits into
mainfrom
fix/1602-scanner-staging-bounds

Conversation

@Dumbris

@Dumbris Dumbris commented Oct 16, 2026

Copy link
Copy Markdown
Member

Summary

  • Directory enumeration reads in bounded batches and charges the 100k entry budget as entries arrive, so an oversized directory fails closed without allocating every entry.
  • The scan deadline is created before source staging, so staging runs under the scan timeout instead of an unbounded context.
  • File copies go in 256 KiB chunks and check the context between chunks.

Tests

  • TestStagedSource_EntryCapEnforcedMidEnumeration: entries read stay at or under cap + one batch.
  • TestStagedSource_CopyCancelledByDeadline: a cancelled context stops the copy.
  • TestStagedSource_ScanDeadlineCoversStaging: an expired scan timeout stops staging with DeadlineExceeded.
  • go test -race on internal/security/scanner, with and without -tags server; both golangci-lint runs clean.

Closes #1602

…the scan timeout

Read directories in bounded batches charging the entry budget as entries
arrive, create the scan deadline before staging, and copy files in
context-checked chunks.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/1602-scanner-staging-bounds

Available Artifacts

  • archive-darwin-amd64 (31 MB)
  • archive-darwin-arm64 (28 MB)
  • archive-linux-amd64 (19 MB)
  • archive-linux-arm64 (17 MB)
  • archive-windows-amd64 (31 MB)
  • archive-windows-arm64 (28 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (27 MB)
  • installer-dmg-darwin-arm64 (25 MB)
  • smart-mcp-proxymcpproxy-goP3I96Y.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 38895224208 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@codecov-commenter

codecov-commenter commented Oct 16, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 77.08333% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/security/scanner/source_staging.go 75.00% 6 Missing and 5 partials ⚠️

📢 Thoughts on this report? Let us know!

@Dumbris
Dumbris merged commit 4e78309 into main Oct 16, 2026
50 checks passed
Dumbris added a commit that referenced this pull request Oct 17, 2026
…taging deadline test (#1648)

Fixes the two E2E failures on main after #1628 (#1602) that block
v0.71.0.

## Root causes
1. **macOS: `TestServiceStartScanDeepOnRunsSourceResolutionAndPass2`**
(run 39002404847). Not caused by #1628 (Pass 2 staging/in-process path
unchanged; #1628's own main run was green). Real production race:
`OnScanCompleted` spawns `startPass2` inside `executeScan`, before the
deferred `clearActiveJob` frees the server's engine slot. When Pass 2
reaches `StartScan` first it is rejected with "scan already in
progress", logged at Warn and dropped: no Pass 2 job is ever saved (test
times out after 10s) and the resolved source dir leaked. Slow runners
widen the window.
2. **Windows: `TestStagedSource_ScanDeadlineCoversStaging`**. A 1ns
`context.WithTimeout` is not observably expired on the coarse Windows
clock, so staging of a tiny dir finished before the ctx check.

## Fix
- `ErrScanInProgress` sentinel; `startPass2` retries (bounded 10s) while
the blocking job is a Pass-1 job, releases the resolved source and saves
a failed Pass 2 job if it still cannot start.
- Staging test now uses a 20ms timeout, plus a non-skipped source file
and a stall in the `stagingAfterReadBatch` seam past the deadline, so
the following ctx check deterministically fails. Still proves staging
runs under the scan deadline.
- New regression test `TestStartPass2WaitsForPass1SlotRelease` (fails
before: Pass 2 saved as failed with "scan already in progress").

## Verification
`go test -race ./internal/security/...` green, `-count=30` on the
touched tests, `GOOS=windows go vet`, both golangci-lint runs (bare and
`--build-tags server`) 0 issues. The macOS flake did not reproduce
locally under CPU load; diagnosis is from code plus the deterministic
regression test.
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.

scanner: bound source staging enumeration and run it under the scan timeout

2 participants