Serialize concurrent frontend dependency installs

AL
5d ago
+698-100

Why

Two Reflex processes can currently enter the same frontend install transaction together. Both can mutate .web, invoke the package manager, overwrite the procedure cache, and persist root lockfiles concurrently. A simultaneous frontend reinitialization can also replace .web during an install.

What changed

  • Add a dependency-free per-project process lock at the stable, ignored app-root path .reflex.frontend.lock. POSIX uses flock; Windows uses a retrying one-byte msvcrt lock. The process-local layer is thread-safe and reentrant, and the OS releases ownership after crashes.
  • Hold that lock across the complete public install transaction: package-manager selection, root-to-web synchronization and cache invalidation, cached install execution, cache commit, and web-to-root persistence.
  • Coordinate initialize_web_directory with the same lock so it cannot replace .web during an active install.
  • Atomically replace procedure-cache and persisted package-manager files. Invalid/truncated pickles become cache misses; genuine I/O errors still surface. Existing modes and symlinks are preserved.
  • Keep the lock implementation lazily imported so ordinary CLI startup does not load platform lock modules or allocate lock state.

Atomic replacement guarantees complete-file visibility and preserves the prior file when commit fails; it does not claim power-loss durability without fsync.

Validation

  • uv run pytest -q tests/units/test_prerequisites.py tests/units/utils/test_utils.py tests/units/reflex_base/constants/test_base.py tests/units/reflex_base/constants/test_installer.py — 344 passed
  • uv run ruff check . — passed
  • uv run ruff format --check . — 1407 files already formatted
  • uv run pyright reflex tests — 0 errors, 0 warnings
  • uv run pre-commit run --all-files — all hooks passed, including Ruff, codespell, Pyright, ty, and Biome
  • Independent adversarial review: 104 prerequisite tests and 16 focused concurrency/atomic/cache tests passed; no release-blocking findings

Tests cover positive same-project OS contention, complete transaction scope, completed-cache reuse by a waiting process, separate-project concurrency, reentrancy, exceptions and abrupt process exits, Windows contention retries, initialization exclusion, atomic failure recovery, corrupt-cache recovery, permissions, and symlink compatibility.

Checklist

  • Bug fix (non-breaking)
  • Followed CONTRIBUTING.md
  • Checked for overlapping open PRs
  • Added regression tests for core behavior
  • Ran tests and linters locally