fix(sast): concurrent scans no longer fail semgrep while staging the rules
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 10m4s

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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
This commit is contained in:
Sharang ParnerkarandClaude Opus 5.5 committed 2026-10-05 15:37:45 +02:00
1 parent 82736a1eec
commit 58ec612b77
1 file changed
+16 -2
+16 -2
View File
@@ -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<PathBuf, CoreError> {
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<String> {
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<String, Vec<String>> {