Skip to content

Commit 41d26b9

Browse files
committed
Fixed detection of posix backend type in PosixResources
1 parent 893b9fd commit 41d26b9

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ public final class EmulatedPosixSupport extends PosixResources {
255255
private int currentUmask = 0022;
256256
private boolean hasDefaultUmask = true;
257257

258-
public EmulatedPosixSupport(PythonContext context) {
258+
public EmulatedPosixSupport(PythonContext context, boolean useNfiForSocketFd) {
259+
super(useNfiForSocketFd);
259260
this.context = context;
260261
setEnv(context.getEnv());
261262
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private static void loadLibrary(NFIPosixSupport posix) {
268268
try {
269269
posix.nfiLibrary = posix.context.getEnv().parseInternal(loadSrc).call();
270270
} catch (Throwable e) {
271-
throw CompilerDirectives.shouldNotReachHere(e);
271+
throw CompilerDirectives.shouldNotReachHere("Unable to load native posix support library", e);
272272
}
273273
}
274274

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class PosixResources extends PosixSupport {
8383
private final List<Process> children;
8484
private final Map<String, Integer> inodes;
8585
private int inodeCnt = 0;
86+
private boolean useNfiForSocketFd;
8687

8788
private static class ProcessGroup extends Process {
8889
private final List<Process> children;
@@ -186,7 +187,7 @@ void setNewChannel(OutputStream outputStream) {
186187
}
187188
}
188189

189-
public PosixResources() {
190+
public PosixResources(boolean useNfiForSocketFd) {
190191
files = Collections.synchronizedSortedMap(new TreeMap<>());
191192
filePaths = Collections.synchronizedMap(new HashMap<>());
192193
children = Collections.synchronizedList(new ArrayList<>());
@@ -208,6 +209,7 @@ public PosixResources() {
208209
children.add(new ProcessGroup(children)); // PID 0 is special, and refers to all processes
209210
// in the process group
210211
inodes = new HashMap<>();
212+
this.useNfiForSocketFd = useNfiForSocketFd;
211213
}
212214

213215
@TruffleBoundary
@@ -340,22 +342,23 @@ public void close(int fd) {
340342

341343
@TruffleBoundary
342344
public int openSocket(PSocket socket, PythonContext context) {
343-
Object posixSupport = context.getPosixSupport();
344345
synchronized (files) {
345346
int fd;
346-
if (posixSupport == this) {
347+
if (!useNfiForSocketFd) {
347348
// using emulated backend
348349
fd = nextFreeFd();
349350
} else {
350351
// using nfi backend
351352
try {
353+
Object posixSupport = context.getPosixSupport();
352354
PosixSupportLibrary posixLib = PosixSupportLibrary.getUncached();
353355
if (nativeFdForSockets == -1) {
354356
int[] fds = posixLib.pipe(posixSupport);
355-
posixLib.close(posixSupport, fds[1]);
356357
nativeFdForSockets = fds[0];
358+
fd = fds[1];
359+
} else {
360+
fd = posixLib.dup(posixSupport, nativeFdForSockets);
357361
}
358-
fd = posixLib.dup(posixSupport, nativeFdForSockets);
359362
} catch (PosixException e) {
360363
throw CompilerDirectives.shouldNotReachHere("Unable to assign native fd to a socket", e);
361364
}
@@ -373,11 +376,10 @@ public void closeSocket(PSocket socket, PythonContext context) {
373376
}
374377
socket.setFileno(-1);
375378
close(fd);
376-
Object posixSupport = context.getPosixSupport();
377-
if (posixSupport != this) {
379+
if (useNfiForSocketFd) {
378380
// using nfi backend
379381
try {
380-
PosixSupportLibrary.getUncached().close(posixSupport, fd);
382+
PosixSupportLibrary.getUncached().close(context.getPosixSupport(), fd);
381383
} catch (PosixException e) {
382384
throw CompilerDirectives.shouldNotReachHere("Unable to close native fd", e);
383385
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,30 +568,30 @@ private void initalizePosixSupport() {
568568
// The resources field will be removed once all posix builtins go through PosixSupport
569569
switch (option) {
570570
case "java":
571-
result = resources = new EmulatedPosixSupport(this);
571+
result = resources = new EmulatedPosixSupport(this, false);
572572
break;
573573
case "native":
574574
case "llvm":
575575
// TODO this condition will be moved into a factory method in NFIPosixBackend
576576
// for now it's here because we still need to expose the emulated backend as
577577
// 'resources'
578578
if (ImageInfo.inImageBuildtimeCode()) {
579-
EmulatedPosixSupport emulatedPosixSupport = new EmulatedPosixSupport(this);
579+
EmulatedPosixSupport emulatedPosixSupport = new EmulatedPosixSupport(this, false);
580580
NFIPosixSupport nativePosixSupport = new NFIPosixSupport(this, option);
581581
result = new ImageBuildtimePosixSupport(nativePosixSupport, emulatedPosixSupport);
582582
resources = emulatedPosixSupport;
583583
} else if (ImageInfo.inImageRuntimeCode()) {
584584
NFIPosixSupport nativePosixSupport = new NFIPosixSupport(this, option);
585585
result = new ImageBuildtimePosixSupport(nativePosixSupport, null);
586-
resources = new EmulatedPosixSupport(this);
586+
resources = new EmulatedPosixSupport(this, true);
587587
resources.setEnv(env);
588588
} else {
589589
if (!getOption(PythonOptions.RunViaLauncher)) {
590590
writeWarning("Native Posix backend is not fully supported when embedding. For example, standard I/O always uses file " +
591591
"descriptors 0, 1 and 2 regardless of stream redirection specified in Truffle environment");
592592
}
593593
result = new NFIPosixSupport(this, option);
594-
resources = new EmulatedPosixSupport(this);
594+
resources = new EmulatedPosixSupport(this, true);
595595
resources.setEnv(env);
596596
}
597597
break;

0 commit comments

Comments
 (0)