Skip to content

Commit

Permalink
Merge pull request jruby#7967 from headius/socket_rst_fixes
Browse files Browse the repository at this point in the history
Propagate read errors in IO backend
  • Loading branch information
headius authored Oct 18, 2023
2 parents 2cb723c + 51464f6 commit f72776a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ public boolean select(Channel channel, OpenFile fptr, int ops, long timeout) {
if (result == 1) {
Set<SelectionKey> keySet = currentSelector.selectedKeys();

if (keySet.iterator().next() == key) {
if (keySet.contains(key) && key.isValid()) {
return true;
}
}
Expand Down
22 changes: 10 additions & 12 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.net.BindException;
import java.net.PortUnreachableException;
import java.net.SocketException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.NonReadableChannelException;
import java.nio.channels.NonWritableChannelException;
Expand Down Expand Up @@ -240,7 +241,7 @@ public static Errno errnoFromException(Throwable t) {
// Try specific exception types by rethrowing and catching.
try {
throw t;
} catch (FileNotFoundException fnfe) {
} catch (FileNotFoundException | NoSuchFileException fnfe) {
return Errno.ENOENT;
} catch (EOFException eofe) {
return Errno.EPIPE;
Expand All @@ -254,15 +255,11 @@ public static Errno errnoFromException(Throwable t) {
return Errno.EEXIST;
} catch (FileSystemLoopException fsle) {
return Errno.ELOOP;
} catch (NoSuchFileException nsfe) {
return Errno.ENOENT;
} catch (NotDirectoryException nde) {
return Errno.ENOTDIR;
} catch (AccessDeniedException ade) {
return Errno.EACCES;
} catch (DirectoryNotEmptyException dnee) {
return errnoFromMessage(dnee);
} catch (BindException be) {
} catch (IOException be) {
return errnoFromMessage(be);
} catch (NotYetConnectedException nyce) {
return Errno.ENOTCONN;
Expand All @@ -271,12 +268,6 @@ public static Errno errnoFromException(Throwable t) {
return Errno.EINVAL;
} catch (IllegalArgumentException nrce) {
return Errno.EINVAL;
} catch (IOException ioe) {
String message = ioe.getMessage();
// Raised on Windows for process launch with missing file
if (message.endsWith("The system cannot find the file specified")) {
return Errno.ENOENT;
}
} catch (Throwable t2) {
// fall through
}
Expand All @@ -302,6 +293,7 @@ private static Errno errnoFromMessage(Throwable t) {
return Errno.ECONNABORTED;
case "Broken pipe":
return Errno.EPIPE;
case "Connection reset":
case "Connection reset by peer":
case "An existing connection was forcibly closed by the remote host":
return Errno.ECONNRESET;
Expand Down Expand Up @@ -335,7 +327,13 @@ private static Errno errnoFromMessage(Throwable t) {
case "Protocol family not supported":
return Errno.EPFNOSUPPORT;
}

// Raised on Windows for process launch with missing file
if (errorMessage.endsWith("The system cannot find the file specified")) {
return Errno.ENOENT;
}
}

return null;
}

Expand Down
6 changes: 4 additions & 2 deletions core/src/main/java/org/jruby/util/io/OpenFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -1298,10 +1298,12 @@ public int fillbuf(ThreadContext context) {
r = readInternal(context, this, fd, rbuf.ptr, 0, rbuf.capa);

if (r < 0) {
if (waitReadable(context, fd)) {
Errno errno = posix.getErrno();
if (errno == Errno.EAGAIN || errno == Errno.EWOULDBLOCK
&& waitReadable(context, fd)) {
continue retry;
}
throw context.runtime.newErrnoFromErrno(posix.getErrno(), "channel: " + fd + (pathv != null ? " " + pathv : ""));
throw context.runtime.newErrnoFromErrno(errno, "channel: " + fd + (pathv != null ? " " + pathv : ""));
}
break;
}
Expand Down

0 comments on commit f72776a

Please sign in to comment.