Repository navigation
Return false from a failed non-blocking flock on Windows - #10086
Merged
Merged
Conversation
Without native flock, which is always the case on Windows, File#flock uses FileChannel#tryLock for LOCK_NB. That returns null when another process holds the lock, and PosixShim#tryLock then returned without setting errno. So LOCK_EX | LOCK_NB raised NullPointerException, and LOCK_SH | LOCK_NB returned 0 though it didn't get the lock. Set EWOULDBLOCK and return -1 for both, so flock returns false. Fixes jruby#10085
This was referenced Nov 16, 2026
Closed
headius
approved these changes
Nov 17, 2026
Member
|
Approved, as it's very simple and obviously NPE is not acceptable. I'm super happy to entertain more improvements for Windows, including better CI testing. |
Contributor
Author
|
@headius should I open another PR against the master branch? |
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.
Fixes #10085
On Windows,
File#flockusesFileChannel#tryLockforLOCK_NB. When another process holds the lock,tryLockreturns null, andPosixShim#tryLockreturned without setting errno. Soflock(File::LOCK_EX | File::LOCK_NB)raised a NullPointerException, andflock(File::LOCK_SH | File::LOCK_NB)returned 0 even though it didn't get the lock. NowtryLocksetsEWOULDBLOCKand returns -1 for both, whichRubyFile#flockalready turns intofalse, the same as CRuby'sflock(2)path.I removed the
windows:tag from the existing "returns false if trying to lock an exclusively locked file" spec and added one for the shared case. I tested on macOS with-Xnative.enabled=false, which takes the sameFileChannelpath as Windows. That reproduced both bugs before the fix. After it, every combination of exclusive and shared locks across two processes gives the same result as CRuby 3.4.12. mspec won't run with native disabled (it rejects the 0 umask), so I checked those cases with a script. The specs pass on CRuby and on JRuby with native enabled, in interpreter and JIT mode.One thing I noticed but didn't change:
tryLockreleases the lock it already holds before trying the new one. So if upgrading a shared lock to exclusive fails, the file is left with no lock at all, whereflock(2)would keep the shared lock.