Skip to content

Commit 508346e

Browse files
committed
plugins: fix plugin socket being closed before use
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
1 parent 5f6c55a commit 508346e

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

cli-plugins/socket/socket.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@ import (
1414
const EnvKey = "DOCKER_CLI_PLUGIN_SOCKET"
1515

1616
// SetupConn sets up a Unix socket listener, establishes a goroutine to handle connections
17-
// and update the conn pointer, and returns the environment variable to pass to the plugin.
18-
func SetupConn(conn **net.UnixConn) (string, error) {
17+
// and update the conn pointer, and returns the listener for the socket (which the caller
18+
// is responsible for closing when it's no longer needed).
19+
func SetupConn(conn **net.UnixConn) (*net.UnixListener, error) {
1920
listener, err := listen("docker_cli_" + uuid.Generate().String())
2021
if err != nil {
21-
return "", err
22+
return nil, err
2223
}
2324

2425
accept(listener, conn)
2526

26-
return EnvKey + "=" + listener.Addr().String(), nil
27+
return listener, nil
2728
}
2829

2930
func accept(listener *net.UnixListener, conn **net.UnixConn) {
30-
defer listener.Close()
31-
3231
go func() {
3332
for {
3433
// ignore error here, if we failed to accept a connection,

cmd/docker/docker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ func tryPluginRun(dockerCli command.Cli, cmd *cobra.Command, subcommand string,
223223

224224
// Establish the plugin socket, adding it to the environment under a well-known key if successful.
225225
var conn *net.UnixConn
226-
socketenv, err := socket.SetupConn(&conn)
226+
listener, err := socket.SetupConn(&conn)
227227
if err == nil {
228-
envs = append(envs, socketenv)
228+
envs = append(envs, socket.EnvKey+"="+listener.Addr().String())
229+
defer listener.Close()
229230
}
230231

231232
plugincmd.Env = append(envs, plugincmd.Env...)

0 commit comments

Comments
 (0)