@@ -35,6 +35,8 @@ import (
3535 notaryclient "github.com/theupdateframework/notary/client"
3636)
3737
38+ const defaultInitTimeout = 2 * time .Second
39+
3840// Streams is an interface which exposes the standard input and output streams
3941type Streams interface {
4042 In () * streams.In
@@ -77,6 +79,7 @@ type DockerCli struct {
7779 currentContext string
7880 dockerEndpoint docker.Endpoint
7981 contextStoreConfig store.Config
82+ initTimeout time.Duration
8083}
8184
8285// DefaultVersion returns api.defaultVersion.
@@ -259,6 +262,9 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
259262}
260263
261264func newAPIClientFromEndpoint (ep docker.Endpoint , configFile * configfile.ConfigFile ) (client.APIClient , error ) {
265+ // NOTE(nick): Currently, this doesn't set any reasonable default timeouts, which
266+ // means that the CLI can hang forever on any operation. Repro steps:
267+ // https://github.com/docker/cli/issues/3652
262268 clientOpts , err := ep .ClientOpts ()
263269 if err != nil {
264270 return nil , err
@@ -313,13 +319,21 @@ func resolveDefaultDockerEndpoint(opts *cliflags.CommonOptions) (docker.Endpoint
313319 }, nil
314320}
315321
322+ func (cli * DockerCli ) getInitTimeout () time.Duration {
323+ if cli .initTimeout != 0 {
324+ return cli .initTimeout
325+ }
326+ return defaultInitTimeout
327+ }
328+
316329func (cli * DockerCli ) initializeFromClient () {
317330 ctx := context .Background ()
318- if strings .HasPrefix (cli .DockerEndpoint ().Host , "tcp://" ) {
331+ host := cli .DockerEndpoint ().Host
332+ if strings .HasPrefix (host , "tcp://" ) || strings .HasPrefix (host , "unix://" ) {
319333 // @FIXME context.WithTimeout doesn't work with connhelper / ssh connections
320334 // time="2020-04-10T10:16:26Z" level=warning msg="commandConn.CloseWrite: commandconn: failed to wait: signal: killed"
321335 var cancel func ()
322- ctx , cancel = context .WithTimeout (ctx , 2 * time . Second )
336+ ctx , cancel = context .WithTimeout (ctx , cli . getInitTimeout () )
323337 defer cancel ()
324338 }
325339
0 commit comments