On Sat, Feb 29, 2020 at 1:34 PM Ian Lance Taylor <i...@golang.org> wrote:
>
> It does not make sense to use dup2 if you are not in control of the FD
> namespace.  In order to use dup2 you need to specify the new FD.  If
> that FD might be concurrently opened by some other package, or by the
> runtime, then you can not use dup2 safely.

But if you opened newfd yourself, or if it is 0/1/2 and you never
closed os.Std*, then you *can* dup2 safely, regardless of other
packages.

> This doesn't mean that Go program can never use dup2.  The runtime
> will only open a file descriptor when requested.  You can avoid
> packages that open descriptors at unpredictable times.

Can you really do that? I don't think the standard library guarantees
that it will not create a new FD behind the scenes tomorrow (nor it
exactly documents its FD usage and timing).

> Is this a theoretical question or one that arises from real code?

It's not theoretical, I became aware of EINTR and I'm trying to fix my own code.
I use dup2 to redirect FD 0/1/2 from inside the program itself (I know
I can assign to os.Std* but that is not always sufficient or safe).
The most common case is to redirect stderr including panic output.

As you can see, I cannot retry dup2 on EINTR unless I'm sure that the
first call has left newfd open, otherwise I will incur the race. But
if I don't retry it, then I have no way to recover from ordinary
signals!

I don't think you can argue that I'm supposed to have control of the
FD namespace: the whole point of dup2 being atomic [1] is that users
may not have such control, see also [2][3].

(I'm not actually getting EINTR from my dup2's, but I want to handle
it correctly if it can happen.)

> > Just to ask the an obvious question: is dup2() idempotent or not?
>
> dup2 in itself is idempotent.

It's hard to talk about idempotence when the context changes
unpredictably (FD state).
Dup2 is "atomic", in the sense that newfd is never reusable during the
whole syscall.
But is it "atomic" in the sense that it will either "leave FDs
unchanged with an error", or "complete without error", nothing in
between?

[1] http://man7.org/linux/man-pages/man2/dup2.2.html
[2] https://lwn.net/Articles/236843/
[3] https://stackoverflow.com/questions/23440216/race-condition-when-using-dup2

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEvMKD9mw6ZmX_1S65KkN%2BD%2BTtWybdbYqzrUOSH53TgeP_2NGw%40mail.gmail.com.

Reply via email to