Repository navigation
Description
Problem
In src/libsimlin/src/patch.rs, the apply_project_patch_internal function mutates the shared salsa DB with staged (uncommitted) patch data, then releases the DB lock before the accept/reject/dry-run decision is made. This creates a window where concurrent simlin_sim_new or simlin_project_get_errors calls can compile and simulate against uncommitted edits.
Timeline
- Line 441-442: Clone prev_state from sync_state
- Line 443-447: Sync DB to staged datamodel (DB is now inconsistent with committed project)
- Line 452: Drop (release) the DB lock
- Lines 454-500: Validate patch (errors, unit warnings, etc.)
- Line 502: Decision point - if rejected or dry_run, revert DB
- Line 526: On commit, update sync_state
Between steps 3 and 5, concurrent callers can acquire the DB lock and observe the staged state even though it hasn't been accepted yet.
Why It Matters
- Correctness violation: The dry-run contract states "project remains unchanged", but concurrent operations may see staged changes
- Silent failures: A dry-run validation can succeed, but a concurrent
simlin_sim_newobserves different state than the final commit - Race condition: Multi-threaded callers (Python bindings, concurrent patch calls) can corrupt sync state if timing aligns poorly
Components Affected
src/libsimlin/src/patch.rs-apply_project_patch_internal(lines 441-520)src/simlin-engine/src/db.rs- salsa DB mutation and lock semantics
Possible Solutions
-
Hold DB lock for entire validation duration (lines 441-500): Blocks concurrent access during patch validation, but simplest and most correct.
-
Clone the salsa DB for staging (expensive): Create a separate DB clone before mutation, keep original locked. Merge results on commit. High memory cost.
-
Document the behavior explicitly: Make it clear that concurrent reads during patch validation may observe staged state. Requires callers to avoid concurrent patch+read patterns (not practical).
Context
- Identified during code review of PR Incremental compilation via salsa framework #289 (incremental compilation implementation)
- Related to but distinct from issue libsimlin: sync_state TOCTOU race in apply_project_patch_internal #297 (sync_state TOCTOU race), which addressed the
.take()→.clone()mutation pattern. This issue concerns the broader window where staged state is visible. - The fix for libsimlin: sync_state TOCTOU race in apply_project_patch_internal #297 changed line 442 from
.take()to.clone(), preventing sync_state invalidation, but does not address DB lock release timing
Activity
libsimlin: add deterministic patch race regressions
libsimlin: add deterministic patch race regressions (#314)
bpowers commentedon Feb 24, 2026
Fixed in PR #289 (incremental compilation). Regression test added in PR #314.