Skip to content

Commit a25fae2

Browse files
committed
[GH-526] Reading named pipes with Java posix fails with Illegal seek
1 parent 436a0d9 commit a25fae2

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_posix.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@ def test_pipe(self):
173173
os.close(fd1)
174174
os.close(fd2)
175175

176+
@unittest.skipIf(sys.platform != 'linux', 'mkfifo is a linux command')
177+
def test_seek_pipe(self):
178+
new_file_path = './myscript.sh'
179+
with io.open(new_file_path, 'w') as script:
180+
script.write("""#!/bin/sh
181+
mkfifo testpipe
182+
echo "4" > testpipe &
183+
""")
184+
try:
185+
st = os.stat(new_file_path)
186+
os.chmod(new_file_path, st.st_mode | stat.S_IEXEC)
187+
os.system(new_file_path)
188+
with io.open("testpipe", "rb") as r:
189+
out = r.read(1)
190+
assert out == b"4", out
191+
finally:
192+
try:
193+
os.remove(new_file_path)
194+
except:
195+
pass
196+
try:
197+
os.remove("testpipe")
198+
except:
199+
pass
200+
176201
def test_mkdir_rmdir(self):
177202
os.mkdir(TEST_FULL_PATH1)
178203
try:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/OSErrorEnum.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -312,7 +312,7 @@ public static ErrorAndMessagePair fromException(Exception e, TruffleString.Equal
312312
return new ErrorAndMessagePair(oserror, oserror.getMessage());
313313
}
314314
} else { // Generic IOException
315-
OSErrorEnum oserror = tryFindErrnoFromMessage(e);
315+
OSErrorEnum oserror = tryFindErrnoFromMessage(e, eqNode);
316316
if (oserror == null) {
317317
return new ErrorAndMessagePair(OSErrorEnum.EIO, getMessage(e));
318318
} else {
@@ -360,15 +360,13 @@ private static TruffleString getReason(FileSystemException e) {
360360
}
361361

362362
@TruffleBoundary
363-
private static OSErrorEnum tryFindErrnoFromMessage(Exception e) {
364-
if (e.getMessage().contains("Broken pipe")) {
365-
return OSErrorEnum.EPIPE;
366-
}
367-
Matcher m = ERRNO_PATTERN.matcher(e.getMessage());
363+
private static OSErrorEnum tryFindErrnoFromMessage(Exception e, TruffleString.EqualNode eqNode) {
364+
String message = e.getMessage();
365+
Matcher m = ERRNO_PATTERN.matcher(message);
368366
if (m.find()) {
369367
return fromNumber(Integer.parseInt(m.group(1)));
370368
}
371-
return null;
369+
return OSErrorEnum.fromMessage(toTruffleStringUncached(message), eqNode);
372370
}
373371

374372
@ValueType

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,15 @@ public Buffer read(int fd, long length,
527527
}
528528

529529
@TruffleBoundary
530-
private static Buffer readBytesFromChannel(ReadableByteChannel channel, long size) throws IOException {
530+
private static Buffer readBytesFromChannel(ReadableByteChannel channel, long sizeIn) throws IOException {
531+
long size = sizeIn;
531532
if (channel instanceof SeekableByteChannel seekableByteChannel) {
532-
long availableSize = seekableByteChannel.size() - seekableByteChannel.position();
533-
size = Math.min(size, availableSize);
533+
try {
534+
long availableSize = seekableByteChannel.size() - seekableByteChannel.position();
535+
size = Math.min(size, availableSize);
536+
} catch (IOException e) {
537+
// pass and read what we can
538+
}
534539
}
535540
size = Math.min(size, MAX_READ);
536541
ByteBuffer dst = ByteBuffer.allocate((int) size);

0 commit comments

Comments
 (0)