From 909a23cb85b323541d59670b42a493010a2e619b Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sun, 15 Oct 2023 18:43:14 +0200 Subject: [PATCH] Don't send on a closed channel --- network/engine.go | 13 ++++++++++--- network/engine_test.go | 2 -- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/network/engine.go b/network/engine.go index be2a5c5e..6f8192ee 100644 --- a/network/engine.go +++ b/network/engine.go @@ -45,9 +45,16 @@ func (engine *Engine) Stop(ctx context.Context) error { } else { engine.logger.Error().Msg("Listener is not initialized") } - engine.stopServer <- struct{}{} - close(engine.stopServer) - return err //nolint:wrapcheck + + select { + case <-engine.stopServer: + engine.logger.Info().Msg("Server stopped") + return err //nolint:wrapcheck + default: + engine.stopServer <- struct{}{} + close(engine.stopServer) + return err //nolint:wrapcheck + } } // NewEngine creates a new engine. diff --git a/network/engine_test.go b/network/engine_test.go index c985a1fd..a9ee7e2e 100644 --- a/network/engine_test.go +++ b/network/engine_test.go @@ -44,8 +44,6 @@ func TestEngine(t *testing.T) { assert.Zero(t, engine.connections) }) - t.Log(output) - // Expected output: assert.Contains(t, output, "ERR Listener is not initialized") }