From 508346ef61ed7495a6eadaa1ca68b310085e126e Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Mon, 15 Jan 2024 14:29:48 +0000 Subject: [PATCH] plugins: fix plugin socket being closed before use Signed-off-by: Laura Brehm --- cli-plugins/socket/socket.go | 11 +++++------ cmd/docker/docker.go | 5 +++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cli-plugins/socket/socket.go b/cli-plugins/socket/socket.go index a7ad00746b..beb46ae3a3 100644 --- a/cli-plugins/socket/socket.go +++ b/cli-plugins/socket/socket.go @@ -14,21 +14,20 @@ import ( const EnvKey = "DOCKER_CLI_PLUGIN_SOCKET" // SetupConn sets up a Unix socket listener, establishes a goroutine to handle connections -// and update the conn pointer, and returns the environment variable to pass to the plugin. -func SetupConn(conn **net.UnixConn) (string, error) { +// and update the conn pointer, and returns the listener for the socket (which the caller +// is responsible for closing when it's no longer needed). +func SetupConn(conn **net.UnixConn) (*net.UnixListener, error) { listener, err := listen("docker_cli_" + uuid.Generate().String()) if err != nil { - return "", err + return nil, err } accept(listener, conn) - return EnvKey + "=" + listener.Addr().String(), nil + return listener, nil } func accept(listener *net.UnixListener, conn **net.UnixConn) { - defer listener.Close() - go func() { for { // ignore error here, if we failed to accept a connection, diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index f3bc230080..cfc53a6fa1 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -223,9 +223,10 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string, // Establish the plugin socket, adding it to the environment under a well-known key if successful. var conn *net.UnixConn - socketenv, err := socket.SetupConn(&conn) + listener, err := socket.SetupConn(&conn) if err == nil { - envs = append(envs, socketenv) + envs = append(envs, socket.EnvKey+"="+listener.Addr().String()) + defer listener.Close() } plugincmd.Env = append(envs, plugincmd.Env...)