python / python/cpython

TemporaryDirectory ignore_cleanup_errors flag is not robustic

未关闭
#135,812 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 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 摘要。