From 83671f95d8c0e1eb9c8e4c4b187b0e03605d2b73 Mon Sep 17 00:00:00 2001 From: Keith Johnson Date: Mon, 25 Mar 2024 10:15:23 -0400 Subject: [PATCH] Properly parse stderr when updating container status I believe the previous code meant to use cmd.Run instead of cmd.Start. The issue is that cmd.Start returns before the command has finished executing, so the conditional body checking for the stderr of the command never gets executed. Raise the cmd.Start up into it's own conditional, which is checking for whether the process could be started. Then we consume stderr, check for some specific strings in the output, and then finally continue on with the rest of the code. Signed-off-by: Keith Johnson --- libpod/oci_conmon_common.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/libpod/oci_conmon_common.go b/libpod/oci_conmon_common.go index c544f0f75a..3bff76adec 100644 --- a/libpod/oci_conmon_common.go +++ b/libpod/oci_conmon_common.go @@ -232,29 +232,31 @@ func (r *ConmonOCIRuntime) UpdateContainerStatus(ctr *Container) error { return fmt.Errorf("getting stderr pipe: %w", err) } - if err := cmd.Start(); err != nil { - out, err2 := io.ReadAll(errPipe) - if err2 != nil { - return fmt.Errorf("getting container %s state: %w", ctr.ID(), err) - } - if strings.Contains(string(out), "does not exist") || strings.Contains(string(out), "No such file") { - if err := ctr.removeConmonFiles(); err != nil { - logrus.Debugf("unable to remove conmon files for container %s", ctr.ID()) - } - ctr.state.ExitCode = -1 - ctr.state.FinishedTime = time.Now() - ctr.state.State = define.ContainerStateExited - return ctr.runtime.state.AddContainerExitCode(ctr.ID(), ctr.state.ExitCode) - } - return fmt.Errorf("getting container %s state. stderr/out: %s: %w", ctr.ID(), out, err) + err = cmd.Start() + if err != nil { + return fmt.Errorf("error launching container runtime: %w", err) } defer func() { _ = cmd.Wait() }() + stderr, err := io.ReadAll(errPipe) + if err != nil { + return fmt.Errorf("reading stderr: %s: %w", ctr.ID(), err) + } + if strings.Contains(string(stderr), "does not exist") || strings.Contains(string(stderr), "No such file") { + if err := ctr.removeConmonFiles(); err != nil { + logrus.Debugf("unable to remove conmon files for container %s", ctr.ID()) + } + ctr.state.ExitCode = -1 + ctr.state.FinishedTime = time.Now() + ctr.state.State = define.ContainerStateExited + return ctr.runtime.state.AddContainerExitCode(ctr.ID(), ctr.state.ExitCode) + } if err := errPipe.Close(); err != nil { return err } + out, err := io.ReadAll(outPipe) if err != nil { return fmt.Errorf("reading stdout: %s: %w", ctr.ID(), err)