Skip to content

Commit

Permalink
fix: handle hang-on-halt behavior from agoric-labs/cosmos-sdk#305
Browse files Browse the repository at this point in the history
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
JimLarson committed Nov 13, 2023
1 parent 2b23487 commit a4fd510
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
7 changes: 5 additions & 2 deletions golang/cosmos/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SHARED_BUILD_FLAGS := -tags "$(build_tags)" -gcflags '$(gcflags)' -ldflags '$(sh

all: compile-chain

compile-chain: compile-agd compile-daemon
compile-chain: compile-agd compile-daemon compile-tools
compile-go: compile-agd compile-libdaemon
compile-node: node-compile-gyp

Expand Down Expand Up @@ -77,6 +77,9 @@ compile-libdaemon: go-mod-cache
go build -v $(MOD_READONLY) $(SHARED_BUILD_FLAGS) -buildmode=c-shared \
-o build/libagcosmosdaemon.so ./cmd/libdaemon/main.go

compile-tools:
go build -o build/runtillmsg ./cmd/runtillmsg

go-mod-cache: go.sum
@echo "--> Download go modules to local cache"
@go mod download
Expand Down Expand Up @@ -106,7 +109,7 @@ t:

TM_URL := file://$(shell go list -m -f '{{ .Dir }}' github.com/tendermint/tendermint)/proto/tendermint
GOGO_PROTO_URL := file://$(shell go list -m -f '{{ .Dir }}' github.com/gogo/protobuf)
IBC_PROTO_URL := file://$(shell go list -m -f '{{ .Dir }}' github.com/cosmos/ibc-go/v3)/proto/ibc/core
IBC_PROTO_URL := file://$(shell go list -m -f '{{ .Dir }}' github.com/cosmos/ibc-go/v4)/proto/ibc/core
COSMOS_SDK_PROTO_URL := file://$(shell go list -m -f '{{ .Dir }}' github.com/cosmos/cosmos-sdk)/proto/cosmos

GOGO_PROTO_TYPES = third_party/proto/gogoproto
Expand Down
60 changes: 60 additions & 0 deletions golang/cosmos/cmd/runtillmsg/main.go
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
}
9 changes: 7 additions & 2 deletions packages/cosmic-swingset/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,16 @@ scenario2-run-chain: ../vm-config/decentral-devnet-config.json
OTEL_EXPORTER_PROMETHEUS_PORT=$(OTEL_EXPORTER_PROMETHEUS_PORT) \
$(AGC) --home=t1/n0 start --log_level=warn $(AGC_START_ARGS)

tools:
cd ../../golang/cosmos && make compile-tools

# Run a chain with an explicit halt.
BLOCKS_TO_RUN=3
scenario2-run-chain-to-halt: t1/decentral-economy-config.json
scenario2-run-chain-to-halt: t1/decentral-economy-config.json tools
CHAIN_BOOTSTRAP_VAT_CONFIG="$$PWD/t1/decentral-economy-config.json" \
$(AGC) --home=t1/n0 start --log_level=warn --halt-height=$$(($(INITIAL_HEIGHT) + $(BLOCKS_TO_RUN))); \
DEBUG=$(DEBUG) PATH="$$PWD/bin:$$PATH" \
../../golang/cosmos/build/runtillmsg "halt application" \
"$(SDK_ROOT)/bin/agd" --home=t1/n0 start --log_level=warn --halt-height=$$(($(INITIAL_HEIGHT) + $(BLOCKS_TO_RUN))); \
test "$$?" -eq 98
echo ran to $(INITIAL_HEIGHT) + $(BLOCKS_TO_RUN)

Expand Down

0 comments on commit a4fd510

Please sign in to comment.