client_python icon indicating copy to clipboard operation
client_python copied to clipboard

write_to_textfile: tmp file is left behind if generator code fails

Open sevagh opened this issue 5 years ago • 1 comments

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.

sevagh avatar Dec 23 '20 03:12 sevagh

We could do that, or delete the file if there's an error.

brian-brazil avatar Dec 23 '20 08:12 brian-brazil