Issue #23115: 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 ad8993d..d94f89a 100644
--- a/Python/random.c
+++ b/Python/random.c
@@ -103,16 +103,24 @@
 {
     while (size > 0) {
         Py_ssize_t len = size < 256 ? 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;
     }