Skip to content

Commit 324a947

Browse files
Merge pull request #3659 from katiewasnothere/shimreconnectupstream
fail on file not found for shim reconnect on containerd restart
2 parents a7e67ff + b4211d9 commit 324a947

6 files changed

Lines changed: 28 additions & 7 deletions

File tree

runtime/v2/binary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
7474
if err != nil {
7575
return nil, err
7676
}
77-
f, err := openShimLog(ctx, b.bundle)
77+
f, err := openShimLog(ctx, b.bundle, client.AnonDialer)
7878
if err != nil {
7979
return nil, errors.Wrap(err, "open shim log pipe")
8080
}

runtime/v2/shim.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func loadShim(ctx context.Context, bundle *Bundle, events *exchange.Exchange, rt
6767
if err != nil {
6868
return nil, err
6969
}
70-
conn, err := client.Connect(address, client.AnonDialer)
70+
conn, err := client.Connect(address, client.AnonReconnectDialer)
7171
if err != nil {
7272
return nil, err
7373
}
@@ -76,7 +76,7 @@ func loadShim(ctx context.Context, bundle *Bundle, events *exchange.Exchange, rt
7676
conn.Close()
7777
}
7878
}()
79-
f, err := openShimLog(ctx, bundle)
79+
f, err := openShimLog(ctx, bundle, client.AnonReconnectDialer)
8080
if err != nil {
8181
return nil, errors.Wrap(err, "open shim log pipe")
8282
}

runtime/v2/shim/util_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
7878
return net.DialTimeout("unix", "\x00"+address, timeout)
7979
}
8080

81+
func AnonReconnectDialer(address string, timeout time.Duration) (net.Conn, error) {
82+
return AnonDialer(address, timeout)
83+
}
84+
8185
// NewSocket returns a new socket
8286
func NewSocket(address string) (*net.UnixListener, error) {
8387
if len(address) > 106 {

runtime/v2/shim/util_windows.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ func getSysProcAttr() *syscall.SysProcAttr {
3333
return nil
3434
}
3535

36+
// AnonReconnectDialer returns a dialer for an existing npipe on containerd reconnection
37+
func AnonReconnectDialer(address string, timeout time.Duration) (net.Conn, error) {
38+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
39+
defer cancel()
40+
41+
c, err := winio.DialPipeContext(ctx, address)
42+
if os.IsNotExist(err) {
43+
return nil, errors.Wrap(os.ErrNotExist, "npipe not found on reconnect")
44+
} else if err == context.DeadlineExceeded {
45+
return nil, errors.Wrapf(err, "timed out waiting for npipe %s", address)
46+
} else if err != nil {
47+
return nil, err
48+
}
49+
return c, nil
50+
}
51+
3652
// AnonDialer returns a dialer for a npipe
3753
func AnonDialer(address string, timeout time.Duration) (net.Conn, error) {
3854
ctx, cancel := context.WithTimeout(context.Background(), timeout)

runtime/v2/shim_unix.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ package v2
2121
import (
2222
"context"
2323
"io"
24+
"net"
2425
"path/filepath"
26+
"time"
2527

2628
"github.com/containerd/fifo"
2729
"golang.org/x/sys/unix"
2830
)
2931

30-
func openShimLog(ctx context.Context, bundle *Bundle) (io.ReadCloser, error) {
32+
func openShimLog(ctx context.Context, bundle *Bundle, _ func(string, time.Duration) (net.Conn, error)) (io.ReadCloser, error) {
3133
return fifo.OpenFifo(ctx, filepath.Join(bundle.Path, "log"), unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
3234
}
3335

runtime/v2/shim_windows.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"time"
2727

2828
"github.com/containerd/containerd/namespaces"
29-
client "github.com/containerd/containerd/runtime/v2/shim"
3029
"github.com/pkg/errors"
3130
)
3231

@@ -64,7 +63,7 @@ func (dpc *deferredPipeConnection) Close() error {
6463

6564
// openShimLog on Windows acts as the client of the log pipe. In this way the
6665
// containerd daemon can reconnect to the shim log stream if it is restarted.
67-
func openShimLog(ctx context.Context, bundle *Bundle) (io.ReadCloser, error) {
66+
func openShimLog(ctx context.Context, bundle *Bundle, dialer func(string, time.Duration) (net.Conn, error)) (io.ReadCloser, error) {
6867
ns, err := namespaces.NamespaceRequired(ctx)
6968
if err != nil {
7069
return nil, err
@@ -74,7 +73,7 @@ func openShimLog(ctx context.Context, bundle *Bundle) (io.ReadCloser, error) {
7473
}
7574
dpc.wg.Add(1)
7675
go func() {
77-
c, conerr := client.AnonDialer(
76+
c, conerr := dialer(
7877
fmt.Sprintf("\\\\.\\pipe\\containerd-shim-%s-%s-log", ns, bundle.ID),
7978
time.Second*10,
8079
)

0 commit comments

Comments
 (0)