prometheus / prometheus/client_python

write_to_textfile: tmp file is left behind if generator code fails

Open
#607 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.4k
Forks
876
Avg merge
8d 4h
Merged PRs (30d)
1

Description

In the write_to_textfile function, the code to run the collector registry is in the temp file creation block: https://github.com/prometheus/client_python/blob/master/prometheus_client/exposition.py#L202-L203

In my case, I had an error in my collector code (run by generate_latest(registry)). This resulted in many tmp files left over from the errors.

I'm wondering if it would be preferable to run the code before the temp file block, e.g.:

    tmppath = '%s.%s.%s' % (path, os.getpid(), threading.current_thread().ident)
    out = generate_latest(registry)
    with open(tmppath, 'wb') as f:
        f.write(out)
    # rename(2) is atomic.
    os.rename(tmppath, path)

Unless leaving temp files behind is the desired/intended behavior.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in prometheus_client/exposition.py at the write_to_textfile implementation around lines 202-203, where generate_latest(registry) runs during temporary-file creation. Trace the failure path when collector code raises and verify that the completed behavior leaves no temporary file behind while preserving the atomic rename flow.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.