From 2877425dd57959a0a059733b1cae787936891d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Rop=C3=A9?= Date: Mon, 28 Jun 2021 15:59:51 +0200 Subject: [PATCH 1/2] runtimeVM: ExecSync should return a recognizable error on timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running ExecSync with a timeout, do not report an error, but make an ExecSyncResponse showing that a timeout occurred. Signed-off-by: Julien Ropé --- internal/oci/runtime_vm.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/oci/runtime_vm.go b/internal/oci/runtime_vm.go index 6d38919f8ea..7479c9c4844 100644 --- a/internal/oci/runtime_vm.go +++ b/internal/oci/runtime_vm.go @@ -18,6 +18,7 @@ import ( "github.com/containerd/containerd/runtime/v2/task" "github.com/containerd/ttrpc" "github.com/containerd/typeurl" + conmonconfig "github.com/containers/conmon/runner/config" "github.com/cri-o/cri-o/internal/log" "github.com/cri-o/cri-o/server/cri/types" "github.com/cri-o/cri-o/server/metrics" @@ -318,6 +319,14 @@ func (r *runtimeVM) ExecSyncContainer(ctx context.Context, c *Container, command return nil, errors.Wrap(err, "ExecSyncContainer failed") } + // if the execution stopped because of the timeout, report it as such + if exitCode == -2 { + return &types.ExecSyncResponse{ + Stderr: []byte(conmonconfig.TimedOutMessage), + ExitCode: -1, + }, nil + } + return &types.ExecSyncResponse{ Stdout: stdoutBuf.Bytes(), Stderr: stderrBuf.Bytes(), @@ -453,7 +462,8 @@ func (r *runtimeVM) execContainerCommon(ctx context.Context, c *Container, cmd [ return -1, killErr } <-execCh - return -1, errors.Errorf("ExecSyncContainer timeout (%v)", timeoutDuration) + // do not make an error for timeout: report it with a specific error code + return -2, nil } if err == nil { From 4bd79a02dc74c46fa6b90f7b7670525a01ab2f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Rop=C3=A9?= Date: Mon, 28 Jun 2021 17:18:57 +0200 Subject: [PATCH 2/2] Define constants for execContainerCommon() error codes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julien Ropé --- internal/oci/runtime_vm.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/internal/oci/runtime_vm.go b/internal/oci/runtime_vm.go index 7479c9c4844..a063986957b 100644 --- a/internal/oci/runtime_vm.go +++ b/internal/oci/runtime_vm.go @@ -54,6 +54,11 @@ type containerInfo struct { cio *cio.ContainerIO } +const ( + execError = -1 + execTimeout = -2 +) + // newRuntimeVM creates a new runtimeVM instance func newRuntimeVM(path, root string) RuntimeImpl { logrus.Debug("oci.newRuntimeVM() start") @@ -320,7 +325,7 @@ func (r *runtimeVM) ExecSyncContainer(ctx context.Context, c *Container, command } // if the execution stopped because of the timeout, report it as such - if exitCode == -2 { + if exitCode == execTimeout { return &types.ExecSyncResponse{ Stderr: []byte(conmonconfig.TimedOutMessage), ExitCode: -1, @@ -345,13 +350,13 @@ func (r *runtimeVM) execContainerCommon(ctx context.Context, c *Container, cmd [ // Generate a unique execID execID, err := utils.GenerateID() if err != nil { - return -1, errors.Wrap(err, "exec container") + return execError, errors.Wrap(err, "exec container") } // Create IO fifos execIO, err := cio.NewExecIO(c.ID(), r.fifoDir, tty, stdin != nil) if err != nil { - return -1, errdefs.FromGRPC(err) + return execError, errdefs.FromGRPC(err) } defer execIO.Close() @@ -382,7 +387,7 @@ func (r *runtimeVM) execContainerCommon(ctx context.Context, c *Container, cmd [ any, err := typeurl.MarshalAny(pSpec) if err != nil { - return -1, errdefs.FromGRPC(err) + return execError, errdefs.FromGRPC(err) } request := &task.ExecProcessRequest{ @@ -397,7 +402,7 @@ func (r *runtimeVM) execContainerCommon(ctx context.Context, c *Container, cmd [ // Create the "exec" process if _, err = r.task.Exec(r.ctx, request); err != nil { - return -1, errdefs.FromGRPC(err) + return execError, errdefs.FromGRPC(err) } defer func() { @@ -410,7 +415,7 @@ func (r *runtimeVM) execContainerCommon(ctx context.Context, c *Container, cmd [ // Start the process if err := r.start(c.ID(), execID); err != nil { - return -1, err + return execError, err } // close closeIOChan to notify execIO exec has started. @@ -453,17 +458,17 @@ func (r *runtimeVM) execContainerCommon(ctx context.Context, c *Container, cmd [ case err = <-execCh: if err != nil { if killErr := r.kill(c.ID(), execID, syscall.SIGKILL, false); killErr != nil { - return -1, killErr + return execError, killErr } - return -1, err + return execError, err } case <-timeoutCh: if killErr := r.kill(c.ID(), execID, syscall.SIGKILL, false); killErr != nil { - return -1, killErr + return execError, killErr } <-execCh // do not make an error for timeout: report it with a specific error code - return -2, nil + return execTimeout, nil } if err == nil {