Skip to content

Do not fail the session when a tmp_path symlink cannot be resolved - #15416

Open
Jamarlli wants to merge 2 commits into
pytest-dev:mainfrom
Jamarlli:fix-dead-symlink-cleanup-oserror
Open

Jamarlli wants to merge 2 commits into
pytest-dev:mainfrom
Jamarlli:fix-dead-symlink-cleanup-oserror

Conversation

@Jamarlli

Copy link
Copy Markdown

Description

On Windows, pytest fails at the end of the session when the process runs with RedirectionGuard enabled. All tests pass, but the summary line is not printed and the session exits with code 1:

  File ".../src/_pytest/pathlib.py", line 400, in cleanup_dead_symlinks
    if not left_dir.resolve().exists():
           ^^^^^^^^^^^^^^^^^^
  File "<frozen ntpath>", line 639, in _getfinalpathname_nonstrict
OSError: [WinError 448] The path cannot be traversed because it contains an untrusted mount point: '...\\pytest-of-<user>\\pytest-3\\test_uses_tmp_pathcurrent'

(My Windows is in Spanish, so I translated the message. The error code is the original.)

I found this running the test suite of a personal project from a sandboxed agent session on Windows 11. Processes in that sandbox run with RedirectionGuard, so Windows refuses to follow any symlink or junction and returns ERROR_UNTRUSTED_MOUNT_POINT (448). In my project I worked around it by wrapping cleanup_dead_symlinks in a conftest.py.

cleanup_dead_symlinks calls Path.resolve() on every symlink in the basetemp. In non-strict mode ntpath.realpath only ignores a fixed list of Windows errors, and 448 is not in it (CPython main, Lib/ntpath.py, same on 3.12 to 3.14). So the error reaches pytest. The function runs in pytest_sessionfinish and in the exit stack of the numbered dir cleanup, so the session fails after all the tests have passed.

This PR:

  • leaves a symlink alone when resolve() raises OSError, and continues with the next entry;
  • keeps the same behaviour as before when the link can be resolved: dead links are removed and live links are kept.

This follows the idea of _force_symlink, which already ignores errors because the current links are best effort.

#15174 changes the next line (unlink(missing_ok=True)). The two changes are independent. I am happy to rebase on whichever is merged first.

How to reproduce

run_guarded.py enables RedirectionGuard in the current process and then runs pytest:

import ctypes
import sys

import pytest

# ProcessRedirectionTrustPolicy (16) with the EnforceRedirectionTrust flag.
flags = ctypes.c_ulong(1)
assert ctypes.windll.kernel32.SetProcessMitigationPolicy(16, ctypes.byref(flags), ctypes.sizeof(flags))
sys.exit(pytest.main(sys.argv[1:]))
# test_a.py
def test_uses_tmp_path(tmp_path):
    (tmp_path / "f.txt").write_text("x")
python run_guarded.py -q -p no:cacheprovider test_a.py

Python has to start from a real path. If it starts through a junction (for example the cpython-3.12-windows-x86_64-none alias that uv creates), imports fail before pytest gets to the cleanup.

Results

Windows 11, CPython 3.12.13, pytest main at 9efd012:

main this PR
run_guarded.py (one session) exit code 1, WinError 448 exit code 0, 1 passed
run_guarded.py (6 sessions in a row) exit code 0 in all of them, old numbered dirs still removed (3 kept)
without RedirectionGuard, dead link + live link dead link removed dead link removed, live link kept
new test test_cleanup_keeps_symlink_that_cannot_be_resolved fails passes
testing/test_tmpdir.py 59 passed, 9 skipped 60 passed, 9 skipped

pre-commit passes on the changed files.

I used Claude Code to investigate the problem and write the change. I have reviewed every line and I understand it. The commit credits it with a Co-authored-by trailer.

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • If AI agents were used, they are credited in Co-authored-by commit trailers.
  • Create a new changelog file in the changelog directory, with a name like <ISSUE NUMBER>.<TYPE>.rst.
  • Add yourself to AUTHORS in alphabetical order.

Jamarlli and others added 2 commits November 19, 2026 10:22
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants