Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyBasicSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import jnr.posix.Timeval;
import jnr.posix.POSIX;
import jnr.unixsocket.UnixSocketAddress;
import org.jruby.FiberScheduler;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBasicObject;
Expand Down Expand Up @@ -967,6 +968,23 @@ protected void initSocket(ChannelFD fd) {
setAscii8bitBinmode();
}

/**
* Wait through the fiber scheduler for a connection, after a nonblocking accept found none pending.
* Call it without holding any lock on this server, since io_wait runs sibling fibers that may accept on
* or close it. Returns when the caller should try accepting again.
*/
// MRI: rsock_s_accept on EAGAIN, which waits with rb_io_maybe_wait_readable and RUBY_IO_TIMEOUT_DEFAULT
protected void schedulerWaitAcceptable(ThreadContext context, IRubyObject scheduler) {
IRubyObject result = FiberScheduler.ioWaitReadable(context, scheduler, this);

if (result == context.fals) {
throw RubyIOTimeoutError.newIOTimeoutError(context.runtime, "Timed out waiting for IO to become readable!").toThrowable();
}

// no events ready, so MRI gives up with the EAGAIN it got from accept(2)
if (result.isNil() || toInt(context, result) == 0) throw context.runtime.newErrnoFromErrno(Errno.EAGAIN, "accept(2)");
}

private Channel getOpenChannel() {
return getOpenFileChecked().channel();
}
Expand Down
28 changes: 27 additions & 1 deletion core/src/main/java/org/jruby/ext/socket/RubyServerSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
package org.jruby.ext.socket;

import jnr.constants.platform.Sock;
import org.jruby.FiberScheduler;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.anno.JRubyClass;
Expand All @@ -49,6 +51,7 @@
import java.nio.channels.Channel;
import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

