Skip to content

fix(registry): prevent file descriptor leak in WriteToTextfile#2010

Merged
kakkoyun merged 1 commit into
prometheus:mainfrom
ProjectMutilation:fix-handle-leak
Jun 1, 2026
Merged

fix(registry): prevent file descriptor leak in WriteToTextfile#2010
kakkoyun merged 1 commit into
prometheus:mainfrom
ProjectMutilation:fix-handle-leak

Conversation

@ProjectMutilation
Copy link
Copy Markdown
Contributor

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.

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>
Copy link
Copy Markdown
Member

@kakkoyun kakkoyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the contribution.

@kakkoyun kakkoyun merged commit 5654a21 into prometheus:main Jun 1, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants