Fixture finalizers are skipped when setup is interrupted before `cached_result` is assigned
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- python
- Domain
- testing-qa
Research direction
Start by running the two-file reproduction in conftest.py and test_example.py, then trace pytest_fixture_setup, which the issue identifies as the relevant entry point. Confirm the fix by rerunning pytest and verifying that finalizer-ran is created after the interrupted setup.
Written by the indexing model from the issue text.
Description
Summary
If a fixture registers a finalizer and its setup is interrupted by KeyboardInterrupt or another BaseException that is not handled by pytest_fixture_setup, the registered finalizer is not executed during teardown. This can leave resources created before the
interruption open or otherwise unreleased.
Reproduction Code
Create these two files in an empty directory.
conftest.py:
from pathlib import Path
import pytest
MARKER = Path(__file__).with_name("finalizer-ran")
@pytest.fixture
def resource(request):
request.addfinalizer(lambda: MARKER.write_text("ran", encoding="utf-8"))
raise KeyboardInterrupt
test_example.py:
def test_setup(resource):
pass
Run pytest, then check whether the finalizer marker was created:
python -m pytest -q test_example.py
python -c "from pathlib import Path; print(Path('finalizer-ran').exists())"
Actual Behavior
Pytest stops with the KeyboardInterrupt from fixture setup and exits with code 2.
The finalizer marker is not created:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
path/to/conftest.py:12: KeyboardInterrupt
no tests ran in 1.38s
False
Expected Behavior
Once request.addfinalizer() has successfully registered a finalizer, pytest should run that finalizer during teardown even if the fixture setup later raises an interruption.The marker should be created before pytest exits.
This is also the documented behavior for request.addfinalizer(): pytest's fixture documentation states that a finalizer is run once it has been added, even if the fixture raises after adding it.
Environment
The repro requires no third-party plugins. Verified on a clean virtualenv:
$ python -m pytest --version
pytest 9.1.1
$ python -m pip list
Package Version
--------- -------
iniconfig 2.3.0
packaging 26.3
pip 26.2.1
pluggy 1.6.0
Pygments 2.21.0
pytest 9.1.1
$ python -VV
Python 3.12.3 (main, Jan 22 2026, 20:57:42) [GCC 13.3.0]
$ uname -srm
Linux 6.8.0-124-generic x86_64
$ grep PRETTY_NAME /etc/os-release
PRETTY_NAME="Ubuntu 24.04.4 LTS"
- Dominant language
- Python
- Stars
- 14.6k
- Forks
- 3.4k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 37
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytest-dev/pytest
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pytest-dev/pytest#15099 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
pytest-dev/pytest#14514 · 6 comments ·
Maintainers usually reply within 1 day
-
type: enhancement type: feature-branch
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pytest-dev/pytest#14186 · 2 comments ·
Maintainers usually reply within 1 day
-
type: docs
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pytest-dev/pytest#9825 · 7 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
pytest-dev/pytest#15097 · 1 comment ·
Maintainers usually reply within 1 day
All issues in pytest-dev/pytest
Similar issues
-
upstream update
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
conan-io/conan-center-index#31098 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
john-kurkowski/tldextract#382 ·
-
comp/tools duplicate P2 sweeper:risk-compatibility tool/mcp type/bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
NousResearch/hermes-agent#132042 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
deepset-ai/haystack#13092 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
feder-cr/invisible_playwright_mcp#1408 ·
Maintainers usually reply within 1 day