Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to nonblocking whenever possible #7466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,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 @@ -4173,6 +4182,7 @@ private void setupPopen(final Ruby runtime, ModeFlags modes, POpenProcess proces
ChannelFD main = new ChannelFD(inChannel, runtime.getPosix(), runtime.getFilenoUtil());

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

if (openFile.isWritable() && process.hasOutput()) {
Expand All @@ -4195,6 +4205,7 @@ private void setupPopen(final Ruby runtime, ModeFlags modes, POpenProcess proces
setInstanceVariable("@tied_io_for_writing", writeIO);
} else {
openFile.setFD(pipe);
openFile.setBlocking(runtime, false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,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
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,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 @@ -192,10 +192,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