Skip to content

[beat] LoadMeta has a race condition. #46841

@leehinman

Description

@leehinman

The LoadMeta function has a race condition if more than one beat has the same paths defined.

tempFile := metaPath + ".new"
f, err = os.OpenFile(tempFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("failed to create Beat meta file: %w", err)
}
encodeErr := json.NewEncoder(f).Encode(meta{UUID: b.Info.ID, FirstStart: b.Info.FirstStart})
err = f.Sync()
if err != nil {
return fmt.Errorf("Beat meta file failed to write: %w", err)
}
err = f.Close()
if err != nil {
return fmt.Errorf("Beat meta file failed to write: %w", err)
}
if encodeErr != nil {
return fmt.Errorf("Beat meta file failed to write: %w", encodeErr)
}
// move temporary file into final location
err = file.SafeFileRotate(metaPath, tempFile)
return err

The os.OpenFile should be replaced with os.CreateTemp

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions