Skip to content

Commit

Permalink
fix(provider): improve recognition of exit codes
Browse files Browse the repository at this point in the history
currently, both DO and Docker providers do not wait for the container to
exit until returning an exit code. this introduces a condition, where
the container hasn't exited yet (e.g. with an error), but we already
fetch it's stdout and exit code (which at that point is 0). this commit
makes the providers wait for the exec container to finish and only then
returns the response.

(cherry picked from commit 4a67052)

# Conflicts:
#	core/provider/digitalocean/task.go
#	core/provider/docker/task.go
#	cosmos/node/init.go
  • Loading branch information
Zygimantass authored and mergify[bot] committed Nov 7, 2024
1 parent 8394032 commit 0b75508
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 13 deletions.
88 changes: 84 additions & 4 deletions core/provider/digitalocean/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,39 @@ func (p *Provider) RunCommand(ctx context.Context, taskName string, command []st

defer resp.Close()

execInspect, err := dockerClient.ContainerExecInspect(ctx, exec.ID)
lastExitCode := 0

err = util.WaitForCondition(ctx, 10*time.Second, 100*time.Millisecond, func() (bool, error) {
execInspect, err := dockerClient.ContainerExecInspect(ctx, exec.ID)
if err != nil {
return false, err
}

if execInspect.Running {
return false, nil
}

lastExitCode = execInspect.ExitCode

return true, nil
})

if err != nil {
return "", "", 0, err
p.logger.Error("failed to wait for exec", zap.Error(err), zap.String("taskName", taskName))
return "", "", lastExitCode, err
}

var stdout, stderr bytes.Buffer

_, err = stdcopy.StdCopy(&stdout, &stderr, resp.Reader)
<<<<<<< HEAD

Check failure on line 374 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '<<' (typecheck)
=======
if err != nil {
return "", "", lastExitCode, err
}
>>>>>>> 4a67052 (fix(provider): improve recognition of exit codes)

Check failure on line 379 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '>>' (typecheck)

return stdout.String(), stderr.String(), execInspect.ExitCode, nil
return stdout.String(), stderr.String(), lastExitCode, nil
}

