From: Victor Stinner <victor.stinner@gmail.com>
Date: Mon, 30 Mar 2015 09:18:30 +0000 (+0200)
Subject: Issue #22585: os.urandom() now releases the GIL when the getentropy() is used
X-Git-Tag: v3.5.0a4~93^2^2~49
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9aa1331c6f73b0868f736a40445cc3bcd33c5345;p=python

Issue #22585: os.urandom() now releases the GIL when the getentropy() is used
(OpenBSD 5.6+).
---

diff --git a/Python/random.c b/Python/random.c
index 93d300dae4..3f307cf62a 100644
--- a/Python/random.c
+++ b/Python/random.c
@@ -78,16 +78,24 @@ py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
 {
     while (size > 0) {
         Py_ssize_t len = Py_MIN(size, 256);
-        int res = getentropy(buffer, len);
-        if (res < 0) {
-            if (fatal) {
-                Py_FatalError("getentropy() failed");
-            }
-            else {
+        int res;
+
+        if (!fatal) {
+            Py_BEGIN_ALLOW_THREADS
+            res = getentropy(buffer, len);
+            Py_END_ALLOW_THREADS
+
+            if (res < 0) {
                 PyErr_SetFromErrno(PyExc_OSError);
                 return -1;
             }
         }
+        else {
+            res = getentropy(buffer, len);
+            if (res < 0)
+                Py_FatalError("getentropy() failed");
+        }
+
         buffer += len;
         size -= len;
     }