Skip to content

Commit 931babc

Browse files
committed
Fix select errorfds throwing exceptions
1 parent 0ac4198 commit 931babc

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/EmulatedPosixSupport.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
import java.util.Map;
9191
import java.util.Set;
9292
import java.util.concurrent.TimeUnit;
93-
import java.util.function.Function;
9493
import java.util.logging.Level;
9594

9695
import org.graalvm.nativeimage.ImageInfo;
@@ -425,9 +424,10 @@ public int[] pipeMessage() throws PosixException {
425424
public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeval timeout) throws PosixException {
426425
SelectableChannel[] readChannels = getSelectableChannels(readfds);
427426
SelectableChannel[] writeChannels = getSelectableChannels(writefds);
428-
SelectableChannel[] errChannels = getSelectableChannels(errorfds);
427+
// Java doesn't support any exceptional conditions we could apply on errfds
428+
// TODO raise exception if errfds is not a subset of readfds & writefds?
429429

430-
boolean[] wasBlocking = new boolean[readChannels.length + writeChannels.length + errChannels.length];
430+
boolean[] wasBlocking = new boolean[readChannels.length + writeChannels.length];
431431
int i = 0;
432432

433433
for (SelectableChannel channel : readChannels) {
@@ -436,25 +436,19 @@ public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeva
436436
for (SelectableChannel channel : writeChannels) {
437437
wasBlocking[i++] = channel.isBlocking();
438438
}
439-
for (SelectableChannel channel : errChannels) {
440-
wasBlocking[i++] = channel.isBlocking();
441-
}
439+
440+
final int readOps = SelectionKey.OP_READ | SelectionKey.OP_ACCEPT;
441+
final int writeOps = SelectionKey.OP_WRITE;
442442

443443
try (Selector selector = Selector.open()) {
444444
for (SelectableChannel channel : readChannels) {
445445
channel.configureBlocking(false);
446-
channel.register(selector, (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT) & channel.validOps());
446+
channel.register(selector, readOps & channel.validOps());
447447
}
448448

449449
for (SelectableChannel channel : writeChannels) {
450450
channel.configureBlocking(false);
451-
channel.register(selector, SelectionKey.OP_WRITE);
452-
}
453-
454-
for (SelectableChannel channel : errChannels) {
455-
// TODO(fa): not sure if these ops are representing "exceptional condition pending"
456-
channel.configureBlocking(false);
457-
channel.register(selector, SelectionKey.OP_CONNECT);
451+
channel.register(selector, writeOps);
458452
}
459453

460454
// IMPORTANT: The meaning of the timeout value is slightly different: 'timeout == 0.0'
@@ -478,9 +472,9 @@ public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeva
478472
int selected = useSelectNow ? selector.selectNow() : selector.select(timeoutMs);
479473

480474
// remove non-selected channels from given lists
481-
boolean[] resReadfds = createSelectedMap(readfds, readChannels, selector, SelectionKey::isReadable);
482-
boolean[] resWritefds = createSelectedMap(writefds, writeChannels, selector, SelectionKey::isWritable);
483-
boolean[] resErrfds = createSelectedMap(errorfds, errChannels, selector, key -> key.isAcceptable() || key.isConnectable());
475+
boolean[] resReadfds = createSelectedMap(readfds, readChannels, selector, readOps);
476+
boolean[] resWritefds = createSelectedMap(writefds, writeChannels, selector, writeOps);
477+
boolean[] resErrfds = new boolean[errorfds.length];
484478

485479
assert selected == countSelected(resReadfds) + countSelected(resWritefds) + countSelected(resErrfds);
486480
return new SelectResult(resReadfds, resWritefds, resErrfds);
@@ -499,23 +493,18 @@ public SelectResult select(int[] readfds, int[] writefds, int[] errorfds, Timeva
499493
channel.configureBlocking(true);
500494
}
501495
}
502-
for (SelectableChannel channel : errChannels) {
503-
if (wasBlocking[i++]) {
504-
channel.configureBlocking(true);
505-
}
506-
}
507496
} catch (IOException e) {
508497
// We didn't manage to restore the blocking status, ignore
509498
}
510499
}
511500
}
512501

513-
private static boolean[] createSelectedMap(int[] fds, SelectableChannel[] channels, Selector selector, Function<SelectionKey, Boolean> selectedPredicate) {
502+
private static boolean[] createSelectedMap(int[] fds, SelectableChannel[] channels, Selector selector, int op) {
514503
boolean[] result = new boolean[fds.length];
515504
for (int i = 0; i < channels.length; i++) {
516505
SelectableChannel channel = channels[i];
517506
SelectionKey selectionKey = channel.keyFor(selector);
518-
result[i] = selectedPredicate.apply(selectionKey);
507+
result[i] = (selectionKey.readyOps() & op) != 0;
519508
}
520509
return result;
521510
}

0 commit comments

Comments
 (0)