Title: PEP 475: os.urandom() doesn't handle EINTR correctly
Type: Stage:
Components: Versions: Python 3.5
Status: closed Resolution: fixed
Dependencies: 23708 Superseder:
Assigned To: Nosy List: neologix, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2015-03-19 12:19 by vstinner, last changed 2015-03-19 22:44 by vstinner. This issue is now closed.

File name Uploaded Description Edit
random.patch vstinner, 2015-03-19 13:38 review
Messages (3)
msg238503 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-03-19 12:19
dev_urandom_python() retries read() if it fails with EINTR, but Python signal handlers are not called: PyErr_CheckSignals() is not called. It's important be able to interrupt a function by a signal, especially if it's blocking.

We need maybe a new _Py_read() function which would retry on EINTR. The code can be based on posix_read(). For open(), there is already _Py_open() which retries on EINTR (since the changeset ed25e945cdc2, issue #23694). It can be useful for

dev_urandom_noraise() retries on EINTR without calling PyErr_CheckSignals(). It may be acceptable since this function is only called by _PyRandom_Init() at an early stage of Python initialization. Or maybe EINTR should call Py_FatalError()?

On Linux 3.18 (Fedora 21 with kernel 3.18.7-200.fc21.x86_64), reading 1 GB from /dev/urandom or getrandom() returns 0x1ffffff bytes (32 MB). It's not an issue, os.urandom() continues to read until it gets enough data.

I tried to interrupt os.urandom() on Python 3.4 (read) and Python 3.5 (getrandom): reading from /dev/urandom using a file descriptor or getrandom() have the same behaviour, none fail with EINTR. I checked syscalls using strace.

It looks like getrandom() only fails with EINTR when flags=GRND_RANDOM. So this issue is maybe more theoric.
msg238519 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-03-19 13:38
random.patch: fix the issue with the new _Py_read() function added by the issue #23708.
msg238574 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-03-19 22:43
New changeset e57b4d464d1c by Victor Stinner in branch 'default':
Issue #23707: On UNIX, os.urandom() now calls the Python signal handler when
Date User Action Args
2015-03-19 22:44:11vstinnersetstatus: open -> closed
resolution: fixed
2015-03-19 22:43:59python-devsetnosy: + python-dev
messages: + msg238574
2015-03-19 20:20:53ned.deilysetnosy: + neologix
2015-03-19 13:41:20vstinnerlinkissue23648 dependencies
2015-03-19 13:38:08vstinnersetfiles: + random.patch
keywords: + patch
dependencies: + PEP 475: Add _Py_read() and _Py_write() functions
messages: + msg238519
2015-03-19 12:19:55vstinnercreate