Repository navigation
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
sferik
force-pushed
the
windows-flock-keep-lock
branch
from
November 16, 2026 22:16
1415a5b to
c30df04
Compare
FileChannel refuses a second lock on a range the channel already locks, so PosixShim#tryLock released the old lock before it tried the new one. When the new lock couldn't be taken, the file was left with no lock at all. A non-blocking upgrade from a shared lock now fails without releasing anything, as in CRuby on Windows, where LockFileEx can't upgrade a shared lock. A failed non-blocking downgrade still has to release the exclusive lock first, so it takes it back with another tryLock. Fixes jruby#10087
sferik
force-pushed
the
windows-flock-keep-lock
branch
from
November 16, 2026 22:49
c30df04 to
c4c450e
Compare
Contributor
Author
|
Closing this. While fixing CI I found that Linux's |
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 #10087
Without native flock (always on Windows),
PosixShim#tryLockreleased the lock it held before it tried the new one, becauseFileChannelwon't take a second lock on the same range. When the new lock failed, the file was left with no lock at all. So a failedflock(File::LOCK_EX | File::LOCK_NB)on a file with a shared lock dropped the shared lock.CRuby on Windows calls
LockFileExwithout unlocking first: https://github.com/ruby/ruby/blob/67814cf9b0f3a4bad6f7369b0c8e6fc69220de64/win32/win32.c#L343-L377Windows can't upgrade a shared lock to an exclusive one, so that call fails and the shared lock stays. Now
tryLockdoes the same: a non-blocking exclusive request on a file that holds a shared lock returns false without releasing anything. That removes the window where another process could take the lock. It also means the upgrade fails even when no other process holds a lock, which is what CRuby does on Windows. A failed non-blocking downgrade from exclusive to shared still has to release first, so it takes the exclusive lock back with anothertryLock.I added a spec where a child process upgrades a shared lock and fails, and the parent checks that it still can't get an exclusive lock. The spec is skipped on Linux, because Linux's
flock(2)releases the shared lock before it tries the exclusive one, so CRuby on Linux drops the lock too. macOS keeps it, like Windows. I tested on macOS with-Xnative.enabled=false, which takes the sameFileChannelpath as Windows, using the script from the issue. Before the fix a third process got the exclusive lock, and now it doesn't. The spec passes on CRuby and on JRuby in interpreter and JIT mode, but it only exercises theFileChannelpath on Windows, and I haven't run it there. The WindowsLockFileExbehavior comes from Microsoft's docs, so it would be good if someone could check it on Windows.One difference remains: a blocking
flock(File::LOCK_EX)on a file with a shared lock still releases and waits for the exclusive lock. On CRuby on Windows I'd expect that call to wait forever on the file's own lock, and I didn't copy that.This builds on #10086, which changes the same method, so it includes that commit too. I'll mark it ready once that one is merged.