Skip to content

Commit a236b0a

Browse files
committed
Fixed leaking file descriptors
1 parent 8f2515f commit a236b0a

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def test_os_pipe():
1010
r,w = os.pipe()
1111
written = os.write(w, b"hello")
1212
assert os.read(r, written) == b"hello"
13+
os.close(r)
14+
os.close(w)
1315

1416

1517
class TestSubprocess(unittest.TestCase):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ abstract static class CloseNode extends PythonUnaryBuiltinNode {
178178
@Specialization
179179
@TruffleBoundary
180180
Object close(PSocket socket) {
181-
if (!socket.isOpen()) {
182-
return PNone.NONE;
183-
}
184-
185181
if (socket.getSocket() != null) {
186182
try {
187183
socket.getSocket().close();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/zipimporter/ZipImporterBuiltins.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ public LOCZipEntryStream(InputStream in) {
131131
this.in = in;
132132
}
133133

134+
@Override
135+
public void close() throws IOException {
136+
super.close();
137+
in.close();
138+
}
139+
134140
@Override
135141
public int read() throws IOException {
136142
if (readFirstLoc) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ public int openSocket(PSocket socket, PythonContext context) {
351351
try {
352352
PosixSupportLibrary posixLib = PosixSupportLibrary.getUncached();
353353
if (nativeFdForSockets == -1) {
354-
nativeFdForSockets = posixLib.pipe(posixSupport)[0];
354+
int[] fds = posixLib.pipe(posixSupport);
355+
posixLib.close(posixSupport, fds[1]);
356+
nativeFdForSockets = fds[0];
355357
}
356358
fd = posixLib.dup(posixSupport, nativeFdForSockets);
357359
} catch (PosixException e) {
@@ -366,6 +368,10 @@ public int openSocket(PSocket socket, PythonContext context) {
366368
@TruffleBoundary
367369
public void closeSocket(PSocket socket, PythonContext context) {
368370
int fd = socket.getFileno();
371+
if (fd < 0) {
372+
return;
373+
}
374+
socket.setFileno(-1);
369375
close(fd);
370376
Object posixSupport = context.getPosixSupport();
371377
if (posixSupport != this) {

0 commit comments

Comments
 (0)