git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How to handle terminal detection in a daemon calling git?

From
Jeff King <peff@peff.net>
Date
Jun 1, 2012, 09:53 UTC
Message-ID
<20120601095333.GF32340@sigill.intra.peff.net>
In-Reply-To
<1338471582.12573.140661083191693.32060E78@webmail.messagingengine.com>
On Thu, May 31, 2012 at 08:39:42AM -0500, Travis P wrote:
Show 7 quoted lines
> Here's what I learned this morning: it appears to work when I don't
> close STDIN.
> 
>     #close $_ for *STDIN, *STDOUT, *STDERR; # What I was doing. Fails.
>     close $_ for *STDOUT, *STDERR;  # Tried this, it works.
>     *STDOUT = $log_fh;
>     *STDERR = $log_fh;

Yeah, don't do that. This can cause subtle bugs in subprocesses. For example:

  1. You don't have a descriptor 0, because it is closed.
  2. Some part of the program opens a new descriptor (e.g., to read a
     file, making a pipe, etc). This becomes descriptor 0, because it is
     the lowest unused descriptor.
  3. The program wants to redirect its stdin (e.g., because it is
     forking and exec'ing a child). So it calls dup2(fd, 0), closing
     what was at stdin previously, which might have been valuable.

The right thing to do is to redirect stdin from /dev/null, not close it entirely.

Show 13 quoted lines
> >  2. We check isatty(1) for starting a pager, auto-selecting color,
> >  and in recent versions of git, for column support. But none of
> >  those things should be in use by git-pull anyway.
> 
> Ahh, this could be it:  when the pull does receive an output and I'm
> running the command in the shell, I get output with a "+-" where the
> plus is green and the minus red.  So, I think that git may be trying
> to check whether color (and columns?) is supported to output.
> However, it appears that this check is sensitive to stdin being
> connected (based on test mentioned earlier here), which is surprising.
>
> Is the code that calls isatty, calling it on all 3 descriptors, even
> when STDIN is not relevant?

No, the color code just checks isatty(1). And even if it checked some random descriptor, the worst case should be that it affects the color flag. So I think the terminal thing is a red herring, and it is more likely you are seeing some subtle issue like the one I described above.

-Peff
Previous: Travis PNext: Travis
Message 5 of 9 in “How to handle terminal detection in a daemon calling git?”
  1. Travis PMay 30, 2012
  2. Jeff KingMay 31, 2012
  3. Junio C HamanoMay 31, 2012
  4. Travis PMay 31, 2012
  5. Jeff KingJun 1, 2012
  6. TravisJun 1, 2012
  7. Jeff KingJun 2, 2012
  8. Travis PMay 31, 2012
  9. Sitaram ChamartyMay 31, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.