Skip to content

Commit 2204f26

Browse files
committed
make sure we strictly follow the pattern gil.release(true); try { ... } finally { gil.acquire(); ... }
1 parent 30199d2 commit 2204f26

File tree

3 files changed

+26
-29
lines changed

3 files changed

+26
-29
lines changed

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ private void execv(VirtualFrame frame, PosixPath path, Object argv, SequenceStor
410410

411411
auditNode.audit("os.exec", path.originalObject, argv, PNone.NONE);
412412

413+
gil.release(true);
413414
try {
414-
gil.release(true);
415415
posixLib.execv(getPosixSupport(), path.value, opaqueArgs);
416416
} catch (PosixException e) {
417417
gil.acquire();
@@ -498,11 +498,10 @@ int open(VirtualFrame frame, PosixPath path, int flags, int mode, int dirFd,
498498
return posixLib.openat(getPosixSupport(), dirFd, path.value, fixedFlags, mode);
499499
} catch (PosixException e) {
500500
errorProfile.enter();
501-
gil.acquire();
502501
if (e.getErrorCode() == OSErrorEnum.EINTR.getNumber()) {
503502
PythonContext.triggerAsyncActions(this);
504-
gil.release(true);
505503
} else {
504+
gil.acquire(); // need GIL to construct OSError
506505
throw raiseOSErrorFromPosixException(frame, e, path.originalObject);
507506
}
508507
}
@@ -580,9 +579,7 @@ public PBytes read(VirtualFrame frame, int fd, int length,
580579
} catch (PosixException e) {
581580
errorProfile.enter();
582581
if (e.getErrorCode() == OSErrorEnum.EINTR.getNumber()) {
583-
gil.acquire(); // need gil to trigger actions or construct OSError
584582
PythonContext.triggerAsyncActions(this);
585-
gil.release(true); // continue read loop without gil
586583
} else {
587584
throw e;
588585
}
@@ -629,9 +626,7 @@ public long write(int fd, byte[] data,
629626
} catch (PosixException e) {
630627
errorProfile.enter();
631628
if (e.getErrorCode() == OSErrorEnum.EINTR.getNumber()) {
632-
gil.acquire();
633629
PythonContext.triggerAsyncActions(this);
634-
gil.release(true);
635630
} else {
636631
throw e;
637632
}
@@ -1793,21 +1788,23 @@ PTuple waitpid(VirtualFrame frame, long pid, int options,
17931788
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
17941789
@Cached BranchProfile errorProfile) {
17951790
gil.release(true);
1796-
while (true) {
1797-
try {
1798-
long[] result = posixLib.waitpid(getPosixSupport(), pid, options);
1799-
gil.acquire();
1800-
return factory().createTuple(new Object[]{result[0], result[1]});
1801-
} catch (PosixException e) {
1802-
errorProfile.enter();
1803-
gil.acquire();
1804-
if (e.getErrorCode() == OSErrorEnum.EINTR.getNumber()) {
1805-
PythonContext.triggerAsyncActions(this);
1806-
gil.release(true);
1807-
} else {
1808-
throw raiseOSErrorFromPosixException(frame, e);
1791+
try {
1792+
while (true) {
1793+
try {
1794+
long[] result = posixLib.waitpid(getPosixSupport(), pid, options);
1795+
return factory().createTuple(new Object[]{result[0], result[1]});
1796+
} catch (PosixException e) {
1797+
errorProfile.enter();
1798+
if (e.getErrorCode() == OSErrorEnum.EINTR.getNumber()) {
1799+
PythonContext.triggerAsyncActions(this);
1800+
} else {
1801+
gil.acquire();
1802+
throw raiseOSErrorFromPosixException(frame, e);
1803+
}
18091804
}
18101805
}
1806+
} finally {
1807+
gil.acquire();
18111808
}
18121809
}
18131810
}
@@ -1959,9 +1956,9 @@ int system(PBytes command,
19591956
// conversions for emulated backend because the bytes version after fsencode conversion
19601957
// is subject to sys.audit.
19611958
auditNode.audit("os.system", command);
1959+
byte[] bytes = toBytesNode.execute(command);
19621960
gil.release(true);
19631961
try {
1964-
byte[] bytes = toBytesNode.execute(command);
19651962
Object cmdOpaque = posixLib.createPathFromBytes(getPosixSupport(), bytes);
19661963
return posixLib.system(getPosixSupport(), cmdOpaque);
19671964
} finally {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ Object sleep(PythonModule self, long seconds,
486486
try {
487487
doSleep(seconds, deadline);
488488
} finally {
489-
dylib.put(self, TIME_SLEPT, nanoTime() - t + timeSlept(self));
490489
gil.acquire();
490+
dylib.put(self, TIME_SLEPT, nanoTime() - t + timeSlept(self));
491491
}
492492
PythonContext.triggerAsyncActions(this);
493493
return PNone.NONE;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ Object connect(PSocket socket, PTuple address,
207207
@Cached GilNode gil,
208208
@Cached GetObjectArrayNode getObjectArrayNode) {
209209
Object[] hostAndPort = getObjectArrayNode.execute(address);
210-
gil.release(true);
211210
try {
211+
gil.release(true);
212212
try {
213213
doConnect(socket, hostAndPort);
214214
} finally {
@@ -358,10 +358,10 @@ Object recv(VirtualFrame frame, PSocket socket, int bufsize, @SuppressWarnings("
358358
if (socket.getSocket() == null) {
359359
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
360360
}
361-
ByteBuffer readBytes = PythonUtils.allocateByteBuffer(bufsize);
362-
gil.release(true);
363361
try {
362+
gil.release(true);
364363
try {
364+
ByteBuffer readBytes = PythonUtils.allocateByteBuffer(bufsize);
365365
int length = SocketUtils.recv(this, socket, readBytes);
366366
return factory().createBytes(PythonUtils.getBufferArray(readBytes), length);
367367
} finally {
@@ -447,8 +447,8 @@ Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, @Suppress
447447
int bufferLen = lenNode.execute(storage);
448448
if (byteStorage.profile(storage instanceof ByteSequenceStorage)) {
449449
ByteBuffer byteBuffer = ((ByteSequenceStorage) storage).getBufferView();
450-
gil.release(true);
451450
try {
451+
gil.release(true);
452452
try {
453453
return SocketUtils.recv(this, socket, byteBuffer);
454454
} finally {
@@ -463,8 +463,8 @@ Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, @Suppress
463463
byte[] targetBuffer = new byte[bufferLen];
464464
ByteBuffer byteBuffer = PythonUtils.wrapByteBuffer(targetBuffer);
465465
int length;
466-
gil.release(true);
467466
try {
467+
gil.release(true);
468468
try {
469469
length = SocketUtils.recv(this, socket, byteBuffer);
470470
} finally {
@@ -521,8 +521,8 @@ Object send(VirtualFrame frame, PSocket socket, PBytes bytes, @SuppressWarnings(
521521
}
522522
int written;
523523
ByteBuffer buffer = PythonUtils.wrapByteBuffer(toBytes.execute(bytes.getSequenceStorage()));
524-
gil.release(true);
525524
try {
525+
gil.release(true);
526526
try {
527527
written = SocketUtils.send(this, socket, buffer);
528528
} finally {
@@ -564,8 +564,8 @@ Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, @SuppressWa
564564
timeoutMillis = timeoutHelper.checkAndGetRemainingTimeout(this);
565565
}
566566
int written;
567-
gil.release(true);
568567
try {
568+
gil.release(true);
569569
try {
570570
written = SocketUtils.send(this, socket, buffer, timeoutMillis);
571571
} finally {

0 commit comments

Comments
 (0)