python / python/cpython

TemporaryDirectory ignore_cleanup_errors flag is not robustic

オープン
#135,812 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

stdlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

When using ignore_cleanup_errors flag in TemporaryDirectory context manager, there's a path that will raise an issue.

In the onerror function that's being called when error in rmtree function:
If the first error is FileNotFound, pass
If it's everything else, check the ignore_errors flag, and raise if set to False
if the error was PermissionsError first try to change the permissions (the call to _resetperms , and then try to remove the files again. But the the call to resetparams is in an outer try, which is a lot weaker, and catching only the FileNotFoundError . But in our case, this very call raised PermissionError (yet again). It was not caught and raised back to our code.

Internal logs

2025-06-16 08:07:41 - ERROR - failed to solve strawman_local_enumeration: [Errno 1] Operation not permitted: '/tmp/tmpsrelpq9p/__pycache__'
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/shutil.py", line 701, in _rmtree_safe_fd
    os.unlink(entry.name, dir_fd=topfd)
PermissionError: [Errno 13] Permission denied: 'main.cpython-310.pyc'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/work/CTF/CTF/src/solvers/solvers/solve_remote/solve_remote.py", line 212, in solve_remote_single
    return solve_challenge_on_local_solver(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

...

 File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/tempfile.py", line 287, in _resetperms
    _dont_follow_symlinks(_os.chmod, path, 0o700)
  File "/opt/hostedtoolcache/Python/3.11.8/x64/lib/python3.11/tempfile.py", line 278, in _dont_follow_symlinks
    func(path, *args)
PermissionError: [Errno 1] Operation not permitted: '/tmp/tmpsrelpq9p/__pycache__'
CPython versions tested on:

3.11

Operating systems tested on:

macOS, Linux

Linked PRs
  • gh-150696

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

調査の方向性

tempfile.py の _resetperms と TemporaryDirectory のクリーンアップパスから始め、shutil.rmtree の onerror 処理と比較してください。macOS または Linux 上の Python 3.11 で PermissionError のケースを再現し、ignore_cleanup_errors によってクリーンアップの失敗が外部に伝播しないことを示す回帰テストを追加してください。

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

評価

技術スタック
python
領域
operating-systems
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

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

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