Re: close() can be interrupted with undefined behavior
Duy Nguyen 21 Jul 2019 11:59 UTC
On Sun, Jul 21, 2019 at 6:45 PM Lassi Kortela <xxxxxx@lassi.io> wrote:
>
> "POSIX close(2) is broken", Colin Percival and Taylor Campbell, 2011.
> <http://www.daemonology.net/blog/2011-12-17-POSIX-close-is-broken.html>
>
> Apparently POSIX close() can cause EINTR and leave the file descriptor
> in an unknown state :D
At least on Linux EINTR on close() seems impossible [1]. I don't know
what other POSIX-y OSes do though.
[1] https://lwn.net/Articles/576478/
> I mean:
>
> int
> threadunsafe_close(int fd)
> {
> if (close(fd) == 0)
> return (0);
> if (errno != EINTR)
> return (-1);
> while (close(fd)) {
> if (errno == EBADF)
> return (0);
> if (errno != EINTR)
> return (-1);
> }
> return (0);
> }
>
> And the thread- and signal-safe version is worse.
>
> Let that sink in.
--
Duy