Skip to content

Commit 9460e21

Browse files
committed
Fix GIL acquire-release patterns in socket builtins
1 parent 0cceb6d commit 9460e21

File tree

1 file changed

+41
-29
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket

1 file changed

+41
-29
lines changed

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

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ Object accept(PSocket socket) {
129129
if (socket.getServerSocket() == null) {
130130
throw raiseOSError(null, OSErrorEnum.EINVAL);
131131
}
132-
try (GilNode.UncachedRelease gil = GilNode.uncachedRelease()) {
133-
SocketChannel acceptSocket = SocketUtils.accept(this, socket);
132+
try {
133+
SocketChannel acceptSocket;
134+
try (GilNode.UncachedRelease gil = GilNode.uncachedRelease()) {
135+
acceptSocket = SocketUtils.accept(this, socket);
136+
}
134137
if (acceptSocket == null) {
135138
throw raiseOSError(null, OSErrorEnum.EWOULDBLOCK);
136139
}
@@ -209,12 +212,14 @@ Object connect(PSocket socket, PTuple address,
209212
Object[] hostAndPort = getObjectArrayNode.execute(address);
210213
gil.release(true);
211214
try {
212-
doConnect(socket, hostAndPort);
215+
try {
216+
doConnect(socket, hostAndPort);
217+
} finally {
218+
gil.acquire();
219+
}
213220
return PNone.NONE;
214221
} catch (IOException e) {
215222
throw raise(OSError);
216-
} finally {
217-
gil.acquire();
218223
}
219224
}
220225

@@ -353,17 +358,18 @@ Object recv(VirtualFrame frame, PSocket socket, int bufsize, int flags,
353358
if (socket.getSocket() == null) {
354359
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
355360
}
356-
gil.release(true);
357361
ByteBuffer readBytes = PythonUtils.allocateByteBuffer(bufsize);
362+
gil.release(true);
358363
try {
359-
int length = SocketUtils.recv(this, socket, readBytes);
360-
gil.acquire();
361-
return factory().createBytes(PythonUtils.getBufferArray(readBytes), length);
364+
try {
365+
int length = SocketUtils.recv(this, socket, readBytes);
366+
return factory().createBytes(PythonUtils.getBufferArray(readBytes), length);
367+
} finally {
368+
gil.acquire();
369+
}
362370
} catch (NotYetConnectedException e) {
363-
gil.acquire();
364371
throw raiseOSError(frame, OSErrorEnum.ENOTCONN, e);
365372
} catch (IOException e) {
366-
gil.acquire();
367373
throw raiseOSError(frame, OSErrorEnum.EBADF, e);
368374
}
369375
}
@@ -436,40 +442,42 @@ Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, Object fl
436442
if (socket.getSocket() == null) {
437443
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
438444
}
439-
gil.release(true);
440445
SequenceStorage storage = buffer.getSequenceStorage();
441446
int bufferLen = lenNode.execute(storage);
442447
if (byteStorage.profile(storage instanceof ByteSequenceStorage)) {
443448
ByteBuffer byteBuffer = ((ByteSequenceStorage) storage).getBufferView();
449+
gil.release(true);
444450
try {
445-
int len = SocketUtils.recv(this, socket, byteBuffer);
446-
gil.acquire();
447-
return len;
451+
try {
452+
return SocketUtils.recv(this, socket, byteBuffer);
453+
} finally {
454+
gil.acquire();
455+
}
448456
} catch (NotYetConnectedException e) {
449-
gil.acquire();
450457
throw raiseOSError(frame, OSErrorEnum.ENOTCONN, e);
451458
} catch (IOException e) {
452-
gil.acquire();
453459
throw raiseOSError(frame, OSErrorEnum.EBADF, e);
454460
}
455461
} else {
456462
byte[] targetBuffer = new byte[bufferLen];
457463
ByteBuffer byteBuffer = PythonUtils.wrapByteBuffer(targetBuffer);
458464
int length;
465+
gil.release(true);
459466
try {
460-
length = SocketUtils.recv(this, socket, byteBuffer);
467+
try {
468+
length = SocketUtils.recv(this, socket, byteBuffer);
469+
} finally {
470+
gil.acquire();
471+
}
461472
} catch (NotYetConnectedException e) {
462-
gil.acquire();
463473
throw raiseOSError(frame, OSErrorEnum.ENOTCONN, e);
464474
} catch (IOException e) {
465-
gil.acquire();
466475
throw raiseOSError(frame, OSErrorEnum.EBADF, e);
467476
}
468477
for (int i = 0; i < length; i++) {
469478
// we don't allow generalization
470479
setItem.execute(frame, storage, i, targetBuffer[i]);
471480
}
472-
gil.acquire();
473481
return length;
474482
}
475483
}
@@ -507,17 +515,19 @@ Object send(VirtualFrame frame, PSocket socket, PBytes bytes, Object flags,
507515
if (socket.getSocket() == null) {
508516
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
509517
}
510-
gil.release(true);
511518
int written;
512519
ByteBuffer buffer = PythonUtils.wrapByteBuffer(toBytes.execute(bytes.getSequenceStorage()));
520+
gil.release(true);
513521
try {
514-
written = SocketUtils.send(this, socket, buffer);
522+
try {
523+
written = SocketUtils.send(this, socket, buffer);
524+
} finally {
525+
gil.acquire();
526+
}
515527
} catch (NotYetConnectedException e) {
516528
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
517529
} catch (IOException e) {
518530
throw raise(OSError);
519-
} finally {
520-
gil.acquire();
521531
}
522532
if (written == 0) {
523533
throw raiseOSError(frame, OSErrorEnum.EWOULDBLOCK);
@@ -539,7 +549,6 @@ Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, Object flag
539549
if (socket.getSocket() == null) {
540550
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
541551
}
542-
gil.release(true);
543552
ByteBuffer buffer = PythonUtils.wrapByteBuffer(toBytes.execute(bytes.getSequenceStorage()));
544553
long timeoutMillis = socket.getTimeoutInMilliseconds();
545554
TimeoutHelper timeoutHelper = null;
@@ -551,14 +560,17 @@ Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, Object flag
551560
timeoutMillis = timeoutHelper.checkAndGetRemainingTimeout(this);
552561
}
553562
int written;
563+
gil.release(true);
554564
try {
555-
written = SocketUtils.send(this, socket, buffer, timeoutMillis);
565+
try {
566+
written = SocketUtils.send(this, socket, buffer, timeoutMillis);
567+
} finally {
568+
gil.acquire();
569+
}
556570
} catch (NotYetConnectedException e) {
557571
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
558572
} catch (IOException e) {
559573
throw raise(OSError);
560-
} finally {
561-
gil.acquire();
562574
}
563575
if (written == 0) {
564576
throw raiseOSError(frame, OSErrorEnum.EWOULDBLOCK);

0 commit comments

Comments
 (0)