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

git subcommand sigint gotcha

From
Joey Hess <joey@kitenet.net>
Date
Oct 19, 2010, 04:53 UTC
Message-ID
<20101019045300.GA18043@gnu.kitenet.net>

I was trying to write a git subcommand, and I noticed that if I ctrl-c'd it, git would return, but leave the subcommand running in the background.

You can see the problem with this test case. 

#!/usr/bin/perl print "first sleep...\n"; $ret=system("sleep", "1m"); print "second sleep...\n"; system("sleep", "1s"); print "done with second sleep\n";

If you put it in path named git-sleep, then run "git sleep" and press ctrl-c, it keeps running:

joey@gnu:~>git sleep first sleep... ^Csecond sleep... joey@gnu:~>done with second sleep

So what's going on? Well, perl's system() blocks sigint while the child process is running. So if you run this as git-sleep, and press ctrl-c, it will continue on to the second sleep. If the code above checked the return status of system() it could detect that it was killed by SIGINT and itself exit.

What I don't understand is, why does git not wait() on the subcommand it ran? Any subcommand that forgets to check exit codes is liable to exhibit this weird behavior sometimes.

Ie, imagine the subcommand was running something like "git config --get core.bare" instead of sleep. It'd be easy to forget to check the exit status of that for a SIGINT; if the user ctrl-c'd at just the right instant, weird things would happen.

-- 
see shy jo
Next: Dmitry Potapov
Message 1 of 10 in “git subcommand sigint gotcha”
  1. Joey HessOct 19, 2010
  2. Dmitry PotapovOct 19, 2010
  3. RFC: [PATCH] ignore SIGINT&QUIT while waiting for external commandDmitry Potapov, Oct 19, 2010
  4. Jeff KingOct 19, 2010
  5. Jeff KingOct 19, 2010
  6. Jonathan NiederOct 19, 2010
  7. Jeff KingOct 19, 2010
  8. Jakub NarebskiOct 19, 2010
  9. Jonathan NiederOct 19, 2010
  10. Dmitry PotapovOct 19, 2010

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.