[8.19](backport #51791) make creation of meta.json file atomic#51799
Merged
Conversation
* make creation of meta.json file atomic * improve changelog summary Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> (cherry picked from commit 368c0ec)
Contributor
🤖 GitHub commentsJust comment with:
|
|
Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane) |
6 tasks
leehinman
approved these changes
Jul 9, 2026
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
filebeat/registrar/migrate.go contains two places that write meta.json during registry migration. The final step of updateToVersion1 used os.WriteFile, which truncates the target file to zero bytes on open before writing the new content. If Filebeat was killed (OOM, SIGKILL) in the window between that truncation and the write completing, meta.json was left empty.
On every subsequent restart, readVersion finds the empty file, json.Unmarshal fails with "unexpected end of JSON input", and Filebeat refuses to start. The error is permanent until the file is manually removed.
safeWriteFile (used by updateToVersion0) had the same structural risk: it opened with O_TRUNC and wrote in-place, with no atomic handoff.
Changes
safeWriteFile rewritten to use a temp-file + atomic rename pattern:
The target file's previous content is never destroyed until the new content is fully written and synced. A kill at any point leaves either the old file intact or the complete new file in place — never an empty file.
updateToVersion1 updated to call safeWriteFile instead of os.WriteFile for the final {"version": "1"} write, giving it the same crash-safety guarantee as the version-0 path.
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.
How to test this PR locally
go test ./filebeat/registrar/ -run TestSafeWriteFile -vRelated issues
Use cases
Screenshots
Logs
This is an automatic backport of pull request #51791 done by [Mergify](https://mergify.com).