From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: Eric Sunshine <sunshine@sunshineco.com>,
git@vger.kernel.org, Baruch Burstein <bmburstein@gmail.com>,
Randall Becker <rsbecker@nexbridge.com>
Subject: Re: [RFC PATCH] vreportf: ensure sensible ordering of normal and error output
Date: Tue, 30 Nov 2021 02:14:25 -0500 [thread overview]
Message-ID: <YaXPUe9Sz3JBlzYL@coredump.intra.peff.net> (raw)
In-Reply-To: <xmqqtufu2pll.fsf@gitster.g>
On Mon, Nov 29, 2021 at 09:13:10PM -0800, Junio C Hamano wrote:
> Eric Sunshine <sunshine@sunshineco.com> writes:
>
> > This is RFC because I naturally worry about potential fallout from
> > making a change to such a core function. I can't think of any case that
> > it wouldn't be advantageous to flush stdout before stderr, so this
> > change _seems_ safe, however, it may be that I'm just not imaginative
> > enough, hence my hesitancy.
>
> If stdout and stderr are both going to the same place (e.g. the
> user's terminal), this would probably is an improvement, but if the
> standard output is going to a pipe talking to another process, which
> may care when the output is flushed, this may hurt.
>
> But as long as the calling code is using stdio, it cannot precisely
> control when the buffered contents are flushed anyway, so as long as
> the caller has working standard output, this may be OK.
Yeah, I think this logic applies to the "happy" case. Any caller which
is depending on the time of flush is already racily buggy.
What I wonder about is the error case. What can happen if flushing
fails? There are two interesting cases I can think of:
- flushing causes an error (which is quite likely, as we may
vreportf() because of an error on stdout). We should be OK, as we do
not care about the return value here, nor eventually checking
ferror(stdout). We may overwrite errno, but at this point in
vreportf(), we are committed to whatever error we're going to show
(and obviously the stderr flush below could cause the same issues).
- flushing causes us to block. This implies our stdout is connected to
a pipe or socket, and the other side is not expecting to read. A
plausible case here is a client sending us a big input which we find
to be bogus (maybe index-pack checking an incoming pack). We call
die() to complain about the input, but the client is still writing.
In the current code, we'd write out our error and then exit; the
client would get SIGPIPE or a write() error and abort. But with a
flush here, we could block writing back to the client, and now we're
in a deadlock; they are trying to write to us but we are no longer
reading, and we are blocked trying to get out a little bit of
irrelevant stdout data.
I _think_ we're probably OK here. The scenario above means that the
caller is already doing asynchronous I/O via stdio and is subject to
deadlock. Because the segment of buffer we try to flush here _could_
have been flushed already under the hood, which would have caused
the same blocking. A careful caller might be using select() or
similar to decide when it is OK to write, but I find it highly
unlikely they'd be using stdio in that case.
Of the two, the deadlock case worries me more, just because it would be
quiet subtle and racy. As I said, I think we may be OK, but my reasoning
there is pretty hand-wavy.
-Peff
next prev parent reply other threads:[~2021-11-30 7:15 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-30 4:39 [RFC PATCH] vreportf: ensure sensible ordering of normal and error output Eric Sunshine
2021-11-30 5:13 ` Junio C Hamano
2021-11-30 7:14 ` Jeff King [this message]
2021-11-30 7:23 ` Jeff King
2021-11-30 15:10 ` Ævar Arnfjörð Bjarmason
2021-11-30 20:52 ` Jeff King
2021-11-30 14:15 ` Eric Sunshine
2021-11-30 7:21 ` Jeff King
2021-11-30 14:05 ` Eric Sunshine
2021-11-30 14:57 ` Eric Sunshine
2021-12-01 13:51 ` "breaking" command output message parsing (was: [RFC PATCH] vreportf: ensure sensible ordering of normal and error output) Ævar Arnfjörð Bjarmason
2021-12-01 14:34 ` Eric Sunshine
2021-11-30 20:47 ` [RFC PATCH] vreportf: ensure sensible ordering of normal and error output Jeff King
2021-12-01 2:36 ` Eric Sunshine
2021-12-01 5:38 ` Eric Sunshine
2021-12-01 21:20 ` Ævar Arnfjörð Bjarmason
2021-12-02 0:43 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YaXPUe9Sz3JBlzYL@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=bmburstein@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=rsbecker@nexbridge.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).