Repository navigation
gh-130209: Retry path-based os functions on EINTR - #160544
larryhastings wants to merge 2 commits into
Conversation
PEP 475 (3.5) added "EINTR" retry loops to the functions expected to block:
read, write, open, stat, select, sockets, wait*, and so on. The path-based
functions (stat, rename, utime, unlink, listdir, ...) were skipped,
on the assumption that they don't block long enough to be interrupted.
That assumption doesn't hold on network or userspace filesystems.
This commit adds retry loops to thirty more functions, to fill in the gap:
* os.access
* os.chdir
* os.chflags
* os.chmod
* os.chown
* os.chroot
* os.getxattr
* os.lchflags
* os.lchmod
* os.lchown
* os.link
* os.listdir
* os.listxattr
* os.lstat
* os.mkdir
* os.readlink
* os.removexattr
* os.rename
* os.replace
* os.rmdir
* os.scandir (open, iteration, and DirEntry.stat() / is_*() )
* os.setxattr
* os.stat
* os.statvfs
* os.statx
* os.symlink
* os.truncate
* os.unlink
* os.utime
* os._fcopyfile
os.access() is a special case worth calling out: it reports failure by
return value, so before this change an EINTR made it silently return
False for a perfectly readable file--with no exception at all.
Issue: pythongh-130209
|
(I'll review next week. Sorry I didn't get to it; I promised too much to people at the sprint.) |
encukou
left a comment
There was a problem hiding this comment.
The main issue I see is handling non-idempotent operations (like mkdir, rmdir, rename, readdir, unlink, symlink, fcopyfile, chdir, chroot, removexattr; setattr with XATTR_CREATE).
I checked my system's man pages for a few of them; all I saw don't mention EINTR handling (unlike for example write(2) which says “The call was interrupted by a signal before any data was written”). I'm not sure if we can always assume that the FS operation did not happen yet. Do you know if that's documented/standardized somewhere?
NFS/FUSE sounds like the wild West in terms of details like this; are we regressing on some systems?
Can you add the tests? We already test some stuff that needs root.
Otherwise, I have a few comments, and nits (ignore those if your choices are deliberate).
| Added retry loops to functions in :mod:`os` that interact with the | ||
| filesystem, such as :func:`~os.stat`, :func:`~os.rename`, :func:`~os.utime`, | ||
| :func:`~os.unlink`, :func:`~os.listdir` and :func:`~os.scandir`. On POSIX, | ||
| if a system call fails and ``errno`` is set to ``EINTR``, it means a signal | ||
| was delivered to the process and interrupted the call; the recommended | ||
| course of action is to handle the signal and retry. :pep:`475` added retry | ||
| loops for a number of functions in the :mod:`os` module, but skipped most of | ||
| the functions that take a *path* argument, because those don't normally | ||
| result in ``EINTR``. However, they *can* (and do!) on network filesystems | ||
| (e.g. NFS) and userspace filesystems (e.g. FUSE). |
There was a problem hiding this comment.
This seems too long for a news entry; consider:
| Added retry loops to functions in :mod:`os` that interact with the | |
| filesystem, such as :func:`~os.stat`, :func:`~os.rename`, :func:`~os.utime`, | |
| :func:`~os.unlink`, :func:`~os.listdir` and :func:`~os.scandir`. On POSIX, | |
| if a system call fails and ``errno`` is set to ``EINTR``, it means a signal | |
| was delivered to the process and interrupted the call; the recommended | |
| course of action is to handle the signal and retry. :pep:`475` added retry | |
| loops for a number of functions in the :mod:`os` module, but skipped most of | |
| the functions that take a *path* argument, because those don't normally | |
| result in ``EINTR``. However, they *can* (and do!) on network filesystems | |
| (e.g. NFS) and userspace filesystems (e.g. FUSE). | |
| Added retry loops to functions in :mod:`os` that interact with the | |
| filesystem, such as :func:`~os.stat`, :func:`~os.rename`, :func:`~os.utime`, | |
| :func:`~os.unlink`, :func:`~os.listdir` and :func:`~os.scandir`, which | |
| :pep:`475` skipped because those don't normally | |
| result in ``EINTR``. They *can* (and do!) on network filesystems | |
| (e.g. NFS) and userspace filesystems (e.g. FUSE). |
| #endif | ||
| result = rename(src->narrow, dst->narrow); | ||
| Py_END_ALLOW_THREADS | ||
| result = rename(src->narrow, dst->narrow); |
There was a problem hiding this comment.
Nit: This line might be after else; unlike other such cases isn't indented, so it's more confusing/misleading than usual. Please indent it; ideally add braces too.
| result = rename(src->narrow, dst->narrow); | |
| { | |
| result = rename(src->narrow, dst->narrow); | |
| } |
(same in utime_default below)
| PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); | ||
| return NULL; | ||
| } | ||
| } else |
There was a problem hiding this comment.
The existing code uses PEP-7 style; please don't change it.
| } else | |
| } | |
| else |
| Py_END_ALLOW_THREADS | ||
|
|
||
| // interrupted by a signal (EINTR)? retry! | ||
| if ((result != 0) && (errno == EINTR)) { |
There was a problem hiding this comment.
Nit: Result checking is understandably inconsistent between functions. But within functions, consider making it textually identical -- either:
| if ((result != 0) && (errno == EINTR)) { | |
| if ((result) && (errno == EINTR)) { |
or change to if (result != 0) below.
Comparing (result) to (result != 0) takes a bit cognitive effort that's better spent elsewhere :)
| Py_END_ALLOW_THREADS | ||
|
|
||
| // interrupted by a signal (EINTR)? retry! | ||
| if ((result != 0) && (errno == EINTR)) { |
There was a problem hiding this comment.
Please use the same condition within a function.
| if ((result != 0) && (errno == EINTR)) { | |
| if ((result < 0) && (errno == EINTR)) { |
| Py_BEGIN_ALLOW_THREADS | ||
| #ifdef HAVE_UNLINKAT | ||
| if (dir_fd != DEFAULT_DIR_FD) { | ||
| if (HAVE_UNLINKAT_RUNTIME) { | ||
|
|
||
| result = unlinkat(dir_fd, path->narrow, 0); | ||
| } else { | ||
| unlinkat_unavailable = 1; | ||
| } | ||
| } else | ||
| if (dir_fd != DEFAULT_DIR_FD) { | ||
| if (HAVE_UNLINKAT_RUNTIME) { | ||
| result = unlinkat(dir_fd, path->narrow, 0); | ||
| } else { | ||
| unlinkat_unavailable = 1; | ||
| result = 0; | ||
| } | ||
| } else | ||
| #endif /* HAVE_UNLINKAT */ | ||
| result = unlink(path->narrow); | ||
| result = unlink(path->narrow); | ||
| Py_END_ALLOW_THREADS |
There was a problem hiding this comment.
Nit: Moving the Py_[BEGIN|END]_ALLOW_THREADS further inwards would allow handling !HAVE_UNLINKAT_RUNTIME here; you could remove the unlinkat_unavailable variable & artificial result:
#ifdef HAVE_UNLINKAT
if (dir_fd != DEFAULT_DIR_FD) {
if (HAVE_UNLINKAT_RUNTIME) {
Py_BEGIN_ALLOW_THREADS
result = unlinkat(dir_fd, path->narrow, 0);
Py_END_ALLOW_THREADS
} else {
argument_unavailable_error(NULL, "dir_fd");
return NULL;
}
} else
#endif /* HAVE_UNLINKAT */
{
Py_BEGIN_ALLOW_THREADS
result = unlink(path->narrow);
Py_END_ALLOW_THREADS
}(There's a bunch of cases like this in other functions.)
| Py_END_ALLOW_THREADS | ||
|
|
||
| // interrupted by a signal (EINTR)? retry! | ||
| if ((result != 0) && (errno == EINTR)) { |
There was a problem hiding this comment.
Please use the same condition.
| if ((result != 0) && (errno == EINTR)) { | |
| if ((result < 0) && (errno == EINTR)) { |
| Py_BEGIN_ALLOW_THREADS | ||
| if (path->is_fd) { | ||
| result = fstatfs(path->fd, &st); | ||
| for (;;) { |
There was a problem hiding this comment.
nit: os.statvfs would now be much shorter with 3 individual #ifdef __APPLE__ blocks for the differences.
I'm not entirely sure if it's more readable that way, though.
|
|
||
| // interrupted by a signal (EINTR)? retry! | ||
| if ((direntp == NULL) && (error == EINTR)) { | ||
| if (!PyErr_CheckSignals()) { |
There was a problem hiding this comment.
PyErr_CheckSignals can run user code. It would be better to unlock &iterator->read_mutex around it to avoid a deadlock.
(Hopefully no one reads directories in a signal handler, but better be safe.)
AFAICS, that means means the outer while can do the for(;;)'s job.
| // suppress error--this means we don't run path_error | ||
| // below, so we'll raise the existing already-set exception |
There was a problem hiding this comment.
That's not the only thing that happens, though. There's also a call to ScandirIterator_closedir, which I don't think we want for errors from a signal handler.
| // interrupted by a signal (EINTR)? retry! | ||
| if ((dirp == NULL) && (errno == EINTR)) { | ||
| if (PyErr_CheckSignals()) { | ||
| close(fd); |
There was a problem hiding this comment.
close can presumably block on NTFS, so it should go in Py_BEGIN_ALLOW_THREADS.
(And these should technically get their own EINTR handling, but I assume a FD leak here isn't worth the complexity.)
There was a problem hiding this comment.
close can presumably block on NTFS, so it should go in Py_BEGIN_ALLOW_THREADS.
(And these should technically get their own EINTR handling, but I assume a FD leak here isn't worth the complexity.)
According to https://lwn.net/Articles/576478/ (2013), on Linux, close() should not be retried if it fails with EINTR:
So the POSIX-suggested handling of an EINTR, which is to retry the close(), could actually be quite dangerous on Linux. For that reason, Mark Mentovai suggested a change to the glibc manual to avoid recommending retrying close() on Linux.
Extends PEP 475's retry-on-
EINTRbehavior to moreosfunctions that take a path (and a few fd-based stragglers). This means these functions can now succeed if a signal arrives during a slow filesystem operation (e.g. networked or user filesystems).Background
PEP 475 (3.5) added
EINTRretry loops to the functions expected to block:read,write,open,fstat,select, sockets,wait*, and so on. The path-based functions (stat,rename,utime,unlink,listdir, ...) were left out on the assumption that they don't block long enough to be interrupted. That assumption doesn't hold on network or userspace filesystems:fs/nfs/nfs4proc.c): when the server answersNFS4ERR_DELAY/GRACEthe client backs off inTASK_INTERRUPTIBLEfor lookup, getattr, access, readdir, remove, mkdir, open and friends, and any signal makes the syscall return-ERESTARTSYS. Sincesignal.signal()installs handlers withoutSA_RESTART, that reaches Python asEINTR. Every path walk goes through lookup/access/getattr, sostat,rename,utime,unlinketc. are all exposed.fs/fuse/dev.c,request_wait_answer): a request still queued when a signal arrives fails with a hard-EINTR, whichSA_RESTARTcannot restart. sshfs, rclone, and any other FUSE filesystem can produce this. A userspace retry is the only fix.The original report came from a program with a
SIGCHLDhandler doing routine bookkeeping on an NFS mount; every child exit was a chance for a path operation in the main thread to die withEINTR.os.access()is a special case worth calling out: it reports failure by return value, so before this change anEINTRmade it silently returnFalsefor a perfectly readable file, with no exception at all.Changes
Modules/posixmodule.conly. Retry loops added to 30 C functions, covering these Python-level functions:os.access,os.chdir,os.chflags,os.chmod,os.chown,os.chroot,os.getxattr,os.lchflags,os.lchmod,os.lchown,os.link,os.listdir,os.listxattr,os.lstat,os.mkdir,os.readlink,os.removexattr,os.rename,os.replace,os.rmdir,os.scandir(open, iteration, andDirEntry.stat()/is_*()),os.setxattr,os.stat,os.statvfs,os.statx,os.symlink,os.truncate,os.unlink,os.utime, andos._fcopyfile(macOS).Behavior on retry matches PEP 475 exactly: on
EINTR, run the Python signal handlers viaPyErr_CheckSignals(); if none raises, call again; if one raises, propagate that exception. Error reporting is unchanged (path_error,path_error2, etc. are called exactly as before). Windows builds compile the loops butEINTRnever occurs there.The code is mostly mechanical cookie-cutter code as follows, adjusted to conform to function-specific details:
Two pre-existing bugs fixed in passing, in code that had to be restructured anyway:
os.scandir(fd)on a platform withoutfdopendirleaked the half-built iterator object when raisingTypeError.DirEntry.stat()carried anerrnosave/restore aroundPy_DECREFthat became dead once the error is reported before the decref; removed.Testing
*atfunctions), and Windows macro sets to verify{}andPy_BEGIN/END_ALLOW_THREADSbalance in each configuration.InterruptedErrorand the twoos.accesscases returnFalsefor a readable file. (I could post the FUSE test harness if there was interest; it's unavoidably Linux-only.)Not compiled/tested locally:
os_lchmod_impl,os_chflags_impl,os_lchflags_impl,os__fcopyfile_impl. I can't even compile those on my Linux box. Reviewers may want to keep an eye on the macOS job for those.AI Involvement
I wrote the code, Claude Fable 5.1 wrote the tests and I reviewed them.
Issue: gh-130209