Skip to content

fix: race condition in update lock lets two updates run at once#27

Open
Adityakk9031 wants to merge 1 commit into
AlmanacCode:mainfrom
Adityakk9031:#26
Open

fix: race condition in update lock lets two updates run at once#27
Adityakk9031 wants to merge 1 commit into
AlmanacCode:mainfrom
Adityakk9031:#26

Conversation

@Adityakk9031

@Adityakk9031 Adityakk9031 commented Jul 10, 2026

Copy link
Copy Markdown

close:#26
The update lock had a classic TOCTOU bug. When the lock was stale, the code would:

  1. Check it's stale
  2. Delete it
  3. Loop back and try open("x") again
    Between step 2 and 3, another process could grab the lock. Now both processes think they own it, so both fire off uv tool upgrade or pip install at the same time. Corrupts the install.

The fix uses os.replace() to atomically swap in the new lock — no window between removing the old one and planting the new one. Also added a read-back safety check: after the replace, we verify our PID is still in the file. If another process raced past us, we detect it and back off.

Tests cover all the paths: fresh lock, non-stale refusal, stale replacement, and the race-lost scenario.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants