3 ms·
Link to the standard: http://pubs.opengroup.org/onlinepubs/000095399/functions/close.html http://pubs.opengroup.org/onlinepubs/000095399/functions/clo... Some
by saurik 15y ago
Link to the standard:
http://pubs.opengroup.org/onlinepubs/000095399/functions/close.html http://pubs.opengroup.org/onlinepubs/000095399/functions/clo...
Some discussion that is related to this:
http://lwn.net/Articles/365294/ http://lwn.net/Articles/365294/
http://stackoverflow.com/questions/7297001/if-close2-fails-with-eio-will-the-file-descriptor-still-be-deleted http://stackoverflow.com/questions/7297001/if-close2-fails-w...
Something which I would consider to be "the answer" from Linus for Linux:
http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000...
http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3171.html http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3171...
http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3202.html http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3202...
(Apparently, the first thing Linux does is deallocate the file descriptor; /then/ it starts flushing pending written data. If this process is interrupted, it will return EINTR, but the file descriptor itself is already deallocated, and may have been reused long before close() returned.)
Honestly, after reviewing all of this material, I think the author of this post might simply not know what "unspecified" means (or, alternatively, is making an abstract problem sound overly concrete and blown somewhat out of proportion).
When a specification says "unspecified", it doesn't mean "unknown at runtime, could be anything nondeterministically". It only means that the standard did not specify the result.
Often, this is because there is either a historical artifact or reasonable practical constraint that was posed by one or more vendors that were implementing the standard that meant different implementations simply /weren't/ going to agree on any one value.
However, for any given implementation, there very likely might be a correct answer. In the case of Linux, you should not retry calls to close(): the primary maintainer has told us that it deterministically guarantees the file descriptor is closed even if the function returns EINTR (critical edit: the wording of this sentence was incorrect in original draft).
That said, there may be another implementation out there, possibly one we may use (but with a standard like this, could easily be one that we have never heard of and that already died a death of obscurity years ago) that has the opposite behavior: where EINTR interrupts something (such as pending writes) before the file descriptor is closed, requiring developers to retry.
It is, however, unfortunate that this happened, and the author is right about issues for developers who attempt to use this API "without assuming more than the standard specified".
That said, there may be other options, such as using SA_RESTART, as argued in this discussion I just found (which also happens to talk about the meaning of "unspecified" ;P):
http://permalink.gmane.org/gmane.os.hurd.devel.readers/265 http://permalink.gmane.org/gmane.os.hurd.devel.readers/265
(edit:) Oh, and people may find it interesting that standards like this are often modified over time, with "interpretations" and outright edits. As an example, here is some vaguely related discussion regarding closing file descriptors during process termination:
http://austingroupbugs.net/view.php?id=498 http://austingroupbugs.net/view.php?id=498
(edit:) Some more existing discussion:
https://sites.google.com/site/michaelsafyan/software-engineering/checkforeintrwheninvokingclosethinkagain https://sites.google.com/site/michaelsafyan/software-enginee...
http://lists.freebsd.org/pipermail/freebsd-threads/2010-August/004866.html http://lists.freebsd.org/pipermail/freebsd-threads/2010-Augu...
http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2009-01/msg00155.html http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/20...
http://www.cpptalk.net/image-vp121851.html http://www.cpptalk.net/image-vp121851.html
http://programming.itags.org/unix-linux-programming/79468/ http://programming.itags.org/unix-linux-programming/79468/
Another (very long) Linux-specific thread involving some more responses from Linus:
http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0391.html http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0391.h...
http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0409.html http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0409.h...
http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/1173.html http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/1173.h...
In this thread we also find someone else claiming the standard is "broken":
http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0413.html http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0413.h...
With a possibly not useful response, that at least demonstrates that the examples from the specification did not retry:
http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0395.html http://lkml.indiana.edu/hypermail/linux/kernel/0207.2/0395.h...
- cperciva 15y agoWhen a specification says "unspecified", it doesn't mean "unknown at runtime, could be anything nondeterministically". It only means that the standard did not specify the result. Sure. But if you want to write portable code, this means you can't assume any particular behaviour. If you never want to run on anything except recent versions of linux, life is much easier for you. But some of us don't like being so limited.
- ajross 15y agoSure. Or just don't close() from a thread where you want to handle signals (without terminating, that is). Signals are the broken bits, frankly, not the POSIX standard. They have never played well with system calls. There's an essay somewhere I remember reading where an ITS hacker looks at Unix for how it handles the interrupted system call problem and comes away horrified at the discovery that it makes the user do it via EINTR. There's no good reason to be catching synchronous signals in modern code[1]. See signalfd() et. al. [1] Well, except for things that can only be delivered synchronously like SIGSEGV for user-handled paging. But that's complicated enough that the extra complexity of handling a SIGSEGV delivered out of a syscall is probably tolerable.
- FooBarWidget 15y agoNo reasons huh? I use signals to interrupt threads that block on open() and waitpid(). The only other alternative is sleep-polling which really sucks.
- ajross 15y agoI said don't catch signals, not don't use them. See signalfd() et. al. Though I'm curious why you're wanting to trying to interrupt those syscalls. In particular, why are you waiting on a process that you don't know is already dead? That's what SIGCHLD is for. And the only case I can think of for a blocking open() is a network/fuse mounted file. In which case the filesystem will surely eventually return an error (and if it doesn't no amount of voodoo in your application logic is going to correct the bugs in the underlying system).