-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle hang-on-halt behavior from agoric-labs/cosmos-sdk#305
The backport referenced above has the agd process hang around after halting. Wrote a wrapper utility which scans stderr for the halt application message, interrupts the process, then propagates its error signal. Conventional shell scripting is not up to the challenge of expressing this behavior.
- Loading branch information
Showing
3 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
// Runs a subcommand until a given string is seen on a line in the stderr output, | ||
// then sends SIGINT the subprocess and propagates its exit status. | ||
|
||
import ( | ||
"bufio" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) < 3 { | ||
log.Fatal("usage: runtillmsg msg cmd arg...") | ||
} | ||
msg, cmdName, args := os.Args[1], os.Args[2], os.Args[3:] | ||
|
||
// launch the command and listen to its stderr | ||
cmd := exec.Command(cmdName, args...) | ||
stderr, err := cmd.StderrPipe() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = cmd.Start() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// copy lines of stderr, but interrupt the command if we see msg | ||
scanner := bufio.NewScanner(stderr) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if strings.Contains(line, msg) { | ||
err = cmd.Process.Signal(syscall.SIGINT) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
os.Stderr.WriteString(line + "\n") | ||
} | ||
err = scanner.Err() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
stderr.Close() | ||
|
||
// wait for the command to exit, and propagate its exit code | ||
err = cmd.Wait() | ||
if err != nil { | ||
if exitErr, ok := err.(*exec.ExitError); ok { | ||
os.Exit(exitErr.ExitCode()) | ||
} | ||
log.Fatal(err) | ||
os.Exit(2) | ||
} | ||
// if cmd.Wait returns nil, exit code was 0, so just end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters