Skip to content
Draft
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
17 changes: 15 additions & 2 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,15 @@ private IRubyObject initializeCommon(ThreadContext context, int fileno, IRubyObj
}
}
}

// set to nonblocking if possible (Ruby 3.0 change for IO/fiber scheduling)
if (fd.chSelect != null) {
try {
fd.chSelect.configureBlocking(false);
} catch (IOException ioe) {
// ignore, can't set nonblocking
}
}
} else {
fd = runtime.getFilenoUtil().getWrapperFromFileno(fileno);

Expand Down Expand Up @@ -4593,6 +4602,8 @@ private void setupPopen(ThreadContext context, ModeFlags modes, POpenProcess pro
openFile.setMode(modes.getOpenFileFlags() | OpenFile.SYNC);
openFile.setProcess(process);

Ruby runtime = context.runtime;

if (openFile.isReadable()) {
Channel inChannel;
if (process.getInput() != null) {
Expand All @@ -4603,9 +4614,10 @@ private void setupPopen(ThreadContext context, ModeFlags modes, POpenProcess pro
inChannel = Channels.newChannel(process.getInputStream());
}

ChannelFD main = new ChannelFD(inChannel, context.runtime.getPosix(), context.runtime.getFilenoUtil());
ChannelFD main = new ChannelFD(inChannel, runtime.getPosix(), runtime.getFilenoUtil());

openFile.setFD(main);
openFile.setBlocking(runtime, false);
}

if (openFile.isWritable() && process.hasOutput()) {
Expand All @@ -4617,7 +4629,7 @@ private void setupPopen(ThreadContext context, ModeFlags modes, POpenProcess pro
outChannel = Channels.newChannel(process.getOutputStream());
}

ChannelFD pipe = new ChannelFD(outChannel, context.runtime.getPosix(), context.runtime.getFilenoUtil());
ChannelFD pipe = new ChannelFD(outChannel, runtime.getPosix(), runtime.getFilenoUtil());

// if also readable, attach as tied IO; otherwise, primary IO
if (openFile.isReadable()) {
Expand All @@ -4628,6 +4640,7 @@ private void setupPopen(ThreadContext context, ModeFlags modes, POpenProcess pro
setInstanceVariable("@tied_io_for_writing", writeIO);
} else {
openFile.setFD(pipe);
openFile.setBlocking(runtime, false);
}
}
}
Expand Down
158 changes: 83 additions & 75 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2453,97 +2453,105 @@ public boolean select(Channel channel, OpenFile fptr, int ops, long timeout) {
public int selectReadyOps(Channel channel, OpenFile fptr, int ops, long timeout) {
// Use selectables but only if they're not associated with a file (which has odd select semantics)
ChannelFD fd = fptr == null ? null : fptr.fd();
if (channel instanceof SelectableChannel && fd != null) {
SelectableChannel selectable = (SelectableChannel)channel;

if (channel instanceof SelectableChannel selectable && fd != null) {
// ensure we have fptr locked, but release it to avoid deadlock
boolean locked = false;
if (fptr != null) {
locked = fptr.lock();
fptr.unlock();
}
try {
synchronized (selectable.blockingLock()) {
boolean oldBlocking = selectable.isBlocking();
boolean locked = fptr.lock();
fptr.unlock();

SelectionKey key;
try {
selectable.configureBlocking(false);
try {
if (!selectable.isBlocking()) {
// channel is nonblocking, proceed without lock
return doBlockingSelect(fptr, ops, timeout, selectable);
} else {
// channel is blocking, ensure we restore that state after select
synchronized (selectable.blockingLock()) {
// acquire again in case it changed outside of lock
boolean oldBlocking = selectable.isBlocking();
try {
selectable.configureBlocking(false);

if (fptr != null) fptr.addBlockingThread(this);
currentSelector = getRuntime().getSelectorPool().get(selectable.provider());
return doBlockingSelect(fptr, ops, timeout, selectable);
} finally {
// go back to previous blocking state on the selectable
try {
selectable.configureBlocking(oldBlocking);
} catch (Exception e) {
// ignore
}
}
}
}
} catch (IOException ioe) {
throw getRuntime().newIOErrorFromException(ioe);
} finally {
// restore lock state
fptr.lock();
if (locked) fptr.unlock();

key = selectable.register(currentSelector, ops);
}
} else {
// can't select, so report everything asked for as ready, like select(2) does for files
return ops;
}
}

beforeBlockingCall(getContext());
int result;
private int doBlockingSelect(OpenFile fptr, int ops, long timeout, SelectableChannel selectable) throws IOException {
SelectionKey key;
try {
if (fptr != null) fptr.addBlockingThread(this);
currentSelector = getRuntime().getSelectorPool().get(selectable.provider());

if (timeout < 0) {
result = currentSelector.select();
} else if (timeout == 0) {
result = currentSelector.selectNow();
} else {
result = currentSelector.select(timeout);
}
key = selectable.register(currentSelector, ops);

// check for thread events, in case we've been woken up to die
pollThreadEvents();
beforeBlockingCall(getContext());
int result;

if (result == 1) {
Set<SelectionKey> keySet = currentSelector.selectedKeys();
if (timeout < 0) {
result = currentSelector.select();
} else if (timeout == 0) {
result = currentSelector.selectNow();
} else {
result = currentSelector.select(timeout);
}

if (keySet.contains(key) && key.isValid()) {
// jnr-enxio's PollSelector reports character devices as selected
// without setting readyOps, so treat that as ready for all of ops
int readyOps = key.readyOps() & ops;
return readyOps != 0 ? readyOps : ops;
}
}
// check for thread events, in case we've been woken up to die
pollThreadEvents();

return 0;
} catch (IOException ioe) {
throw getRuntime().newIOErrorFromException(ioe);
} finally {
// Note: I don't like ignoring these exceptions, but it's
// unclear how likely they are to happen or what damage we
// might do by ignoring them. Note that the pieces are separate
// so that we can ensure one failing does not affect the others
// running.

// shut down and null out the selector
try {
if (currentSelector != null) {
getRuntime().getSelectorPool().put(currentSelector);
}
} catch (Exception e) {
// ignore
} finally {
currentSelector = null;
}
if (result == 1) {
Set<SelectionKey> keySet = currentSelector.selectedKeys();

// remove this thread as a blocker against the given IO
if (fptr != null) fptr.removeBlockingThread(this);
if (keySet.contains(key) && key.isValid()) {
// jnr-enxio's PollSelector reports character devices as selected
// without setting readyOps, so treat that as ready for all of ops
int readyOps = key.readyOps() & ops;
return readyOps != 0 ? readyOps : ops;
}
}

// go back to previous blocking state on the selectable
try {
selectable.configureBlocking(oldBlocking);
} catch (Exception e) {
// ignore
}
return 0;
} finally {
// Note: I don't like ignoring these exceptions, but it's
// unclear how likely they are to happen or what damage we
// might do by ignoring them. Note that the pieces are separate
// so that we can ensure one failing does not affect the others
// running.

// clear thread state from blocking call
afterBlockingCall();
}
// shut down and null out the selector
try {
if (currentSelector != null) {
getRuntime().getSelectorPool().put(currentSelector);
}
} catch (Exception e) {
// ignore
} finally {
if (fptr != null) {
fptr.lock();
if (locked) fptr.unlock();
}
currentSelector = null;
}
} else {
// can't select, so report everything asked for as ready, like select(2) does for files
return ops;

// remove this thread as a blocker against the given IO
if (fptr != null) fptr.removeBlockingThread(this);

// clear thread state from blocking call
afterBlockingCall();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,7 @@ protected void initSocket(ChannelFD fd) {
MakeOpenFile();

openFile.setFD(fd);
openFile.setBlocking(getRuntime(), false);
openFile.setMode(OpenFile.READWRITE | OpenFile.SYNC);

// see rsock_init_sock in MRI; sockets are initialized to binary
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyServerSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ public static IRubyObject doAccept(RubySocket sock, ThreadContext context, boole
throw runtime.newErrnoEAGAINReadableError("accept(2) would block");
}

socket.configureBlocking(false);

RubySocket rubySocket = new RubySocket(runtime, Access.getClass(context, "Socket"));
rubySocket.initFromServer(runtime, sock, socket);

Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public IRubyObject accept(ThreadContext context) {
if (connected == null) continue;

connected.finishConnect();
connected.configureBlocking(false);

// otherwise one key has been selected (ours) so we get the channel and hand it off
return initAcceptedSocket(context, socket, connected);
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/java/org/jruby/ext/socket/RubyTCPSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ private SocketChannel attemptConnect(ThreadContext context, IRubyObject host, St
context.pollThreadEvents();
}

channel.configureBlocking(true);

return channel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ protected void init_sock(Ruby runtime, Channel channel, String path) {
ModeFlags modes = newModeFlags(runtime, ModeFlags.RDWR);

openFile.setFD(newChannelFD(runtime, channel));
openFile.setBlocking(runtime, false);
openFile.setMode(modes.getOpenFileFlags());
openFile.setSync(true);
openFile.setPath(path);
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/jruby/util/io/OpenFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,18 @@ public boolean IS_PREP_STDIO() {

public void setFD(ChannelFD fd) {
this.fd = fd;
updateBlockingFromFD(fd);
}

public void setChannel(Channel fd) {
this.fd = new ChannelFD(fd, runtime.getPosix(), runtime.getFilenoUtil());
updateBlockingFromFD(this.fd);
}

private void updateBlockingFromFD(ChannelFD fd) {
if (fd.chSelect != null ) {
this.nonblock = !fd.chSelect.isBlocking();
}
}

public int getMode() {
Expand Down
Loading