plugins: fix plugin socket being closed before use

Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
Laura Brehm 2024-01-15 14:29:48 +00:00
parent 5f6c55a724
commit 508346ef61
No known key found for this signature in database
GPG Key ID: CFBF847B4A313468
2 changed files with 8 additions and 8 deletions

View File

@ -14,21 +14,20 @@ import (
const EnvKey = "DOCKER_CLI_PLUGIN_SOCKET" const EnvKey = "DOCKER_CLI_PLUGIN_SOCKET"
// SetupConn sets up a Unix socket listener, establishes a goroutine to handle connections // 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. // and update the conn pointer, and returns the listener for the socket (which the caller
func SetupConn(conn **net.UnixConn) (string, error) { // 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()) listener, err := listen("docker_cli_" + uuid.Generate().String())
if err != nil { if err != nil {
return "", err return nil, err
} }
accept(listener, conn) accept(listener, conn)
return EnvKey + "=" + listener.Addr().String(), nil return listener, nil
} }
func accept(listener *net.UnixListener, conn **net.UnixConn) { func accept(listener *net.UnixListener, conn **net.UnixConn) {
defer listener.Close()
go func() { go func() {
for { for {
// ignore error here, if we failed to accept a connection, // ignore error here, if we failed to accept a connection,

View File

@ -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. // Establish the plugin socket, adding it to the environment under a well-known key if successful.
var conn *net.UnixConn var conn *net.UnixConn
socketenv, err := socket.SetupConn(&conn) listener, err := socket.SetupConn(&conn)
if err == nil { if err == nil {
envs = append(envs, socketenv) envs = append(envs, socket.EnvKey+"="+listener.Addr().String())
defer listener.Close()
} }
plugincmd.Env = append(envs, plugincmd.Env...) plugincmd.Env = append(envs, plugincmd.Env...)