Repository navigation
Remove log_prob's temporary output directory when the call returns - #869
Conversation
WardBrian
left a comment
There was a problem hiding this comment.
Thanks. One suggestion to make a comment more succinct.
Additionally, would you mind removing the Co-authored by metadata from the commit? Regardless of any tools used, you should be the official author of the change
| with ( | ||
| temp_single_json(data) as _data, | ||
| temp_single_json(params) as _params, | ||
| # Not under _TMPDIR, which is only removed at interpreter exit; |
There was a problem hiding this comment.
| # Not under _TMPDIR, which is only removed at interpreter exit; |
|
One thought: is there any reason we can't have this temp dir be a subdir of |
|
I think that would be a good idea |
|
Done in b228372: the directory is now |
log_prob created a directory under _TMPDIR on every call and never removed it, so a long-running process kept one directory per call until interpreter exit. The output directory is now a TemporaryDirectory nested under _TMPDIR and scoped to the call. It is removed on both the success and the error path. Closes stan-dev#867
b228372 to
d5827a3
Compare
|
Squashed to one commit with the Co-authored-by trailer removed; the tree is unchanged. |
|
Could you mark this (and your other PR) as ready to review if there are no more intended changes? |
Done. Was waiting for CI checks to finish. :) |
Submission Checklist
Summary
Closes #867.
CmdStanModel.log_probcreated its output directory withtempfile.mkdtemp(prefix=self.name, dir=_TMPDIR)and never removed it._TMPDIRis only removed at interpreter exit, so a long-running process kept one directory per call.The output directory is now a
tempfile.TemporaryDirectory(prefix=self.name, dir=_TMPDIR)in the samewithstatement as the JSON inputs. Following review, it is still nested under_TMPDIR.pd.read_csvruns inside thewithblock, so the returned DataFrame is unchanged. The directory is removed when the call returns or raises.Test:
test_lp_removes_output_dircallslog_probthree times, once with valid parameters and once with parameters that make CmdStan fail (theRuntimeErrorpath). It asserts that the entries of_TMPDIRare unchanged afterwards, and that the logged command wrote its output under_TMPDIR. Againstdevelopwithout the fix, the test fails with three leftoverbernoulli*directories in_TMPDIR.CmdStanModel.diagnosehas the samemkdtemp(..., dir=_TMPDIR)pattern. I left it alone to keep this PR to the issue. I can make the same change there, here or in a separate PR, if you want it.What I ran. All runs were in a Linux container with Python 3.12.14 and CmdStan 2.40.0, on
developat edf9fe3:isort --check-only,black --check,flake8,pylint --rcfile=.pylintrc, andmypyoncmdstanpy test: all clean.pytest test/test_log_prob.py test/test_model.py: 34 passed.pytest test, excludingtest_install_cmdstan.pyandtest_cxx_installation.py(they install toolchains) and thetest_model.pyandtest_compilation.pymodules (the first was run above): 407 passed, 3 skipped, 6 failed. None of the failures involvelog_prob:test_args_bad,test_bernoulli_bad,test_save_csvandtest_validate_dirfail because I ran as root, where read-only directories are writable. They fail the same way on unmodifieddevelopas root, and pass as a non-root user.test_sample.py::test_multi_proc_1andtest_multi_proc_2fail on unmodifieddeveloptoo, whenlogistic.stanis already compiled: the expected "Chain [1] done processing" record is not captured. They passed in an earlier run in which the test compiled the model.AI disclosure, per the Stan AI Contribution Policy: this change was prepared with Claude Code (Anthropic).
Copyright and Licensing
Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):
Aleksandr Tarutin
By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:
🤖 Generated with Claude Code