Skip to content

Commit

Permalink
Merge pull request #5146 from laurazard/close-plugin-server
Browse files Browse the repository at this point in the history
plugins: cleanup sockets when done
  • Loading branch information
thaJeztah authored Jun 12, 2024
2 parents 5041626 + 3dcc653 commit 9dabf16
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
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

0 comments on commit 9dabf16

Please sign in to comment.