fix: add advisory file locking (flock) to prevent TOCTOU race in Installer::install() (closes #77)#157
Merged
Merged
Conversation
…aller::install() (closes #77) Add LOCK_EX acquisition on lib/install.lock before the file-exists check to serialize concurrent installations of the FFI shared library. Key changes: - Acquire exclusive flock before checking if library exists (lock-check pattern) - Double-check inside the lock: re-verify library isn't already installed - Lock file persists as sentinel (never deleted) — ensures all concurrent processes share the same inode for flock() serialization - Lock released in finally block — crash-safe (kernel auto-releases flock) - Add PHPDoc documenting the lock pattern and rationale - Add test_installer_flock.phpt with 4 test scenarios: 1. Basic lock file create/acquire/release/cleanup 2. Concurrent serialization with 3 child processes (no overlap) 3. Source code verification of flock/double-check patterns 4. Behavioral test: calls Installer::install() with dummy library to verify double-check pattern avoids re-download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #77
Adds advisory file locking (
flock()) toInstaller::install()to serialize concurrent installations of the FFI shared library, preventing a TOCTOU race condition between the file-exists check and the download+extract window.Changes
LOCK_EXonlib/install.lockbefore thefile_existscheck. Double-check inside the lock (re-verify library isn't already installed). Lock file persists as sentinel (never deleted) to ensure all processes share the same inode forflock()serialization. Lock released infinally— crash-safe.Installer::install()with a dummy library.Key Design Decisions
flock()serialization entirely.install()is called at most once per Composer invoke, so the lock acquisition overhead is negligible.Installer::install().Testing
Installer::install()callCode Review