Skip to content

Commit

Permalink
runtime: ignore file-already-closed error if dead shim
Browse files Browse the repository at this point in the history
fix: #5130

Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Mar 15, 2021
1 parent d8208e2 commit eabd9b9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
13 changes: 5 additions & 8 deletions runtime/v2/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func loadShim(ctx context.Context, bundle *Bundle, events *exchange.Exchange, rt
}()
f, err := openShimLog(shimCtx, bundle, client.AnonReconnectDialer)
if err != nil {
return nil, errors.Wrap(err, "open shim log pipe")
return nil, errors.Wrap(err, "open shim log pipe when reload")
}
defer func() {
if err != nil {
Expand All @@ -96,13 +96,10 @@ func loadShim(ctx context.Context, bundle *Bundle, events *exchange.Exchange, rt
// copy the shim's logs to containerd's output
go func() {
defer f.Close()
if _, err := io.Copy(os.Stderr, f); err != nil {
// When using a multi-container shim the 2nd to Nth container in the
// shim will not have a separate log pipe. Ignore the failure log
// message here when the shim connect times out.
if !errors.Is(err, os.ErrNotExist) {
log.G(ctx).WithError(err).Error("copy shim log")
}
_, err := io.Copy(os.Stderr, f)
err = checkCopyShimLogError(ctx, err)
if err != nil {
log.G(ctx).WithError(err).Error("copy shim log after reload")
}
}()
onCloseWithShimLog := func() {
Expand Down
7 changes: 3 additions & 4 deletions runtime/v2/shim_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"context"
"io"
"net"
"os"
"path/filepath"
"time"

"github.com/containerd/fifo"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)

Expand All @@ -34,12 +36,9 @@ func openShimLog(ctx context.Context, bundle *Bundle, _ func(string, time.Durati
}

func checkCopyShimLogError(ctx context.Context, err error) error {
// When using a multi-container shim, the fifo of the 2nd to Nth
// container will not be opened when the ctx is done. This will
// cause an ErrReadClosed that can be ignored.
select {
case <-ctx.Done():
if err == fifo.ErrReadClosed {
if err == fifo.ErrReadClosed || errors.Is(err, os.ErrClosed) {
return nil
}
default:
Expand Down
4 changes: 4 additions & 0 deletions runtime/v2/shim_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package v2

import (
"context"
"os"
"testing"

"github.com/containerd/fifo"
Expand All @@ -43,6 +44,9 @@ func TestCheckCopyShimLogError(t *testing.T) {
if err := checkCopyShimLogError(ctx, nil); err != nil {
t.Fatalf("should return the actual error after context is done, but %v", err)
}
if err := checkCopyShimLogError(ctx, os.ErrClosed); err != nil {
t.Fatalf("should return the actual error after context is done, but %v", err)
}
if err := checkCopyShimLogError(ctx, fifo.ErrRdFrmWRONLY); err != fifo.ErrRdFrmWRONLY {
t.Fatalf("should return the actual error after context is done, but %v", err)
}
Expand Down

0 comments on commit eabd9b9

Please sign in to comment.