Skip to content

Keep the old lock when a non-blocking flock fails on Windows - #10088

Closed
sferik wants to merge 2 commits into
jruby:jruby-10.0from
sferik:windows-flock-keep-lock
Closed

sferik wants to merge 2 commits into
jruby:jruby-10.0from
sferik:windows-flock-keep-lock

Conversation

@sferik

@sferik sferik commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #10087

Without native flock (always on Windows), PosixShim#tryLock released the lock it held before it tried the new one, because FileChannel won'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 failed flock(File::LOCK_EX | File::LOCK_NB) on a file with a shared lock dropped the shared lock.

CRuby on Windows calls LockFileEx without unlocking first: https://github.com/ruby/ruby/blob/67814cf9b0f3a4bad6f7369b0c8e6fc69220de64/win32/win32.c#L343-L377

Windows can't upgrade a shared lock to an exclusive one, so that call fails and the shared lock stays. Now tryLock does 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 another tryLock.

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 same FileChannel path 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 the FileChannel path on Windows, and I haven't run it there. The Windows LockFileEx behavior 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.

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
sferik force-pushed the windows-flock-keep-lock branch from 1415a5b to c30df04 Compare November 16, 2026 22:16
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
sferik force-pushed the windows-flock-keep-lock branch from c30df04 to c4c450e Compare November 16, 2026 22:49
@sferik

sferik commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing this. While fixing CI I found that Linux's flock(2) also drops the shared lock when a non-blocking upgrade fails, so CRuby on Linux behaves just like JRuby on Windows does today. This change would only match CRuby on Windows, which I haven't confirmed there, and it would make upgrades fail even when no other process holds a lock. That isn't worth it. More detail in #10087.

@sferik sferik closed this Nov 16, 2026
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.

1 participant