Skip to content

File#flock raises NullPointerException on Windows when another process holds the lock #10085

Closed

Description

@sferik

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:

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

  1. added this to the JRuby 10.0.9.0 milestone on Nov 17, 2026
  2. headius commented on Nov 17, 2026

    @headius
    Member

    Fixed by #10086!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions