Skip to content

Make only sockets nonblocking and skip the blocking lock for them - #9921

Open
route wants to merge 5 commits into
jruby:masterfrom
route:socket-nonblock
Open

route wants to merge 5 commits into
jruby:masterfrom
route:socket-nonblock

Conversation

@route

@route route commented Oct 28, 2026

Copy link
Copy Markdown

Experiment for #9886: a socket-only subset of #7466 combined with #9905, to see how much of CI breaks when only sockets are set nonblocking at creation.

Expected breakage is limited to socket paths that read or write the channel without waiting, e.g. BasicSocket#recv (doRead), UDP receive/send, and possibly jruby-openssl. If this subset turns out manageable, it could land before the rest of #7466.

Ruby 3.0 began to set O_NONBLOCK by default for all IO, to allow more IO
types to enlist in the new IO/fiber scheduling. JRuby has not previously
defaulted to nonblock, but all IO operations against nonblock-compatible
channels does use nonblocking semantics, so all we really need to align
with CRuby is to just set them non-blocking at the NIO level on
creation.

We can only do this when the underlying channel supports
nonblocking semantics, but this commit attempts to set such
channels up properly.

Part of scheduler work in jruby#7459 and related issues.
In jruby#9886 a user reported that because of our locking
of a selectable channels blockingLock while calling select, any
blocking operation against that channel would prevent other
operations from happening. In the case of a socket, which can both
read and write concurrently, this limits functionality.

The base problem is our need to set a blocking socket to
nonblocking for the duration of the select and then safely restore
it to blocking afterwards. If the socket were always nonblocking,
no lock would need to be acquired because the blocking state would
not need to be changed.

The PR in jruby#7466 makes an attempt to mark selectable
channels as nonblocking at create time, matching CRuby 3.0
behavior and opening up the possibility of avoiding the lock. That
is implemented in this patch, which only acquires the lock if the
blocking state needs to be reconfigured.

This appears to fix the example from jruby#9886. It depends
upon the nonblocking changes in jruby#7466.
@headius

headius commented Oct 28, 2026

Copy link
Copy Markdown
Member

Good experiment to try!

route added 2 commits October 28, 2026 15:59
IO#reopen calls setFD(null), which made updateBlockingFromFD throw a NullPointerException (TestIO#test_reopen_nonascii, reopen $stdout spec in spec:regression).
…ng sockets

With sockets nonblocking at creation, BasicSocket#recv returned nil for TCP and "" for UDP, and UDPSocket#recvfrom raised ECONNRESET, whenever no data was ready yet. Wait for the channel to become readable and retry instead.
@route

route commented Oct 28, 2026

Copy link
Copy Markdown
Author

Update: the socket-only split didn't help. CI failed exactly the same tests as #9905 (master is green), so the RubyIO parts of #7466 aren't what breaks things. I force-pushed this branch to be #9905 plus two fixes instead:

  1. OpenFile.updateBlockingFromFD threw a NullPointerException when IO#reopen sets the fd to null (TestIO#test_reopen_nonascii, the "reopen $stdout and $stderr" spec that takes down spec:regression).
  2. Blocking BasicSocket#recv (TCP and UDP) and UDPSocket#recvfrom now wait for the channel to become readable and retry, instead of returning nil/"" or raising ECONNRESET when no data is ready yet (BasicSocket#recv/#send specs, the Net::FTP specs whose fixture server reads with recv, TestResolvDNS).

Those were the only failures on #9905, so if this run goes green the two commits can be pulled into #9905 as is. I couldn't build locally, so this CI run is also the first compile.

@headius

headius commented Oct 28, 2026

Copy link
Copy Markdown
Member

Remaining failures appear to all ve EAGAIN from popen+read during specs on Windows, which has many peculiarities in its version of the IO subsystem. That's likely almost all one fix.

@headius headius added this to the JRuby 10.1.4.0 milestone Oct 28, 2026
On the Java-process popen path (used on Windows) the pipe channels are FileChannels or stream wrappers. Marking them nonblock only enabled PosixShim's emulation, which returns EAGAIN for pipes forever. setFD already syncs the flag for selectable channels.
@route

route commented Oct 29, 2026 •

Copy link
Copy Markdown
Author

Pushed a quick try for the Windows popen failures (b68db99, untested since I don't have Windows): it drops the two openFile.setBlocking(runtime, false) calls #7466 added to setupPopen.

That path is only taken for Java-process popen (Windows), where the pipe channels are a FileChannel or a Channels.newChannel(stream) wrapper, neither selectable. So setBlocking(false) just sets the nonblock flag, and PosixShim.checkForBlocking then reports EAGAIN for them: a pipe never has position < size, and other non-selectable channels always get EAGAIN. The new OpenFile.setFD sync already marks selectable channels nonblocking, so the explicit calls aren't needed. The socket-only run, which didn't have those calls, passed the Windows jobs.

This is Claude quick fix btw, as I mentioned I don't even have windows

@headius

headius commented Oct 30, 2026

Copy link
Copy Markdown
Member

Restarted the tests, let's see how it goes.

@route

route commented Oct 30, 2026

Copy link
Copy Markdown
Author

Looks like intermittent connection errors

@route
route marked this pull request as ready for review October 30, 2026 12:19
@headius

headius commented Oct 30, 2026

Copy link
Copy Markdown
Member

@route Yeah Maven Central has been having trouble lately. The good news is that you seem to have found all the fixes needed for this!

@headius

headius commented Nov 4, 2026

Copy link
Copy Markdown
Member

Consider this for 10.1.4.0. Merge immediately after 10.1.3.0 to let it bake.

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.

2 participants