Expand Down Expand Up @@ -112,7 +115,30 @@ public IRubyObject bind(ThreadContext context, IRubyObject addr, IRubyObject bac

@JRubyMethod()
public IRubyObject accept(ThreadContext context) {
return doAccept(this, context, true);
IRubyObject scheduler = FiberScheduler.current(context);
if (scheduler != null) return schedulerAccept(context, scheduler);

while (true) {
// :wait_readable when another thread or fiber has the channel nonblocking, so wait instead of raising EAGAIN
IRubyObject result = doAccept(this, context, false);
if (result instanceof RubyArray) return result;

if (!context.getThread().select(this, SelectionKey.OP_ACCEPT)) {
// we were woken up without being selected...poll for thread events and go back to sleep
context.pollThreadEvents();
}
}
}

// MRI: rsock_s_accept, which retries a nonblocking accept(2) whenever io_wait reports the server readable
private IRubyObject schedulerAccept(ThreadContext context, IRubyObject scheduler) {
while (true) {
// [Socket, Addrinfo], or :wait_readable with the channel's blocking lock already released
IRubyObject result = doAcceptNonblock(this, context, false);
if (result instanceof RubyArray) return result;

schedulerWaitAcceptable(context, scheduler);
}
}

@JRubyMethod()
Expand Down
57 changes: 48 additions & 9 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package org.jruby.ext.socket;

import org.jruby.FiberScheduler;
import org.jruby.Ruby;
import org.jruby.RubyBasicObject;
import org.jruby.RubyClass;
Expand Down Expand Up @@ -145,6 +146,9 @@ public IRubyObject accept(ThreadContext context) {
RubyTCPSocket socket = new RubyTCPSocket(context.runtime, Access.getClass(context, "TCPSocket"));

try {
IRubyObject scheduler = FiberScheduler.current(context);
if (scheduler != null) return initAcceptedSocket(context, socket, schedulerAccept(context, scheduler));

RubyThread thread = context.getThread();

while (true) {
Expand All @@ -160,16 +164,8 @@ public IRubyObject accept(ThreadContext context) {

connected.finishConnect();

// Force the client socket to be blocking
synchronized (connected.blockingLock()) {
connected.configureBlocking(false);
connected.configureBlocking(true);
}

// otherwise one key has been selected (ours) so we get the channel and hand it off
socket.initSocket(newChannelFD(context.runtime, connected));

return socket;
return initAcceptedSocket(context, socket, connected);
}
}

Expand All @@ -178,6 +174,46 @@ public IRubyObject accept(ThreadContext context) {
}
}

private static RubyTCPSocket initAcceptedSocket(ThreadContext context, RubyTCPSocket socket, SocketChannel connected) throws IOException {
// Force the client socket to be blocking
synchronized (connected.blockingLock()) {
connected.configureBlocking(false);
connected.configureBlocking(true);
}

socket.initSocket(newChannelFD(context.runtime, connected));

return socket;
}

// MRI: rsock_s_accept, which retries a nonblocking accept(2) whenever io_wait reports the server readable
private SocketChannel schedulerAccept(ThreadContext context, IRubyObject scheduler) throws IOException {
while (true) {
ServerSocketChannel ssc = getServerSocketChannel();
SocketChannel connected;

synchronized (ssc.blockingLock()) {
boolean oldBlocking = ssc.isBlocking();

try {
ssc.configureBlocking(false);
connected = ssc.accept();
} finally {
try {ssc.configureBlocking(oldBlocking);} catch (IOException ioe) {}
}
}

if (connected != null) {
connected.finishConnect();

return connected;
}

// outside the blocking lock, which a sibling fiber may need to accept on this server
schedulerWaitAcceptable(context, scheduler);
}
}

@JRubyMethod(name = "accept_nonblock")
public IRubyObject accept_nonblock(ThreadContext context) {
return accept_nonblock(context, context.runtime, true);
Expand Down Expand Up @@ -235,6 +271,9 @@ public IRubyObject accept_nonblock(ThreadContext context, Ruby runtime, boolean
@JRubyMethod(name = "sysaccept")
public IRubyObject sysaccept(ThreadContext context) {
try {
IRubyObject scheduler = FiberScheduler.current(context);
if (scheduler != null) return asFixnum(context, newChannelFD(context.runtime, schedulerAccept(context, scheduler)).bestFileno());

RubyThread thread = context.getThread();

while (true) {
Expand Down
33 changes: 33 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyUNIXServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jnr.unixsocket.UnixServerSocketChannel;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import org.jruby.FiberScheduler;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.anno.JRubyClass;
Expand Down Expand Up @@ -83,6 +84,9 @@ public IRubyObject accept(ThreadContext context) {
}

private UnixSocketChannel acceptChannel(ThreadContext context) {
IRubyObject scheduler = FiberScheduler.current(context);
if (scheduler != null) return schedulerAcceptChannel(context, scheduler);

try {

while (true) { // select loop to allow interrupting
Expand All @@ -106,6 +110,35 @@ private UnixSocketChannel acceptChannel(ThreadContext context) {
}
}

// MRI: rsock_s_accept, which retries a nonblocking accept(2) whenever io_wait reports the server readable
private UnixSocketChannel schedulerAcceptChannel(ThreadContext context, IRubyObject scheduler) {
while (true) {
SelectableChannel selectable = (SelectableChannel) getChannel();
UnixSocketChannel socketChannel;

synchronized (selectable.blockingLock()) {
boolean oldBlocking = selectable.isBlocking();

try {
selectable.configureBlocking(false);
socketChannel = ((UnixServerSocketChannel) selectable).accept();
} catch (IOException ioe) {
if (!"accept failed: Resource temporarily unavailable".equals(ioe.getMessage())) {
throw context.runtime.newIOErrorFromException(ioe);
}
socketChannel = null;
} finally {
try {selectable.configureBlocking(oldBlocking);} catch (IOException ioe) {}
}
}

if (socketChannel != null) return socketChannel;

// outside the blocking lock, which a sibling fiber may need to accept on this server
schedulerWaitAcceptable(context, scheduler);
}
}

@JRubyMethod
public IRubyObject accept_nonblock(ThreadContext context) {
return accept_nonblock(context, context.runtime, true);
Expand Down
112 changes: 112 additions & 0 deletions spec/ruby/library/socket/tcpserver/accept_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,115 @@
end
end
end

describe "TCPServer#accept with Fiber scheduler" do
require_relative '../../../core/fiber/fixtures/scheduler'

before :each do
@server = TCPServer.new("127.0.0.1", 0)
@port = @server.connect_address.ip_port
Fiber.set_scheduler(FiberSpecs::LoggingScheduler.new)
end

# The server cannot be closed while a fiber waits on it, and the scheduler
# leaves the fiber parked inside #io_wait, so end its accept before closing:
# #io_wait returning false makes accept raise IO::TimeoutError.
after :each do
begin
@fiber.resume(false) if @fiber&.alive?
rescue IO::TimeoutError
end
Fiber.set_scheduler(nil)
@socket&.close
@client&.close
@server.close unless @server.closed?
end

it "calls the scheduler's #io_wait with IO::READABLE and no timeout" do
@fiber = Fiber.new(blocking: false) { @server.accept }
@fiber.resume

Fiber.scheduler.events.should == [
{ event: :io_wait, fiber: @fiber, args: [@server, IO::READABLE, nil] }
]
end

it "returns the connection once #io_wait reports the server readable" do
@fiber = Fiber.new(blocking: false) { @server.accept }
@fiber.resume
@client = TCPSocket.new("127.0.0.1", @port)
IO.select([@server])

@socket = @fiber.resume(IO::READABLE)
@socket.should.instance_of?(TCPSocket)
@client.write("hello")
@socket.read(5).should == "hello"
end

it "accepts a pending connection without calling #io_wait" do
@client = TCPSocket.new("127.0.0.1", @port)
IO.select([@server])

@fiber = Fiber.new(blocking: false) { @server.accept }
@socket = @fiber.resume
@socket.should.instance_of?(TCPSocket)
Fiber.scheduler.events.should == []
end

it "calls #io_wait again when it reports the server readable but no connection is pending" do
@fiber = Fiber.new(blocking: false) { @server.accept }
@fiber.resume
@fiber.resume(IO::READABLE)

io_wait = { event: :io_wait, fiber: @fiber, args: [@server, IO::READABLE, nil] }
Fiber.scheduler.events.should == [io_wait, io_wait]
end

it "raises Errno::EAGAIN when #io_wait returns nil" do
@fiber = Fiber.new(blocking: false) { @server.accept }
@fiber.resume

-> { @fiber.resume(nil) }.should.raise(Errno::EAGAIN, /accept\(2\)/) { |e| e.should.instance_of?(Errno::EAGAIN) }
end

it "raises IO::TimeoutError when #io_wait returns false" do
@fiber = Fiber.new(blocking: false) { @server.accept }
@fiber.resume

-> { @fiber.resume(false) }.should.raise(IO::TimeoutError, "Timed out waiting for IO to become readable!")
end

ruby_version_is "4.0" do
it "calls the scheduler's #fiber_interrupt with an IOError when the server is closed while it waits" do
@fiber = Fiber.new(blocking: false) { @server.accept }
@fiber.resume
closer = Fiber.new(blocking: false) { @server.close }
closer.resume

event = Fiber.scheduler.events.last
event[:event].should == :fiber_interrupt
event[:fiber].should == closer
event[:args][0].should == @fiber
event[:args][1].should.instance_of?(IOError)
event[:args][1].message.should == "stream closed in another thread"

closer.resume
-> { @fiber.resume(IO::READABLE) while @fiber.alive? }.should.raise(IOError)
end
end

it "does not call the scheduler if the fiber is blocking" do
# the root fiber is blocking; the client connects once it waits in #accept
main = Thread.current
connector = Thread.new do
Thread.pass until main.status == "sleep"
TCPSocket.new("127.0.0.1", @port)
end

@socket = @server.accept
@socket.should.instance_of?(TCPSocket)
Fiber.scheduler.events.should == []
ensure
@client = connector&.value
end
end
Loading