From 984ef9072c95daa41f9fc6c4d2e424c51a6ed6be Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Wed, 7 Aug 2024 14:04:31 +0100 Subject: [PATCH] plugins: don't panic on Close if PluginServer nil MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Laura Brehm (cherry picked from commit 9c4480604e0dd00b8256e23c31a5bc2af4e06eac) Signed-off-by: Paweł Gronowski --- cli-plugins/socket/socket.go | 3 +++ cli-plugins/socket/socket_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/cli-plugins/socket/socket.go b/cli-plugins/socket/socket.go index 7096b42b2e94..9a76f7a7805c 100644 --- a/cli-plugins/socket/socket.go +++ b/cli-plugins/socket/socket.go @@ -95,6 +95,9 @@ func (pl *PluginServer) Addr() net.Addr { // // The error value is that of the underlying [net.Listner.Close] call. func (pl *PluginServer) Close() error { + if pl == nil { + return nil + } logrus.Trace("Closing plugin server") // Close connections first to ensure the connections get io.EOF instead // of a connection reset. diff --git a/cli-plugins/socket/socket_test.go b/cli-plugins/socket/socket_test.go index df7b35118139..7d786abfc6f9 100644 --- a/cli-plugins/socket/socket_test.go +++ b/cli-plugins/socket/socket_test.go @@ -117,6 +117,18 @@ func TestPluginServer(t *testing.T) { assert.NilError(t, err, "failed to dial returned server") checkDirNoNewPluginServer(t) }) + + t.Run("does not panic on Close if server is nil", func(t *testing.T) { + var srv *PluginServer + defer func() { + if r := recover(); r != nil { + t.Errorf("panicked on Close") + } + }() + + err := srv.Close() + assert.NilError(t, err) + }) } func checkDirNoNewPluginServer(t *testing.T) {