Skip to content

Treat errors in pytest_unconfigure as internal errors - #15186

Open
rafael-g-v wants to merge 3 commits into
pytest-dev:mainfrom
rafael-g-v:fix-9808
Open

rafael-g-v wants to merge 3 commits into
pytest-dev:mainfrom
rafael-g-v:fix-9808

Conversation

@rafael-g-v

@rafael-g-v rafael-g-v commented Oct 15, 2026 •

Copy link
Copy Markdown

Closes #9808

Exceptions raised in pytest_unconfigure hooks escaped wrap_session and made pytest exit with code 1 and a raw traceback. Now they are reported as an internal error (exit code 3), the same as exceptions in pytest_configure.

  • Wrapped config._ensure_unconfigure() with the same handling already used
    for pytest_sessionfinish.
  • Added a test in testing/test_main.py and a changelog entry.

I'm a student; I used an AI assistant to help find the cause and write the change, and I reviewed and tested it myself. Any feedback is welcome!

@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 15, 2026
The try/except around _ensure_unconfigure() was nested under 'if initstate >= 2', so it only ran when the session had started. Some existing tests expected exit code 1 and the traceback in stderr for errors raised during config cleanup; they now expect INTERNAL_ERROR (3) and INTERNALERROR> lines in stdout. Also add a test for pytest.exit() called from pytest_unconfigure.
@rafael-g-v

Copy link
Copy Markdown
Author

I updated 4 existing tests (in test_threadexception.py and test_unraisableexception.py) that expected the old behavior: exit code 1 and the traceback in stderr. With this change, errors raised during config cleanup are reported as an internal error (exit code 3) with INTERNALERROR> lines in stdout.

Some of these tests have a TODO saying the outcome should be a test failure or error. Is exit code 3 acceptable here, or would you prefer a different handling for these cases?

The Read the Docs failure seems unrelated to this PR: the only warning is "py:class reference target not found: pluggy._compat.DistFacade", from the PluginManager.list_plugin_distinfo docstring in the new pluggy 1.7.0 release.

@rafael-g-v

Copy link
Copy Markdown
Author

Hi @nicoddemus, sorry for the ping! When you have some time, could you take a look at this PR? I'd especially like to know if changing the expected exit code in the existing unraisable/thread exception tests is acceptable. Thanks!

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.

Unhandled exceptions in pytest_unconfigure hooks exit 1 and not 3 - ExitCode.INTERNAL_ERROR

1 participant