Skip to content

Commit 3c7af39

Browse files
committed
plugins: cleanup sockets when done
Since 509123f, we've been leaking sockets in the filesystem on platforms where abstract sockets aren't supported. That change relied on Go to cleanup our sockets for us, which Go will happily do as long as we make sure to close the listener, which we weren't previously doing unless to signal the plugin to terminate. This change adds a deferred call to `PluginServer.Close()`, which makes sure we close the plugin server at the end of the plugin execution, so that we never exit without cleaning up. Signed-off-by: Laura Brehm <laurabrehm@hey.com>
1 parent 591bd17 commit 3c7af39

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

cli-plugins/socket/socket.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"os"
1010
"runtime"
1111
"sync"
12+
13+
"github.com/sirupsen/logrus"
1214
)
1315

1416
// EnvKey represents the well-known environment variable used to pass the
@@ -92,6 +94,7 @@ func (pl *PluginServer) Addr() net.Addr {
9294
//
9395
// The error value is that of the underlying [net.Listner.Close] call.
9496
func (pl *PluginServer) Close() error {
97+
logrus.Debug("Closing plugin server")
9598
// Close connections first to ensure the connections get io.EOF instead
9699
// of a connection reset.
97100
pl.closeAllConns()
@@ -107,6 +110,10 @@ func (pl *PluginServer) closeAllConns() {
107110
pl.mu.Lock()
108111
defer pl.mu.Unlock()
109112

113+
if pl.closed {
114+
return
115+
}
116+
110117
// Prevent new connections from being accepted.
111118
pl.closed = true
112119

cmd/docker/docker.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
245245
if err == nil {
246246
plugincmd.Env = append(plugincmd.Env, socket.EnvKey+"="+srv.Addr().String())
247247
}
248+
defer func() {
249+
// Close the server when plugin execution is over, so that in case
250+
// it's still open, any sockets on the filesystem are cleaned up.
251+
_ = srv.Close()
252+
}()
248253

249254
// Set additional environment variables specified by the caller.
250255
plugincmd.Env = append(plugincmd.Env, envs...)

0 commit comments

Comments
 (0)