Skip to content

Make socket "accept" methods dont block fibers (Fiber::Scheduler support) - #9813

Merged
headius merged 5 commits into
jruby:masterfrom
albertalef:make-socket-accepts-dont-block-fibers
Oct 20, 2026
Merged

headius merged 5 commits into
jruby:masterfrom
albertalef:make-socket-accepts-dont-block-fibers

Conversation

@albertalef

Copy link
Copy Markdown
Contributor

The accept (and sysaccept for servers) methods of ServerSocket, TCPServer and UNIXServer were blocking the fiber threads until some client connects, never calling the io_wait.

When a client in another fiber tries to connect on, using the same scheduler, it never run, blocking the thread permanently.

Now they try a nonblocking accept, and while nothing is pending, wait on scheduler io_wait without holding any lock.
The io_wait result is handled as in "rb_io_maybe_wait_readable", and the server closing interrupts a waiting fiber as in #9760. Without a scheduler, the extra cost is one FiberScheduler.current call.

It also fixes 3 existing bugs around these methods:

  • ServerSocket#accept raised IO::EAGAINWaitReadable once IO.select had left the channel nonblocking
  • UNIXServer#accept was returning a not connected socket when another thread or fiber took the connection first
  • The GC was closing the descriptor returned by UNIXServer#sysaccept when it finalized the socket it came from

The new specs passes on MRI 3.4 and 4.0. This test test/async/grpc/client.rb of async-grpc was hanging.
But now it passes together with #9805 of @sampokuokkanen.

@albertalef albertalef changed the title Make socket accepts dont block fibers Make socket accepts dont block fibers (Fiber::Scheduler support) Oct 16, 2026
@albertalef albertalef changed the title Make socket accepts dont block fibers (Fiber::Scheduler support) Make socket "accept" methods dont block fibers (Fiber::Scheduler support) Oct 17, 2026
@albertalef

Copy link
Copy Markdown
Contributor Author
image

This part of the change can look strange, but the reason is that sysaccept creates a temp UNIXSocket through accept, returns only the fd number, and discards the object. When GC collected that object, OpenFile.finalize() closed the caller's fd

@sampokuokkanen

sampokuokkanen commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Great to have other people also work on the fiber scheduler!

Could we cut the tests down a bit?

#9813 (comment)
This works around the actual bug, so I took a stab at fixing it here: #9818.
If that merges, the setAutoclose(false) and the null check in UNIXServer#accept can be dropped from here.

(I'm not a maintainer so take my review with a grain of salt)

@albertalef

albertalef commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@sampokuokkanen, really really thanks for your time reviewing this!

I ran some mutation tests on the code, trying to find holes in my tests, but that increased the number of specs too much. I removed some tests and kept only the main/edge path specs.

About the workaround, i removed the code assuming your fix on #9818. So, without your change, the JRuby some specs on async-grpc will keep failing sometimes, then we also need your PR merged (thanks for the fix!).

@headius

headius commented Oct 19, 2026

Copy link
Copy Markdown
Member

Welcome! I'll add my review and work with @sampokuokkanen to confirm this is good to go.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just minor things in the tests that I noticed. The functionality added looks correct and avoids the scheduler overhead entirely when not current.

The specs might have some room for de-duplication with a shared spec but the content looks good right now.

Comment thread test/jruby/test_socket.rb Resolved
@albertalef

Copy link
Copy Markdown
Contributor Author

@headius Applied your suggestion. And thanks for the review.

Also made a push to solve one ci shard that failed due to a race in a test I added.
(accidentally made the conflict solve with the fix in the same commit, my bad)

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final result looks clean but the history is a bit of a mess due to added and removed tests, review changes, and conflict resolution. To clean that up I'm just going to squash merge.

In the future, consider amending commits that have review problems or dropping commits that are later removed in whole or part. git rebase -i <base branch> is your friend, allowing you to go back and revise or remove individual commits.

@headius
headius merged commit ce85eb4 into jruby:master Oct 20, 2026
@headius headius added this to the JRuby 10.1.3.0 milestone Oct 20, 2026
@albertalef

Copy link
Copy Markdown
Contributor Author

Nice my friend! Thanks for the instructions

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.

3 participants