Skip to content

Commit b7c827a

Browse files
committed
Prevent endless loop in test.support.reap_children()
1 parent 28a3de1 commit b7c827a

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PosixModuleBuiltins.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,13 +1449,8 @@ PTuple waitpid(VirtualFrame frame, int pid, int options) {
14491449
int exitStatus = getResources().waitpid(pid);
14501450
return factory().createTuple(new Object[]{pid, exitStatus});
14511451
} else if (options == WNOHANG) {
1452-
int exitStatus = getResources().exitStatus(pid);
1453-
if (exitStatus == Integer.MIN_VALUE) {
1454-
// not terminated, yet, we should return 0
1455-
return factory().createTuple(new Object[]{0, 0});
1456-
} else {
1457-
return factory().createTuple(new Object[]{pid, exitStatus});
1458-
}
1452+
int[] res = getResources().exitStatus(pid);
1453+
return factory().createTuple(new Object[]{res[0], res[1]});
14591454
} else {
14601455
throw raise(PythonBuiltinClassType.NotImplementedError, "Only 0 or WNOHANG are supported for waitpid");
14611456
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,13 +458,23 @@ public int waitpid(int pid) throws IndexOutOfBoundsException, InterruptedExcepti
458458
}
459459

460460
@TruffleBoundary(allowInlining = true)
461-
public int exitStatus(int pid) throws IndexOutOfBoundsException {
462-
Process process = getChild(pid);
463-
if (process.isAlive()) {
464-
return Integer.MIN_VALUE;
461+
public int[] exitStatus(int pid) throws IndexOutOfBoundsException {
462+
if (pid == -1) {
463+
for (int childPid = 1; childPid < children.size(); ++childPid) {
464+
Process child = children.get(childPid);
465+
if (child != null && !child.isAlive()) {
466+
children.set(childPid, null);
467+
return new int[]{childPid, child.exitValue()};
468+
}
469+
}
465470
} else {
466-
return process.exitValue();
471+
Process process = getChild(pid);
472+
if (!process.isAlive()) {
473+
children.set(pid, null);
474+
return new int[]{pid, process.exitValue()};
475+
}
467476
}
477+
return new int[]{0, 0};
468478
}
469479

470480
@TruffleBoundary(allowInlining = true)

0 commit comments

Comments
 (0)