Skip to content

Commit 79b74ae

Browse files
committed
Issue #22181: os.urandom() now releases the GIL when the getrandom()
implementation is used.
1 parent a52f31d commit 79b74ae

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Python/random.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,18 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
115115

116116
while (0 < size) {
117117
errno = 0;
118-
/* the libc doesn't expose getrandom() yet, see:
118+
119+
/* Use syscall() because the libc doesn't expose getrandom() yet, see:
119120
* https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
120-
n = syscall(SYS_getrandom, buffer, size, flags);
121+
if (raise) {
122+
Py_BEGIN_ALLOW_THREADS
123+
n = syscall(SYS_getrandom, buffer, size, flags);
124+
Py_END_ALLOW_THREADS
125+
}
126+
else {
127+
n = syscall(SYS_getrandom, buffer, size, flags);
128+
}
129+
121130
if (n < 0) {
122131
if (errno == ENOSYS) {
123132
getrandom_works = 0;

0 commit comments

Comments
 (0)