Skip to content

Commit 9aa1331

Browse files
committed
Issue #22585: os.urandom() now releases the GIL when the getentropy() is used
(OpenBSD 5.6+).
1 parent 04d09eb commit 9aa1331

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Python/random.c

+14-6
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,24 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
7878
{
7979
while (size > 0) {
8080
Py_ssize_t len = Py_MIN(size, 256);
81-
int res = getentropy(buffer, len);
82-
if (res < 0) {
83-
if (fatal) {
84-
Py_FatalError("getentropy() failed");
85-
}
86-
else {
81+
int res;
82+
83+
if (!fatal) {
84+
Py_BEGIN_ALLOW_THREADS
85+
res = getentropy(buffer, len);
86+
Py_END_ALLOW_THREADS
87+
88+
if (res < 0) {
8789
PyErr_SetFromErrno(PyExc_OSError);
8890
return -1;
8991
}
9092
}
93+
else {
94+
res = getentropy(buffer, len);
95+
if (res < 0)
96+
Py_FatalError("getentropy() failed");
97+
}
98+
9199
buffer += len;
92100
size -= len;
93101
}

0 commit comments

Comments
 (0)