Skip to content

Commit e993245

Browse files
committed
Issue #23115: os.urandom() now releases the GIL when the getentropy() is used
(OpenBSD 5.6+).
1 parent 96d8012 commit e993245

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

Python/random.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,24 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
103103
{
104104
while (size > 0) {
105105
Py_ssize_t len = size < 256 ? size : 256;
106-
int res = getentropy(buffer, len);
107-
if (res < 0) {
108-
if (fatal) {
109-
Py_FatalError("getentropy() failed");
110-
}
111-
else {
106+
int res;
107+
108+
if (!fatal) {
109+
Py_BEGIN_ALLOW_THREADS
110+
res = getentropy(buffer, len);
111+
Py_END_ALLOW_THREADS
112+
113+
if (res < 0) {
112114
PyErr_SetFromErrno(PyExc_OSError);
113115
return -1;
114116
}
115117
}
118+
else {
119+
res = getentropy(buffer, len);
120+
if (res < 0)
121+
Py_FatalError("getentropy() failed");
122+
}
123+
116124
buffer += len;
117125
size -= len;
118126
}

0 commit comments

Comments
 (0)