From 82ff0c1969aec83fe9e68e40964ba08555c47d78 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Thu, 6 Mar 2025 13:23:43 +0100 Subject: [PATCH] filebeat: make deep copy before notifying of config change (#42992) This prevents concurrent read & write map access. Unrelated, but I've escalated one log line to Info to allow for easier verifying that ES store is being used from agent logs. Fixes #42815 (cherry picked from commit f1e42fcaf3e3d3f36e9cf7ded219650aaa6a8114) --- filebeat/beater/filebeat.go | 10 +++++++++- libbeat/statestore/backend/es/store.go | 2 +- x-pack/filebeat/tests/integration/managerV2_test.go | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/filebeat/beater/filebeat.go b/filebeat/beater/filebeat.go index aebff1732e91..7c6ebd5556e4 100644 --- a/filebeat/beater/filebeat.go +++ b/filebeat/beater/filebeat.go @@ -328,7 +328,15 @@ func (fb *Filebeat) Run(b *beat.Beat) error { return nil } - stateStore.notifier.Notify(outCfg.Config()) + // Create a new config with the output configuration. Since r.Config is a pointer, a copy is required to + // avoid concurrent map read and write. + // See https://github.com/elastic/beats/issues/42815 + configCopy, err := conf.NewConfigFrom(outCfg.Config()) + if err != nil { + logp.Err("Failed to create a new config from the output config: %v", err) + return nil + } + stateStore.notifier.Notify(configCopy) return nil }) } diff --git a/libbeat/statestore/backend/es/store.go b/libbeat/statestore/backend/es/store.go index fee1e0c9ba48..d4a6fd130853 100644 --- a/libbeat/statestore/backend/es/store.go +++ b/libbeat/statestore/backend/es/store.go @@ -296,7 +296,7 @@ func (s *store) Each(fn func(string, backend.ValueDecoder) (bool, error)) error } func (s *store) configure(ctx context.Context, c *conf.C) { - s.log.Debugf("Configure ES store") + s.log.Info("Configuring ES store") s.mx.Lock() defer s.mx.Unlock() diff --git a/x-pack/filebeat/tests/integration/managerV2_test.go b/x-pack/filebeat/tests/integration/managerV2_test.go index 84f6ad0f54bb..a05ee01570e0 100644 --- a/x-pack/filebeat/tests/integration/managerV2_test.go +++ b/x-pack/filebeat/tests/integration/managerV2_test.go @@ -930,7 +930,7 @@ func TestHTTPJSONInputReloadUnderElasticAgentWithElasticStateStore(t *testing.T) ) for _, contains := range []string{ - "Configure ES store", + "Configuring ES store", "input-cursor::openStore: prefix: httpjson inputID: " + inputID, "input-cursor store read 0 keys", // first, no previous data exists "input-cursor store read 1 keys", // after the restart, previous key is read