From 58ec612b7745377e9d1dc5480aa8950e3cf9bf82 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 5 Oct 2026 15:37:45 +0200 Subject: [PATCH] fix(sast): concurrent scans no longer fail semgrep while staging the rules stage_cra_rules wrote its temp file under the process id, which every scan in an agent shares. With SCAN_CONCURRENCY above one, two scans wrote the same temp file and one renamed it away from under the other, whose rename then failed with "No such file or directory". Since #441 that fails the SAST stage (before, it fell back to the registry rules), so the target got no semgrep findings and its controls read "Nicht bewertet". Seen in the local nightly; a burst of new targets (a Managed Git sync) triggers it. The temp name is now unique per call. A multi-threaded test stages the rules 32 times at once; it fails on the old code every time. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-agent/src/pipeline/semgrep.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/compliance-agent/src/pipeline/semgrep.rs b/compliance-agent/src/pipeline/semgrep.rs index f69943e..11d6365 100644 --- a/compliance-agent/src/pipeline/semgrep.rs +++ b/compliance-agent/src/pipeline/semgrep.rs @@ -19,7 +19,9 @@ const REDACTED_LINES: &str = "requires login"; const CRA_RULES: &str = include_str!("../../rules/cra_semgrep.yaml"); /// Write the bundled CRA rules to a stable temp path (atomic: unique tmp + -/// rename). +/// rename). The tmp name is unique per call, not per process: scans run +/// concurrently in one agent, and a shared tmp file let one scan rename it +/// away while another was still writing it, failing that scan's SAST stage. /// /// # Errors /// @@ -28,7 +30,10 @@ const CRA_RULES: &str = include_str!("../../rules/cra_semgrep.yaml"); async fn stage_cra_rules() -> Result { let dir = std::env::temp_dir(); let path = dir.join("compliance-cra-semgrep.yaml"); - let tmp = dir.join(format!("compliance-cra-semgrep.{}.tmp", std::process::id())); + let tmp = dir.join(format!( + "compliance-cra-semgrep.{}.tmp", + uuid::Uuid::new_v4() + )); let staged = async { tokio::fs::write(&tmp, CRA_RULES).await?; tokio::fs::rename(&tmp, &path).await @@ -209,6 +214,15 @@ fn extract_cwe(metadata: &serde_json::Value) -> Option { mod tests { use super::*; + #[tokio::test(flavor = "multi_thread", worker_threads = 4)] + async fn concurrent_scans_all_get_the_rules() { + let tasks: Vec<_> = (0..32).map(|_| tokio::spawn(stage_cra_rules())).collect(); + for task in tasks { + let path = task.await.unwrap().expect("staged"); + assert_eq!(std::fs::read_to_string(path).unwrap(), CRA_RULES); + } + } + /// The `languages:` of each rule in our pack, by id. The pack is flat /// (`- id:` then `languages: [..]`), so lines are enough. fn rule_languages() -> std::collections::HashMap> {