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

Re: [RFC] test_when_finished in subshells

From
Jeff King <peff@peff.net>
Date
Sep 5, 2015, 08:54 UTC
Message-ID
<20150905085429.GB25039@sigill.intra.peff.net>
In-Reply-To
<xmqqfv2uf2kc.fsf@gitster.mtv.corp.google.com>
On Fri, Sep 04, 2015 at 11:43:15AM -0700, Junio C Hamano wrote:
Show 17 quoted lines
> > t7800 (in its final test) calls test_config in a subshell which has cd'd
> > into a submodule.
> >
> > Is this something worth worrying about, or is it sufficiently rare that
> > we can live with the current behaviour?
> 
> Fixing the instances you found is good, obviously ;-).  Thanks for
> working on this.
> 
> Even though the proposed detection is BASH-ism, I think it would not
> hurt other shells (they obviously do not help you catch bugs, but
> they would not misbehave as long as you make sure BASH_SUBSHELL is
> either unset or set to 0 at the beginning of the test), and the only
> impact to them would be a invocation of (often built-in) 'test'
> utility, whose performance impact should be miniscule.
> 
> I'll wait for opinion from others, of course.

I like it. In general I'm in favor of any lint-like fixes (whether for the tests or the C code itself) as long as:

  1. they don't create false positive noise
  2. they don't require extra effort at each call-site
  3. they don't have a performance impact

And I think this passes all three. Of course it would be nice if the new check ran on all shells, but even this seems like a strict improvement.

And I couldn't come up with anything better. I thought we might be able to use a canary trap, like this:

  trap canary QUIT
  echo outside
  trap
  echo inside
  (trap)

Because traps are reset inside a subshell, the first "trap" without arguments should print a string with "canary" in it, and the second should print nothing.

Except that it isn't remotely how it works in practice. :) Bash leaves the trap in place inside the subshell. And while dash does the right thing, any attempt to actually _look_ at the output will put us in a subshell. So:

  case "$(trap)" in
  *canary*) ... not in a subshell ...
  *) ... in a subshell ...
  esac

doesn't work; the trap report inside backticks is always empty (despite there being an explicit example in the POSIX manpage for trap of using $(trap) to get the value for a later eval).

It looks like if we use the EXIT trap rather than a signal, bash _does_ do the right thing (which kind of makes sense, I guess). So rather than a custom canary, we could use our regular EXIT handler as the canary. But I couldn't find a way to work around dash's issue.

Looks like there was even some discussion in 2010:
  http://www.spinics.net/lists/dash/msg00331.html
but my dash 0.5.7 remains broken. Oh, well.
-Peff
Previous: Junio C HamanoNext: John Keeping
Message 3 of 15 in “[RFC] test_when_finished in subshells”
  1. John KeepingSep 4, 2015
  2. Junio C HamanoSep 4, 2015
  3. Jeff KingSep 5, 2015
  4. 0/5 disallow test_when_finished in subshellsJohn Keeping, Sep 5, 2015
  5. 1/5 t7610: don't use test_config in a subshellJohn Keeping, Sep 5, 2015
  6. 2/5 t5801: don't use test_when_finished in a subshellJohn Keeping, Sep 5, 2015
  7. 3/5 test-lib-functions: support "test_config -C <dir> ..."John Keeping, Sep 5, 2015
  8. 4/5 t7800: don't use test_config in a subshellJohn Keeping, Sep 5, 2015
  9. 5/5 test-lib-functions: detect test_when_finished in subshellJohn Keeping, Sep 5, 2015
  10. Eric SunshineSep 6, 2015
  11. John KeepingSep 6, 2015
  12. Eric SunshineSep 6, 2015
  13. Junio C HamanoSep 5, 2015
  14. John KeepingSep 5, 2015
  15. Junio C HamanoSep 5, 2015

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.