Skip to content

Commit c6ef8d5

Browse files
committed
fix: return error instead of calling os.Exit in runInSandbox
Replace the direct os.Exit() call with a cli.StatusError return so that all deferred cleanup functions (including stopTokenWriter) run normally. The exit code is propagated via cli.StatusError and handled in main.go. This removes the need to manually call stopTokenWriter() before os.Exit() and ensures all defers in the call chain execute. Fixes #1981 Fixes #1986 Assisted-By: docker-agent
1 parent 5039a4e commit c6ef8d5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

cmd/root/sandbox.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"os"
99
"os/exec"
1010

11+
"github.com/docker/cli/cli"
12+
1113
"github.com/spf13/cobra"
1214

1315
"github.com/docker/cagent/pkg/config"
@@ -64,10 +66,7 @@ func runInSandbox(cmd *cobra.Command, runConfig *config.RuntimeConfig, template
6466

6567
if err := dockerCmd.Run(); err != nil {
6668
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
67-
// Clean up the token writer before exiting so the temp
68-
// file is removed (defers won't run after os.Exit).
69-
stopTokenWriter()
70-
os.Exit(exitErr.ExitCode()) //nolint:gocritic // intentional exit to propagate sandbox exit code
69+
return cli.StatusError{StatusCode: exitErr.ExitCode()}
7170
}
7271
return fmt.Errorf("docker sandbox exec failed: %w", err)
7372
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"os"
67
"os/signal"
78
"syscall"
89

910
"github.com/docker/cagent/cmd/root"
11+
"github.com/docker/cli/cli"
1012
)
1113

1214
func main() {
1315
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
1416

1517
if err := root.Execute(ctx, os.Stdin, os.Stdout, os.Stderr, os.Args[1:]...); err != nil {
1618
cancel()
19+
if statusErr, ok := errors.AsType[cli.StatusError](err); ok {
20+
os.Exit(statusErr.StatusCode)
21+
}
1722
os.Exit(1)
1823
} else {
1924
cancel()

0 commit comments

Comments
 (0)