Merge pull request #5146 from laurazard/close-plugin-server

plugins: cleanup sockets when done
This commit is contained in:
Sebastiaan van Stijn 2024-06-12 15:00:55 +02:00 committed by GitHub
commit 9dabf16f76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View File

@ -9,6 +9,8 @@ import (
"os" "os"
"runtime" "runtime"
"sync" "sync"
"github.com/sirupsen/logrus"
) )
// EnvKey represents the well-known environment variable used to pass the // EnvKey represents the well-known environment variable used to pass the
@ -30,6 +32,7 @@ func NewPluginServer(h func(net.Conn)) (*PluginServer, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
logrus.Trace("Plugin server listening on ", l.Addr())
if h == nil { if h == nil {
h = func(net.Conn) {} h = func(net.Conn) {}
@ -92,6 +95,7 @@ func (pl *PluginServer) Addr() net.Addr {
// //
// The error value is that of the underlying [net.Listner.Close] call. // The error value is that of the underlying [net.Listner.Close] call.
func (pl *PluginServer) Close() error { func (pl *PluginServer) Close() error {
logrus.Trace("Closing plugin server")
// Close connections first to ensure the connections get io.EOF instead // Close connections first to ensure the connections get io.EOF instead
// of a connection reset. // of a connection reset.
pl.closeAllConns() pl.closeAllConns()
@ -107,6 +111,10 @@ func (pl *PluginServer) closeAllConns() {
pl.mu.Lock() pl.mu.Lock()
defer pl.mu.Unlock() defer pl.mu.Unlock()
if pl.closed {
return
}
// Prevent new connections from being accepted. // Prevent new connections from being accepted.
pl.closed = true pl.closed = true

View File

@ -245,6 +245,11 @@ func tryPluginRun(ctx context.Context, dockerCli command.Cli, cmd *cobra.Command
if err == nil { if err == nil {
plugincmd.Env = append(plugincmd.Env, socket.EnvKey+"="+srv.Addr().String()) 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. // Set additional environment variables specified by the caller.
plugincmd.Env = append(plugincmd.Env, envs...) plugincmd.Env = append(plugincmd.Env, envs...)