Skip to content

Raise a clear error when xdist is enabled after pytest-cov has started - #749

Open
pctablet505 wants to merge 2 commits into
pytest-dev:masterfrom
pctablet505:fix-xdist-configure-node-crash-740
Open

pctablet505 wants to merge 2 commits into
pytest-dev:masterfrom
pctablet505:fix-xdist-configure-node-crash-740

Conversation

@pctablet505

@pctablet505 pctablet505 commented Jul 16, 2026 •

Copy link
Copy Markdown

Fixes #740

If a plugin turns on pytest-xdist after pytest-cov has started, pytest-cov crashes with INTERNALERROR> AttributeError: 'Central' object has no attribute 'configure_node'. In #740 the plugin is pytest-enabler, in setuptools' test setup. With this change pytest-cov raises a DistCovError instead, which explains the cause and the config change that avoids it.

On scope: the reporter's setup still won't run. It fails with a clear message instead of an internal error. Making that case actually work is a bigger change, covered under Notes.

Problem

pytest-cov picks its controller in its tryfirst pytest_load_initial_conftests, based on the xdist options visible at that moment. If -n (or --dist with --tx) is present, it uses DistMaster, otherwise Central. It has to start that early so that modules imported while conftests load are measured. When another plugin adds -n after that, xdist starts workers and calls pytest_configure_node on a Central controller, which doesn't have that method.

Fix

pytest_configure_node and pytest_testnodedown now check that the controller is a DistMaster, and raise DistCovError if it isn't. The error message says to make -n/--dist visible before pytest-cov starts: put them on the command line, or in the addopts that pytest reads itself rather than ones a plugin injects later. Runs where xdist is configured normally don't change. CHANGELOG and AUTHORS are updated.

Tests

test_xdist_enabled_after_initial_conftests loads a plugin with -p. That plugin's pytest_load_initial_conftests appends -n 2 after pytest-cov's hook has run. The test asserts that the output contains pytest_cov.DistCovError: pytest-xdist workers were started and does not contain AttributeError. On master it fails with the original AttributeError. The rest of tests/test_pytest_cov.py passed locally, except test_celery, which fails the same way on master because Docker/testcontainers isn't available in my environment.

Notes

The other way to fix this is to support the late case: the first time pytest_configure_node fires on a Central controller, promote it to DistMaster in place. That would make the #740 setup work. It is a behaviour change in the engine, though, and would need tests for both collocated and remote workers, so this PR stops at the error. Do you want that instead of (or after) this? If you'd rather keep #740 open for that, I'll change Fixes to Refs.

@yangfan-yf-yf yangfan-yf-yf left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the late-xdist path on 98cc2f6.

  • python -m pytest tests/test_pytest_cov.py::test_xdist_enabled_after_initial_conftests -q passed on Windows/Python 3.12.
  • Ruff linting and formatting checks pass for the changed plugin and test files, and git diff --check is clean.

The guard is limited to the two distributed-only hooks, preserves the already-started coverage controller, and turns the previously uncaught missing-method path into the project’s existing DistCovError with actionable configuration guidance. The changelog and contributor entries follow the repository contribution checklist.

@pctablet505
pctablet505 marked this pull request as ready for review July 27, 2026 10:42
If another plugin enables pytest-xdist only after pytest-cov's own
pytest_load_initial_conftests has already run (for example a plugin that
reacts to addopts from a config file), pytest-cov had already committed
to centralised coverage measurement. When xdist then called
pytest_configure_node, this crashed with a cryptic
"AttributeError: 'Central' object has no attribute 'configure_node'".

Detect this mismatch and raise the existing DistCovError with an
actionable message instead.

Fixes pytest-dev#740
@pctablet505
pctablet505 force-pushed the fix-xdist-configure-node-crash-740 branch from 98cc2f6 to b334857 Compare September 28, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Combining pytest-cov and pytest-xdist leads to internal errors on setuptools

2 participants