[9.4](backport #51897) make statestore creation of meta.json atomic#51926
Merged
Conversation
* make statestore creation of meta.json atomic * add changelog * fix linter issues * address scenario that Anderson caught * fix linter issue (cherry picked from commit 052272d)
6 tasks
Contributor
🤖 GitHub commentsJust comment with:
|
|
Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane) |
leehinman
approved these changes
Jul 14, 2026
Contributor
Author
|
Tick the box to add this pull request to the merge queue (same as
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed commit message
WHAT: writeMetaFile in libbeat/statestore/backend/memlog previously opened the destination meta.json with O_TRUNC, wrote the JSON body, synced, and closed — all in-place on the final path. The new implementation follows the same temp-file-then-rename pattern introduced for Filebeat's registrar in #51791: os.CreateTemp creates a sibling .tmp-* file, the JSON is written and synced to it, and agentfile.SafeFileRotate atomically renames it into the final position. A deferred cleanup always removes the temp file on any error path.
WHY: With the old write pattern, if the process was killed between the O_TRUNC (which immediately empties the file) and the final Close, meta.json would be left empty or partially written. On the next start, readMetaFile would fail to parse it and the store would refuse to open. Because the rename is atomic at the OS level, the new approach guarantees that meta.json is either the previous valid version or the newly completed one — never an empty or partial file.
Checklist
stresstest.shscript to run them under stress conditions and race detector to verify their stability../changelog/fragmentsusing the changelog tool.Disruptive User Impact
None. The change is internal to the memlog backend store and the observable behavior (a valid meta.json on disk) is identical.
How to test this PR locally
go test ./libbeat/statestore/backend/memlog/... -run TestWriteMetaFileAtomic -vRelated issues
Use cases
Screenshots
Logs
This is an automatic backport of pull request #51897 done by [Mergify](https://mergify.com).