Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cli-plugins/socket/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os"
"runtime"
"sync"

"github.com/sirupsen/logrus"
)

// EnvKey represents the well-known environment variable used to pass the
Expand All @@ -30,6 +32,7 @@ func NewPluginServer(h func(net.Conn)) (*PluginServer, error) {
if err != nil {
return nil, err
}
logrus.Trace("Plugin server listening on ", l.Addr())

if h == nil {
h = func(net.Conn) {}
Expand Down Expand Up @@ -92,6 +95,7 @@ func (pl *PluginServer) Addr() net.Addr {
//
// The error value is that of the underlying [net.Listner.Close] call.
func (pl *PluginServer) Close() error {
logrus.Trace("Closing plugin server")
// Close connections first to ensure the connections get io.EOF instead
// of a connection reset.
pl.closeAllConns()
Expand All @@ -107,6 +111,10 @@ func (pl *PluginServer) closeAllConns() {
pl.mu.Lock()
defer pl.mu.Unlock()

if pl.closed {
return
}

// Prevent new connections from being accepted.
pl.closed = true

Expand Down
5 changes: 5 additions & 0 deletions cmd/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
if err == nil {
plugincmd.Env = append(plugincmd.Env, socket.EnvKey+"="+srv.Addr().String())
}
defer func() {
// Close the server when plugin execution is over, so that in case
// it's still open, any sockets on the filesystem are cleaned up.
_ = srv.Close()
}()

// Set additional environment variables specified by the caller.
plugincmd.Env = append(plugincmd.Env, envs...)
Expand Down
Loading