close() can be interrupted with undefined behavior Lassi Kortela (21 Jul 2019 11:44 UTC)
Re: close() can be interrupted with undefined behavior Duy Nguyen (21 Jul 2019 11:59 UTC)
Re: close() can be interrupted with undefined behavior Lassi Kortela (21 Jul 2019 12:11 UTC)
Re: close() can be interrupted with undefined behavior Duy Nguyen (23 Jul 2019 10:17 UTC)
Re: close() can be interrupted with undefined behavior hga@xxxxxx (21 Jul 2019 12:43 UTC)

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