[8.19](backport #51688) add_docker_metadata: fix data races in lazy cgroup cache init#51844
Conversation
add_docker_metadata can run as a beat-level (global) processor, in which case a single instance is shared by every pipeline client and its Run method is invoked concurrently. Two data races existed on that path, both reachable by default because match_pids defaults to ["process.pid", "process.parent.pid"]. lazyCgroupCacheInit guarded the cache with a plain "if d.cgroups == nil" check-then-set. Concurrent Run calls raced on the pointer, and two callers could each build a cache and start its own janitor goroutine, leaking a goroutine and orphaning a cache. Replace the field with an atomic.Pointer[common.Cache] initialized exactly once through sync.Once, and load it everywhere it is read. common.Cache.Get held only the read lock, but get updates the element's last access time on access-expiry caches (the cgroup cache is one), so concurrent Get calls mutated the same element under a shared lock. Take the exclusive lock in Get. Co-authored-by: Lee E. Hinman <lee.e.hinman@elastic.co> (cherry picked from commit e1add68) # Conflicts: # libbeat/processors/add_docker_metadata/add_docker_metadata.go # libbeat/processors/add_docker_metadata/add_docker_metadata_test.go
|
Cherry-pick of e1add68 has failed: To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally |
🤖 GitHub commentsJust comment with:
|
|
Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane) |
TL;DRThe backport is failing because commit Remediation
Investigation detailsRoot CauseThe PR head contains unresolved cherry-pick conflict sections. In Evidence
VerificationNot run; this workflow is read-only and only diagnoses the Buildkite failure. What is this? | From workflow: PR Buildkite Detective Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not. |
The 8.19 branch lacks the docker-retry feature that exists on main, so the cherry-pick of #51688 conflicted on the addDockerMetadata struct and Close(). Keep this branch's existing non-retry code and apply only the cgroup cache race fix: make cgroups an atomic.Pointer initialized once via sync.Once, add the closed flag, and take the atomic path in Close(). Also add the sync/sync/atomic imports (main file) and sync/time imports (test) that the fix now requires but that main already had. Assisted-By: Cursor
TL;DRAll three Remediation
Investigation detailsRoot CauseThe PR head at Evidence
ValidationNot run; this workflow is read-only and the diagnosis is based on the failed workflow logs and PR head contents. Follow-upThe previous detective report on this PR covered unresolved cherry-pick conflict markers in an earlier Buildkite run. The current lint failures are materially different and remain after that issue was addressed. What is this? | From workflow: PR Actions Detective Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not. |
Proposed commit message
Checklist
I have made corresponding changes to the documentationI have made corresponding change to the default configuration filesstresstest.shscript to run them under stress conditions and race detector to verify their stability../changelog/fragmentsusing the changelog tool.Disruptive User Impact
None.
How to test this PR locally
Regression test
Run the new test with the stress test script:
./script/stresstest.sh --race ./libbeat/processors/add_docker_metadata '^TestMatchPIDsConcurrent$' -p 32End-to-end reproduction
A running Docker daemon is required, otherwise
Runreturns early before reaching the cgroup cache./tmp/dmrepro/filebeat.ymlwith 24filestreaminputs (each a distinctid), anadd_fieldsthat setsprocess.pidandadd_docker_metadataas a global processor:Config
On
mainthe run aborts within a second with aWARNING: DATA RACE(exit code 66) on the shared processor's cgroup cache:This is an automatic backport of pull request #51688 done by [Mergify](https://mergify.com).