Skip to content

ResourceWarning/PytestUnraisableExceptionWarning with version 1.7.5 #106

Description

@victorlin

Pytest functions decorated with @pytest.mark.forked that were working fine on version 1.6.0 (previous PyPI release) are now failing on 1.7.5 (full error at the end):

ExceptionGroup: multiple unraisable exception warnings (2 sub-exceptions)
ResourceWarning: unclosed file <_io.TextIOWrapper name='.../stderr' mode='w' encoding='UTF-8'>

I believe it's due to this code which calls flush() without close(), introduced in c33e4e1:

finally:
try:
sys.stdout.flush()
sys.stderr.flush()
except Exception:
pass

The test fails with an ExceptionGroup containing two PytestUnraisableExceptionWarning errors, one each for unclosed stdout and stderr temp files.

Full error
============================= test session starts ==============================
platform darwin -- Python 3.14.6, pytest-9.1.1, pluggy-1.6.0
rootdir: …
configfile: pytest.ini
plugins: anyio-4.14.0, forked-1.7.5
collected 1 item

tests/open_browser.py E                                                  [100%]

==================================== ERRORS ====================================
________________________ ERROR at setup of pytest_fork _________________________
+ Exception Group Traceback (most recent call last):
  |   File "…/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 361, in from_call
  |     result: TResult | None = func()
  |                              ~~~~^^
  |   File "…/.venv/lib/python3.14/site-packages/_pytest/runner.py", line 250, in <lambda>
  |     lambda: runtest_hook(item=item, **kwds),
  |             ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/pluggy/_hooks.py", line 512, in __call__
  |     return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
  |            ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/pluggy/_manager.py", line 120, in _hookexec
  |     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  |            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 167, in _multicall
  |     raise exception
  |   File "…/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 139, in _multicall
  |     teardown.throw(exception)
  |     ~~~~~~~~~~~~~~^^^^^^^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/_pytest/logging.py", line 858, in pytest_runtest_setup
  |     yield
  |   File "…/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 139, in _multicall
  |     teardown.throw(exception)
  |     ~~~~~~~~~~~~~~^^^^^^^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/_pytest/capture.py", line 895, in pytest_runtest_setup
  |     return (yield)
  |             ^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/pluggy/_callers.py", line 121, in _multicall
  |     res = hook_impl.function(*args)
  |   File "…/.venv/lib/python3.14/site-packages/_pytest/unraisableexception.py", line 178, in pytest_runtest_setup
  |     collect_unraisable(item.config)
  |     ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  |   File "…/.venv/lib/python3.14/site-packages/_pytest/unraisableexception.py", line 81, in collect_unraisable
  |     raise ExceptionGroup("multiple unraisable exception warnings", errors)
  | ExceptionGroup: multiple unraisable exception warnings (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "…/.venv/lib/python3.14/site-packages/_pytest/capture.py", line 425, in resume
    |     setattr(sys, self.name, self.tmpfile)
    |     ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | ResourceWarning: unclosed file <_io.TextIOWrapper name='/var/folders/mx/t3rtb4cn7xxfz8w_pcfdffhm0000gn/T/tmpq33e1t7y/stderr' mode='w' encoding='UTF-8'>
    | 
    | The above exception was the direct cause of the following exception:
    | 
    | Traceback (most recent call last):
    |   File "…/.venv/lib/python3.14/site-packages/_pytest/unraisableexception.py", line 67, in collect_unraisable
    |     warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
    |     ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | pytest.PytestUnraisableExceptionWarning: Exception ignored while finalizing file <_io.FileIO name='/var/folders/mx/t3rtb4cn7xxfz8w_pcfdffhm0000gn/T/tmpq33e1t7y/stderr' mode='wb' closefd=True>: None
    | 
    +---------------- 2 ----------------
    | Traceback (most recent call last):
    |   File "…/.venv/lib/python3.14/site-packages/_pytest/capture.py", line 425, in resume
    |     setattr(sys, self.name, self.tmpfile)
    |     ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | ResourceWarning: unclosed file <_io.TextIOWrapper name='/var/folders/mx/t3rtb4cn7xxfz8w_pcfdffhm0000gn/T/tmpq33e1t7y/stdout' mode='w' encoding='UTF-8'>
    | 
    | The above exception was the direct cause of the following exception:
    | 
    | Traceback (most recent call last):
    |   File "…/.venv/lib/python3.14/site-packages/_pytest/unraisableexception.py", line 67, in collect_unraisable
    |     warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
    |     ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | pytest.PytestUnraisableExceptionWarning: Exception ignored while finalizing file <_io.FileIO name='/var/folders/mx/t3rtb4cn7xxfz8w_pcfdffhm0000gn/T/tmpq33e1t7y/stdout' mode='wb' closefd=True>: None
    | 
    +------------------------------------

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions