Skip to content

libsimlin: concurrent reads during patch validation observe staged DB state #298

Closed

Description

@bpowers

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

  1. Line 441-442: Clone prev_state from sync_state
  2. Line 443-447: Sync DB to staged datamodel (DB is now inconsistent with committed project)
  3. Line 452: Drop (release) the DB lock
  4. Lines 454-500: Validate patch (errors, unit warnings, etc.)
  5. Line 502: Decision point - if rejected or dry_run, revert DB
  6. 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_new observes 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

  1. Hold DB lock for entire validation duration (lines 441-500): Blocks concurrent access during patch validation, but simplest and most correct.

  2. Clone the salsa DB for staging (expensive): Create a separate DB clone before mutation, keep original locked. Merge results on commit. High memory cost.

  3. 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

Activity

  1. bpowers commented on Feb 24, 2026

    @bpowers
    OwnerAuthor

    Fixed in PR #289 (incremental compilation). Regression test added in PR #314.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions