Repository navigation
Description
Clone of #10085 for 10.1.
Environment Information
- JRuby 10.0.8.0 and 10.1.3.0
- Temurin 21.0.13, on GitHub's windows-latest runners
- Windows Server 2025
Expected Behavior
A non-blocking lock on a file that another process has locked returns false, as it does on CRuby (and on JRuby elsewhere):
# hold.rb
file = File.new("test.lock", "a+")
file.flock(File::LOCK_EX)
puts "holding"
sleep# try.rb
file = File.new("test.lock", "a+")
p file.flock(File::LOCK_EX | File::LOCK_NB)Run jruby hold.rb, and once it prints holding, run jruby try.rb in another console. try.rb should print false.
Actual Behavior
try.rb raises:
Java::JavaLang::NullPointerException: Cannot invoke "jnr.constants.platform.Errno.ordinal()" because the return value of "org.jruby.util.i...
(My log cut the message off there.)
I think this came in with #9347, which made File#flock use the JDK's FileChannel locks on Windows. When another process holds the lock, FileChannel#tryLock returns null, and PosixShim#tryLock returns lockFailedReturn(...) without setting an errno:
jruby/core/src/main/java/org/jruby/util/io/PosixShim.java
Lines 684 to 698 in 5378ecb
| private int tryLock(ChannelFD fd, boolean exclusive) throws IOException { | |
| FileLock fileLock = fd.currentLock.get(); | |
| if (fileLock != null) fileLock.release(); | |
| fileLock = fd.chFile.tryLock(0L, Long.MAX_VALUE, !exclusive); | |
| fd.currentLock.set(fileLock); | |
| if (fileLock != null) { | |
| return 0; | |
| } | |
| return lockFailedReturn(exclusive ? LOCK_EX : LOCK_SH); | |
| } |
So RubyFile#flock gets -1 with no errno, and its switch (fptr.errno()) throws on the null:
| switch (fptr.errno()) { |
Setting EWOULDBLOCK (or EAGAIN) in tryLock before it returns would make flock return false there, as RubyFile#flock already does for those errnos. From the same code, I think a shared lock that can't be taken (LOCK_SH | LOCK_NB) returns 0 instead, because lockFailedReturn returns 0 for LOCK_SH, so flock would say it got a lock it didn't. I haven't checked that one.
I'm working around it in Duel by taking a Java::JavaLang::NullPointerException from flock to mean the lock is held.
Activity