Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Fixture finalizers are skipped when setup is interrupted before `cached_result` is assigned

Open
#15,067 2 comments 0 reactions 0 assignees View on GitHub

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pytest-dev/pytest

All issues in pytest-dev/pytest

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.