Skip to content

Warn when writing or closing a cache file fails - #15044

Merged
RonnyPfannschmidt merged 2 commits into
pytest-dev:mainfrom
barveP:codex/pytest-second-fix
Sep 18, 2026
Merged

RonnyPfannschmidt merged 2 commits into
pytest-dev:mainfrom
barveP:codex/pytest-second-fix

Conversation

@barveP

@barveP barveP commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #15043.

cache.set() already warns when opening a cache file fails, but an OSError from writing or closing it currently escapes. Move the existing write into the same error handler so these failures also produce PytestCacheWarning.

The regression test covers open, write, and close failures. The write and close cases fail before the fix; all three pass afterward. A separate reproduction using a real file-size limit also fails on pytest 9.1.1 and unmodified main, then produces warnings with this fix.

Checks on macOS:

  • Full Python 3.13 suite, tox -e py313 -- -q: 4,607 passed, 51 skipped, 15 xfailed, 5 xpassed.
  • Cache tests on Python 3.12 and 3.13: 68 passed on each.
  • tox -e linting: passed.

JSON serialization and warning filters keep their existing behavior.

Co-developed by Poorva Barve and Codex.

Co-authored-by: Codex <noreply@openai.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 16, 2026

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Comment thread src/_pytest/cacheprovider.py Outdated
data = json.dumps(value, ensure_ascii=False, indent=2)
try:
f = path.open("w", encoding="UTF-8")
with path.open("w", encoding="UTF-8") as f:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick we now can use path.write_text

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes! switched to path.write_text

Co-authored-by: Codex <noreply@openai.com>
@RonnyPfannschmidt
RonnyPfannschmidt merged commit 6a0de9b into pytest-dev:main Sep 18, 2026
36 checks passed
mtcd001-tech added a commit to mtcd001-tech/pytest that referenced this pull request Sep 20, 2026
Warn when writing or closing a cache file fails (pytest-dev#15044)
@ege-arhan

Copy link
Copy Markdown

Nice consolidation — collapsing the open/write/close paths into a single write_text call inside the existing OSError handler removes the hole where write/close failures escaped as raw tracebacks, and the parametrized test over all three failure modes pins it down well.

Two observations from reading the final state of cacheprovider.py: first, there is now a deliberate asymmetry between get (line ~218, catches OSError and silently returns the default) and set (warns via PytestCacheWarning). That reads correct to me — a cold or unreadable cache should degrade silently, while a failed write means data the user expected to persist is being dropped — but it might be worth one sentence in the set docstring stating the contract explicitly, since a future refactor could easily "harmonize" them in the wrong direction.

Second, on the test itself: it patches Path.open while the implementation calls Path.write_text, which works only because CPython's write_text delegates to open internally. If a future change switched set to os.write on an fd or to atomic write-then-rename, the mocked failure injection would silently stop failing and the test would pass vacuously (warning still raised? no — no exception, no warning, pytest.warns fails, so actually it would catch it). Never mind — pytest.warns as context manager fails the test when no warning fires, so the test is self-guarding. Leaving this note mostly as confirmation the coverage is sound.

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.

cache.set() raises on write and close errors instead of warning

3 participants