From 1794e60ff93ea31a7e8382b2f756f56e3ef8e156 Mon Sep 17 00:00:00 2001 From: Tiago Queiroz Date: Thu, 21 Jul 2022 12:33:41 +0200 Subject: [PATCH] Fix race condition when reloading configuration This commit fixes the race condition introduced by f3d1010029d436807c9cc90234c79e724cfe3f39 that aimed to fix some race conditions. A test has its logging updated to use t.Log, so it only logs when verbosity is on and test line issuing the log is also shown. Some t.Helper() calls are also added. --- CHANGELOG.next.asciidoc | 1 + libbeat/autodiscover/autodiscover_test.go | 4 +++- libbeat/cfgfile/list.go | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index e01d46724101..0caba88b707b 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -43,6 +43,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff] - Expand fields in `decode_json_fields` if target is set. {issue}31712[31712] {pull}32010[32010] - Fix OS name reported by add_host_metadata on Windows 11. {issue}30833[30833] {pull}32259[32259] - Fix race condition when reloading runners {pull}32309[32309] +- Fix race condition when stopping runners {pull}32433[32433] *Auditbeat* diff --git a/libbeat/autodiscover/autodiscover_test.go b/libbeat/autodiscover/autodiscover_test.go index 1cc8bbe8b6d8..fea544e09e36 100644 --- a/libbeat/autodiscover/autodiscover_test.go +++ b/libbeat/autodiscover/autodiscover_test.go @@ -533,7 +533,7 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) { wait(t, func() bool { return len(adapter.Runners()) == 3 }) runners = adapter.Runners() // Ensure the first config is the same as before - fmt.Println(runners) + t.Log(runners) assert.Equal(t, len(runners), 3) assert.Equal(t, len(autodiscover.configs["mock:foo"]), 2) check(t, runners, conf.MustNewConfigFrom(map[string]interface{}{"a": "b"}), true, false) @@ -571,6 +571,7 @@ func TestAutodiscoverWithMutlipleEntries(t *testing.T) { } func wait(t *testing.T, test func() bool) { + t.Helper() sleep := 20 * time.Millisecond ready := test() for !ready && sleep < 10*time.Second { @@ -585,6 +586,7 @@ func wait(t *testing.T, test func() bool) { } func check(t *testing.T, runners []*mockRunner, expected *conf.C, started, stopped bool) { + t.Helper() for _, r := range runners { if reflect.DeepEqual(expected, r.config) { ok1 := assert.Equal(t, started, r.started) diff --git a/libbeat/cfgfile/list.go b/libbeat/cfgfile/list.go index 76608bfd8e07..e99aacddad98 100644 --- a/libbeat/cfgfile/list.go +++ b/libbeat/cfgfile/list.go @@ -87,11 +87,11 @@ func (r *RunnerList) Reload(configs []*reload.ConfigWithMeta) error { wg.Add(1) r.logger.Debugf("Stopping runner: %s", runner) delete(r.runners, hash) - go func() { + go func(runner Runner) { defer wg.Done() runner.Stop() r.logger.Debugf("Runner: '%s' has stopped", runner) - }() + }(runner) moduleStops.Add(1) }