Skip to content

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

Open
albertalef wants to merge 2 commits into
jruby:masterfrom
albertalef:make-socket-accepts-dont-block-fibers
Open

albertalef wants to merge 2 commits into
jruby:masterfrom
albertalef:make-socket-accepts-dont-block-fibers

Conversation

@albertalef

Copy link
Copy Markdown

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
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

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