Skip to content

Commit 08baa43

Browse files
fix(registry): prevent file descriptor leak in WriteToTextfile
In WriteToTextfile, if an error occurs before the explicit tmp.Close() (e.g., Gather fails or MetricFamilyToText fails), the temporary file descriptor is never closed. The deferred os.Remove(tmp.Name()) does not close the file. Explicitly close the file on all error paths that happen before the final close, ensuring the descriptor is always released. Additionally, this fixes the issue on Windows where an open file cannot be deleted, preventing the deferred os.Remove from succeeding. Fixes: 1d54dab ("Add WriteToTextfile test") Found by PostgresPro. Signed-off-by: Maksim Korotkov <m.korotkov@postgrespro.ru>
1 parent d61fb3f commit 08baa43

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

prometheus/registry.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,12 @@ func WriteToTextfile(filename string, g Gatherer) error {
641641

642642
mfs, err := g.Gather()
643643
if err != nil {
644+
tmp.Close()
644645
return err
645646
}
646647
for _, mf := range mfs {
647648
if _, err := expfmt.MetricFamilyToText(tmp, mf); err != nil {
649+
tmp.Close()
648650
return err
649651
}
650652
}

0 commit comments

Comments
 (0)