Skip to content

Commit 80f96cd

Browse files
committed
runtime/v2: net.Dial gRPC shim sockets before trying grpc
This is mostly to workaround an issue with gRPC based shims after containerd restart. If a shim dies while containerd is also down/restarting, on reboot grpc.DialContext with our current set of DialOptions will make us wait for 100 seconds per shim even if the socket no longer exists or has no listener. Signed-off-by: Danny Canter <[email protected]> (cherry picked from commit 0bc9633) Signed-off-by: Danny Canter <[email protected]>
1 parent 096a0e8 commit 80f96cd

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

runtime/v2/shim.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
"net"
2526
"os"
2627
"path/filepath"
2728
"strings"
@@ -253,7 +254,7 @@ func makeConnection(ctx context.Context, params client.BootstrapParams, onClose
253254
grpc.WithTransportCredentials(insecure.NewCredentials()),
254255
grpc.WithBlock(),
255256
}
256-
return grpcDialContext(ctx, dialer.DialAddress(params.Address), onClose, gopts...)
257+
return grpcDialContext(ctx, params.Address, onClose, gopts...)
257258
default:
258259
return nil, fmt.Errorf("unexpected protocol: %q", params.Protocol)
259260
}
@@ -264,10 +265,29 @@ func makeConnection(ctx context.Context, params client.BootstrapParams, onClose
264265
// a callback run when the connection is severed or explicitly closed.
265266
func grpcDialContext(
266267
ctx context.Context,
267-
target string,
268+
address string,
268269
onClose func(),
269270
gopts ...grpc.DialOption,
270271
) (*grpcConn, error) {
272+
// If grpc.WithBlock is specified in gopts this causes the connection to block waiting for
273+
// a connection regardless of if the socket exists or has a listener when Dial begins. This
274+
// specific behavior of WithBlock is mostly undesirable for shims, as if the socket isn't
275+
// there when we go to load/connect there's likely an issue. However, getting rid of WithBlock is
276+
// also undesirable as we don't want the background connection behavior, we want to ensure
277+
// a connection before moving on. To bring this in line with the ttrpc connection behavior
278+
// lets do an initial dial to ensure the shims socket is actually available. stat wouldn't suffice
279+
// here as if the shim exited unexpectedly its socket may still be on the filesystem, but it'd return
280+
// ECONNREFUSED which grpc.DialContext will happily trudge along through for the full timeout.
281+
//
282+
// This is especially helpful on restart of containerd as if the shim died while containerd
283+
// was down, we end up waiting the full timeout.
284+
conn, err := net.DialTimeout("unix", address, time.Second*10)
285+
if err != nil {
286+
return nil, err
287+
}
288+
conn.Close()
289+
290+
target := dialer.DialAddress(address)
271291
client, err := grpc.DialContext(ctx, target, gopts...)
272292
if err != nil {
273293
return nil, fmt.Errorf("failed to create GRPC connection: %w", err)

0 commit comments

Comments
 (0)