From 3c8d65aba6df407ca26f6a6a0d87ac3b8a334038 Mon Sep 17 00:00:00 2001 From: Kirill Garmanov Date: Fri, 28 Feb 2025 16:47:41 +0300 Subject: [PATCH 1/3] feat: add broken test --- .../healthcheckv2extension/extension_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/extension/healthcheckv2extension/extension_test.go b/extension/healthcheckv2extension/extension_test.go index 2f37a158689d1..8a610e541ca95 100644 --- a/extension/healthcheckv2extension/extension_test.go +++ b/extension/healthcheckv2extension/extension_test.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "io" + "net" "net/http" "os" "path/filepath" @@ -133,3 +134,23 @@ func TestNotifyConfig(t *testing.T) { require.NoError(t, err) assert.JSONEq(t, string(confJSON), string(body)) } + +func TestShutdown(t *testing.T) { + t.Run("error in http server start", func(t *testing.T) { + // Want to get error in http server start + endpoint := testutil.GetAvailableLocalAddress(t) + l, err := net.Listen("tcp", endpoint) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, l.Close()) }) + + cfg := createDefaultConfig().(*Config) + cfg.UseV2 = true + cfg.HTTPConfig.Endpoint = endpoint + + ext := newExtension(context.Background(), *cfg, extensiontest.NewNopSettings(extensiontest.NopType)) + // Get address already in use here + require.Error(t, ext.Start(context.Background(), componenttest.NewNopHost())) + + ext.Shutdown(context.Background()) + }) +} From 215d8af52be063207754fc51420a92efe51eeced Mon Sep 17 00:00:00 2001 From: Kirill Garmanov Date: Fri, 28 Feb 2025 16:49:46 +0300 Subject: [PATCH 2/3] fix: use wg instead of chan in http server --- .../healthcheckv2extension/internal/http/server.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/extension/healthcheckv2extension/internal/http/server.go b/extension/healthcheckv2extension/internal/http/server.go index 0d14bdd6e966a..5421fdd64c89e 100644 --- a/extension/healthcheckv2extension/internal/http/server.go +++ b/extension/healthcheckv2extension/internal/http/server.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "net/http" + "sync" "sync/atomic" "time" @@ -32,7 +33,7 @@ type Server struct { colconf atomic.Value aggregator *status.Aggregator startTimestamp time.Time - doneCh chan struct{} + doneWg sync.WaitGroup } var ( @@ -52,7 +53,6 @@ func NewServer( telemetry: telemetry, mux: http.NewServeMux(), aggregator: aggregator, - doneCh: make(chan struct{}), } if legacyConfig.UseV2 { @@ -96,8 +96,10 @@ func (s *Server) Start(ctx context.Context, host component.Host) error { return fmt.Errorf("failed to bind to address %s: %w", s.httpConfig.Endpoint, err) } + s.doneWg.Add(1) go func() { - defer close(s.doneCh) + defer s.doneWg.Done() + if err = s.httpServer.Serve(ln); !errors.Is(err, http.ErrServerClosed) && err != nil { componentstatus.ReportStatus(host, componentstatus.NewPermanentErrorEvent(err)) } @@ -112,7 +114,7 @@ func (s *Server) Shutdown(context.Context) error { return nil } s.httpServer.Close() - <-s.doneCh + s.doneWg.Wait() return nil } From 566c2aa73838e1d7490f82b6bb80af7b34e67bd5 Mon Sep 17 00:00:00 2001 From: Kirill Garmanov Date: Wed, 5 Mar 2025 13:51:35 +0300 Subject: [PATCH 3/3] feat: changelog entry&fix linter --- ...k.garmanov_healthcheckv2-shutdown-fix.yaml | 27 +++++++++++++++++++ .../healthcheckv2extension/extension_test.go | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .chloggen/k.garmanov_healthcheckv2-shutdown-fix.yaml diff --git a/.chloggen/k.garmanov_healthcheckv2-shutdown-fix.yaml b/.chloggen/k.garmanov_healthcheckv2-shutdown-fix.yaml new file mode 100644 index 0000000000000..59684d4fb88c6 --- /dev/null +++ b/.chloggen/k.garmanov_healthcheckv2-shutdown-fix.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: healthcheckv2extension + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix the deadlock in healthcheckv2 extension in case of an error in the healthcheckv2 Start function. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [38269] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/extension/healthcheckv2extension/extension_test.go b/extension/healthcheckv2extension/extension_test.go index 8a610e541ca95..9006a8324763d 100644 --- a/extension/healthcheckv2extension/extension_test.go +++ b/extension/healthcheckv2extension/extension_test.go @@ -151,6 +151,6 @@ func TestShutdown(t *testing.T) { // Get address already in use here require.Error(t, ext.Start(context.Background(), componenttest.NewNopHost())) - ext.Shutdown(context.Background()) + require.NoError(t, ext.Shutdown(context.Background())) }) }