Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

log_prob leaves a temporary output directory per call until interpreter exit

クローズ
#867 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

@atarutin がすでに取り組んでいます。

2026年10月9日 から。

  • #869 @atarutin による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
22/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
tooling

調査の方向性

The leak is in CmdStanModel.log_prob in cmdstanpy/model.py, where tempfile.mkdtemp creates an output directory under _TMPDIR that is never removed. Read how temp_single_json cleans up the neighbouring input files, then check the linked pull request #869 before starting, since it already targets this fix. Done means the output directory is removed on both the success and RuntimeError paths, and a test shows the _TMPDIR entry count is unchanged after repeated calls.

索引モデルが issue の本文から書いたものです。

説明

Summary

Each call to CmdStanModel.log_prob creates a new directory under cmdstanpy._TMPDIR and never removes it. _TMPDIR is cleaned only at interpreter exit (atexit), so a long-running process accumulates one directory, holding an output.csv, per call.

Where

cmdstanpy/model.py, CmdStanModel.log_prob (1.3.0):

output_dir = tempfile.mkdtemp(prefix=self.name, dir=_TMPDIR)
output = os.path.join(output_dir, "output.csv")
...
result = pd.read_csv(output, comment="#")
return result

Nothing removes output_dir, on either the success path or the RuntimeError path. The data and params files right beside it use temp_single_json(...) context managers and are cleaned up.

Why it matters

We run log_prob once per optimizer start, as a gradient diagnostic, inside a long-lived worker process. A single fit runs hundreds of starts, and the worker runs indefinitely. The directories grow without bound until the process exits.

Today we work around it by calling the compiled program's log_prob method directly through subprocess, with an output directory we own. That means duplicating cmdstanpy's argument handling, which we would rather not do.

Suggested fix

Let log_prob own its output directory for the duration of the call, as the JSON inputs already do:

with tempfile.TemporaryDirectory(prefix=self.name, dir=_TMPDIR) as output_dir:
    output = os.path.join(output_dir, "output.csv")
    ...
    result = pd.read_csv(output, comment="#")
return result

read_csv loads everything before the directory is removed, so the returned DataFrame is unaffected. An optional output_dir= argument, as optimize/sample have, would also let callers keep the CSV when they want it.

We are happy to open a PR with a test, for example asserting that _TMPDIR's entry count is unchanged after N calls.

Versions

cmdstanpy 1.3.0, CmdStan 2.40.0, Python 3.12, Linux x86_64.

主要言語
Python
スター
198
フォーク
82
平均マージ
4日 11時間
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

stan-dev/cmdstanpy のほかの issue

stan-dev/cmdstanpy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。