Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fragments/1747938268-fix-diag-race-condition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
kind: bug-fix
summary: Address a race condition that can occur in Agent diagnostics if log rotation runs while logs are being zipped.
component: elastic-agent
pull_request: https://github.com/elastic/elastic-agent/pull/8215
9 changes: 7 additions & 2 deletions internal/pkg/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func writeErrorResult(zw *zip.Writer, path string, errBody string) error {
if err != nil {
return fmt.Errorf("error writing header for error.txt file for component: %w", err)
}
_, err = w.Write([]byte(fmt.Sprintf("%s\n", errBody)))
_, err = fmt.Fprintf(w, "%s\n", errBody)
if err != nil {
return fmt.Errorf("error writing error.txt file for component: %w", err)
}
Expand Down Expand Up @@ -507,7 +507,12 @@ func zipLogsWithPath(pathsHome, commitName string, collectServices, excludeEvent
return nil
}

return saveLogs(name, path, zw)
// Add the file to the zip.
// Ignore files that don't exist to account for races with log rotation.
if err := saveLogs(name, path, zw); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
})
}

Expand Down