func (p *Provider) RunCommandWhileStopped(ctx context.Context, taskName string, definition provider.TaskDefinition, command []string) (string, string, int, error) {
Expand Down Expand Up @@ -425,16 +448,73 @@ func (p *Provider) RunCommandWhileStopped(ctx context.Context, taskName string,

defer resp.Close()

execInspect, err := dockerClient.ContainerExecInspect(ctx, exec.ID)
lastExitCode := 0

err = util.WaitForCondition(ctx, 10*time.Second, 100*time.Millisecond, func() (bool, error) {
execInspect, err := dockerClient.ContainerExecInspect(ctx, exec.ID)
if err != nil {
return false, err
}

if execInspect.Running {
return false, nil
}

lastExitCode = execInspect.ExitCode

return true, nil
})

if err != nil {
<<<<<<< HEAD

Check failure on line 469 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '<<' (typecheck)
return "", "", 0, err
=======

Check failure on line 471 in core/provider/digitalocean/task.go

View workflow job for this annotation

GitHub Actions / golangci-lint (core)

expected statement, found '==' (typecheck)
p.logger.Error("failed to wait for exec", zap.Error(err), zap.String("taskName", taskName))
return "", "", lastExitCode, err
>>>>>>> 4a67052 (fix(provider): improve recognition of exit codes)
}

var stdout, stderr bytes.Buffer

_, err = stdcopy.StdCopy(&stdout, &stderr, resp.Reader)

<<<<<<< HEAD
return stdout.String(), stderr.String(), execInspect.ExitCode, nil
=======
return stdout.String(), stderr.String(), lastExitCode, err
}

func startContainerWithBlock(ctx context.Context, dockerClient *dockerclient.Client, containerID string) error {
// start container
if err := dockerClient.ContainerStart(ctx, containerID, types.ContainerStartOptions{}); err != nil {
return err
}

// cancel container after a minute
waitCtx, cancel := context.WithTimeout(ctx, 3*time.Minute)
defer cancel()
ticker := time.NewTicker(100 * time.Millisecond)
for {
select {
case <-waitCtx.Done():
return fmt.Errorf("error waiting for container to start: %v", waitCtx.Err())
case <-ticker.C:
container, err := dockerClient.ContainerInspect(ctx, containerID)
if err != nil {
return err
}

// if the container is running, we're done
if container.State.Running {
return nil
}

if container.State.Status == "exited" && container.State.ExitCode != 0 {
return fmt.Errorf("container exited with status %d", container.State.ExitCode)
}
}
}
>>>>>>> 4a67052 (fix(provider): improve recognition of exit codes)
}

func (p *Provider) pullImage(ctx context.Context, dockerClient *dockerclient.Client, image string) error {
Expand Down
29 changes: 26 additions & 3 deletions core/provider/docker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,39 @@ func (p *Provider) RunCommand(ctx context.Context, id string, command []string)

defer resp.Close()

execInspect, err := p.dockerClient.ContainerExecInspect(ctx, exec.ID)
lastExitCode := 0

err = util.WaitForCondition(ctx, 10*time.Second, 100*time.Millisecond, func() (bool, error) {
execInspect, err := p.dockerClient.ContainerExecInspect(ctx, exec.ID)
if err != nil {
return false, err
}

if execInspect.Running {
return false, nil
}

lastExitCode = execInspect.ExitCode

return true, nil
})

if err != nil {
return "", "", 0, err
p.logger.Error("failed to wait for exec", zap.Error(err), zap.String("id", id))
return "", "", lastExitCode, err
}

var stdout, stderr bytes.Buffer

_, err = stdcopy.StdCopy(&stdout, &stderr, resp.Reader)
<<<<<<< HEAD
=======
if err != nil {
return "", "", lastExitCode, err
}
>>>>>>> 4a67052 (fix(provider): improve recognition of exit codes)

return stdout.String(), stderr.String(), execInspect.ExitCode, nil
return stdout.String(), stderr.String(), lastExitCode, nil
}

func (p *Provider) RunCommandWhileStopped(ctx context.Context, id string, definition provider.TaskDefinition, command []string) (string, string, int, error) {
Expand Down
24 changes: 20 additions & 4 deletions cosmos/node/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ func (n *Node) AddGenesisAccount(ctx context.Context, address string, genesisAmo
n.logger.Debug("add-genesis-account", zap.String("stdout", stdout), zap.String("stderr", stderr), zap.Int("exitCode", exitCode))

if err != nil {
return err
return fmt.Errorf("failed to add genesis account: %w", err)
}

if exitCode != 0 {
return fmt.Errorf("failed to add genesis account (exitcode=%d): %s", exitCode, stderr)
}

return nil
Expand All @@ -103,11 +107,15 @@ func (n *Node) GenerateGenTx(ctx context.Context, genesisSelfDelegation types.Co
stdout, stderr, exitCode, err := n.Task.RunCommand(ctx, command)
n.logger.Debug("gentx", zap.String("stdout", stdout), zap.String("stderr", stderr), zap.Int("exitCode", exitCode))

if err != nil {
return fmt.Errorf("failed to generate genesis transaction: %w", err)
}

if exitCode != 0 {
return fmt.Errorf("failed to generate genesis transaction: %s (exitcode=%d)", stderr, exitCode)
return fmt.Errorf("failed to generate genesis transaction (exitcode=%d): %s", exitCode, stderr)
}

return err
return nil
}

// CollectGenTxs collects the genesis transactions from the node and create a finalized genesis file
Expand All @@ -125,7 +133,15 @@ func (n *Node) CollectGenTxs(ctx context.Context) error {
stdout, stderr, exitCode, err := n.Task.RunCommand(ctx, n.BinCommand(command...))
n.logger.Debug("collect-gentxs", zap.String("stdout", stdout), zap.String("stderr", stderr), zap.Int("exitCode", exitCode))

return err
if err != nil {
return fmt.Errorf("failed to collect genesis transactions: %w", err)
}

if exitCode != 0 {
return fmt.Errorf("failed to collect genesis transactions (exitcode=%d): %s", exitCode, stderr)
}

return nil
}

// OverwriteGenesisFile overwrites the genesis file on the node with the provided genesis file
Expand Down
15 changes: 14 additions & 1 deletion cosmos/node/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package node

import (
"context"
<<<<<<< HEAD

Check failure on line 5 in cosmos/node/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint (cosmos)

missing import path (typecheck)
=======

Check failure on line 6 in cosmos/node/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint (cosmos)

missing import path (typecheck)
"fmt"

>>>>>>> 4a67052 (fix(provider): improve recognition of exit codes)

Check failure on line 9 in cosmos/node/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint (cosmos)

missing import path (typecheck)
"go.uber.org/zap"
)

Expand All @@ -13,5 +18,13 @@ func (n *Node) InitHome(ctx context.Context) error {
stdout, stderr, exitCode, err := n.Task.RunCommand(ctx, n.BinCommand([]string{"init", n.Definition.Name, "--chain-id", chainConfig.ChainId}...))
n.logger.Debug("init home", zap.String("stdout", stdout), zap.String("stderr", stderr), zap.Int("exitCode", exitCode))

return err
if err != nil {

Check failure on line 21 in cosmos/node/init.go

View workflow job for this annotation

GitHub Actions / golangci-lint (cosmos)

expected declaration, found 'if' (typecheck)
return fmt.Errorf("failed to init home: %w", err)
}

if exitCode != 0 {
return fmt.Errorf("failed to init home (exit code %d): %s", exitCode, stderr)
}

return nil
}
6 changes: 5 additions & 1 deletion cosmos/node/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ func (n *Node) KeyBech32(ctx context.Context, name, bech string) (string, error)
command = append(command, "--bech", bech)
}

stdout, stderr, _, err := n.Task.RunCommand(ctx, command)
stdout, stderr, exitCode, err := n.Task.RunCommand(ctx, command)
n.logger.Debug("show key", zap.String("name", name), zap.String("stdout", stdout), zap.String("stderr", stderr))

if err != nil {
return "", fmt.Errorf("failed to show key %q (stderr=%q): %w", name, stderr, err)
}

if exitCode != 0 {
return "", fmt.Errorf("failed to show key %q (exitcode=%d): %s", name, exitCode, stderr)
}

return util.CleanDockerOutput(stdout), nil
}

0 comments on commit 0b75508

Please sign in to comment.