You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unhandled exceptions in pytest_unconfigure hooks exit 1 and not 3 - ExitCode.INTERNAL_ERROR #9808
changed the title [-]Unhandled exceptions in `pytest_unconfigure` hooks exit `1` and not `3 - ExitCode. INTERNAL_ERROR`[/-][+]Unhandled exceptions in `pytest_unconfigure` hooks exit `1` and not `3` - `ExitCode.INTERNAL_ERROR`[/+]on Apr 29, 2022
@RonnyPfannschmidt@nicoddemus my question is: what is the best approach here in 'solving' it? do we need to guard the cleanup (pytest_unconfigure and registered callables on the config? There is a lot of guarding on on the setup/runtestloop aspects of things and none here as Ronny mentions, on the 'it's too late for errors' could you elaborate that and how you would expect it to be handled?
Hi! I'm Rafael, a college student from Brasil, and I'd like to work on this issue.
Looking at the code, I think the cause is in wrap_session (src/_pytest/main.py):
exceptions during the session are caught and turned into INTERNAL_ERROR, but the
call to config._ensure_unconfigure() at the end of the finally block is not
protected, so the exception escapes and Python exits with code 1.
My plan is to wrap that call in a try/except, similar to how pytest_sessionfinish
is handled right above it, and add a test in testing/test_main.py.
Would a PR for this be welcome?
PS.: English is not my native language, so I had to ask an agent to translate this message
Activity
RonnyPfannschmidt commentedon Mar 22, 2022
pytest/src/_pytest/main.py
Lines 255 to 311 in c326c04
we pretty much dont handle exceptons from that particualr hook
on a sidenote, i also beleive its not expected that it causes issues in the form of exceptions - its too late for errors
[-]Unhandled exceptions in `pytest_unconfigure` hooks exit `1` and not `3 - ExitCode. INTERNAL_ERROR`[/-][+]Unhandled exceptions in `pytest_unconfigure` hooks exit `1` and not `3` - `ExitCode.INTERNAL_ERROR`[/+]symonk commentedon Apr 29, 2022
I've just started having a look at this one; heres an example test to prove out the case:
@RonnyPfannschmidt @nicoddemus my question is: what is the best approach here in 'solving' it? do we need to guard the cleanup (
pytest_unconfigureand registered callables on the config? There is a lot of guarding on on the setup/runtestloop aspects of things and none here as Ronny mentions, on the 'it's too late for errors' could you elaborate that and how you would expect it to be handled?Thanks!
rafael-g-v commentedon Oct 15, 2026
Hi! I'm Rafael, a college student from Brasil, and I'd like to work on this issue.
Looking at the code, I think the cause is in
wrap_session(src/_pytest/main.py):exceptions during the session are caught and turned into INTERNAL_ERROR, but the
call to
config._ensure_unconfigure()at the end of thefinallyblock is notprotected, so the exception escapes and Python exits with code 1.
My plan is to wrap that call in a try/except, similar to how
pytest_sessionfinishis handled right above it, and add a test in testing/test_main.py.
Would a PR for this be welcome?
PS.: English is not my native language, so I had to ask an agent to translate this message
Treat errors in pytest_unconfigure as internal errors
rafael-g-v commentedon Oct 19, 2026
I opened #15186 to fix this. Any feedback is welcome!