Skip to content

Commit

Permalink
testscript: ignore result when interrupting background processes
Browse files Browse the repository at this point in the history
When an entire script runs and the end is reached, any background
processes begun with a '&' command get interrupted or killed,
depending on the platform and timeout, and we wait for them to finish.

We also checked their resulting status code and failed if they didn't
exit with a status code of 0. However, as explained in the comment,
this would always fail on Windows, given that it doesn't have interrupt
signals so we would kill directly, causing a "signal: killed" error.

Worse, any failures here caused a `panic: fail now!` as that is how
we bubble up errors when a script command is being run, but such panics
were not being recovered once we reached the end of a script.
Now that we don't check the result anymore here, the panics are gone.

Fixes #228.
Fixes #260.
  • Loading branch information
mvdan committed Jul 9, 2024
1 parent 66960b6 commit 3ade0b8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions testscript/testdata/interrupt_implicit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Let testscript stop signalcatcher at the end of the testscript.

signalcatcher &
waitfile catchsignal
6 changes: 5 additions & 1 deletion testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,11 @@ func (ts *TestScript) run() {
for _, bg := range ts.background {
interruptProcess(bg.cmd.Process)
}
ts.cmdWait(false, nil)
// On some platforms like Windows, we kill background commands directly
// as we can't send them an interrupt signal, so they always fail.
// Moreover, it's relatively common for a process to fail when interrupted.
// Once we've reached the end of the script, ignore the status of background commands.
ts.waitBackground(false)

// If we reached here but we've failed (probably because ContinueOnError
// was set), don't wipe the log and print "PASS".
Expand Down

0 comments on commit 3ade0b8

Please sign in to comment.