pytest-dev / pytest-dev/pytest

calling `logging.basicConfig` before `pytest.main()` causes `OSError: [WinError 6] The handle is invalid`

Open
#14,289 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Related: #12876 #5743 #5502 #5282 and probably a lot more

OS: Windows
Python: 3.13.7
pytest: 9.0.2

Calling logging.basicConfig() before pytest.main() and then using logging.info/warn/... inside a test will add lengthy, noisy error logs to the test's stderr capture. This noise is an obstacle in test debugging.

Minimal working example

Run the following in PowerShell:

python --version
# Python 3.13.7
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install pytest
pip show pytest
# Version: 9.0.2

@"
import logging, pytest

def test_pass():
    logging.warning('warn')

if __name__ == '__main__':
    logging.basicConfig()
    pytest.main()
"@ | Out-File -FilePath "test_main.py" -Encoding utf8
@"
import logging, pytest

@pytest.hookimpl(trylast=True)
def pytest_runtest_logreport(report):
    logging.warning(report.capstderr)
"@ | Out-File -FilePath "conftest.py" -Encoding utf8
python test_main.py
# --- Logging error ---
# Traceback (most recent call last):
#   File "C:\Users\ABrun\.pyenv\pyenv-win\versions\3.13.7\Lib\logging\__init__.py", line 1154, in emit
#     stream.write(msg + self.terminator)
#     ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
# OSError: [WinError 6] The handle is invalid

The package python-html is using the pytest_runtest_logreport hook to generate the html output:

# https://github.com/pytest-dev/pytest-html/blob/e73b6befd69f1852ec1369ef6faab5b1f0286c8e/src/pytest_html/basereport.py#L210-L212
@pytest.hookimpl(trylast=True)
def pytest_runtest_logreport(self, report):
MWE with pytest-html:
python --version
# Python 3.13.7
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install pytest pytest-html
pip show pytest
# Version: 9.0.2
pip show pytest-html
# Version: 4.2.0

@"
import logging, pytest

def test_pass():
    logging.warning('warn')

if __name__ == '__main__':
    logging.basicConfig()
    pytest.main(['--html=out.html'])
"@ | Out-File -FilePath "test_main.py" -Encoding utf8
python test_main.py
python -c "import re; print(re.search(r';log(.*?-.*?)#34', open('out.html').read()).group(1).replace('\\n', '\n'))"
# --- Logging error ---
# Traceback (most recent call last):
#   File "C:\Users\ABrun\.pyenv\pyenv-win\versions\3.13.7\Lib\logging\__init__.py", line 1154, in emit
#     stream.write(msg + self.terminator)
#     ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
# OSError: [WinError 6] The handle is invalid

A workaround would be appreciated. I have tried the ones from the other issues but they did not prevent that error message. The comment https://github.com/pytest-dev/pytest/issues/13720#issuecomment-3284628303 mentions caplog. Is there a way to use caplog to avoid that error message?

Contributor guide

Open the contributing guide

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.

Research direction

Start by reproducing the Windows failure with the test_main.py and conftest.py minimal examples, focusing on logging.basicConfig(), pytest.main(), and pytest_runtest_logreport. Read the referenced caplog discussion and hook behavior first. Done means the reproduction no longer emits noisy OSError: [WinError 6] logging errors while captured test output and pytest-html reporting still work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.