Repository navigation
fix: tear fixtures down under their setup-time socket state - #564
Merged
Merged
Conversation
The fixture teardown tests need it too. Signed-off-by: Mike Fiedler <miketheman@gmail.com>
pytest_runtest_teardown lifts the restrictions before pytest runs fixture finalizers, so teardown always ran with the network open (#537). A pytest_fixture_setup wrapper now snapshots the socket state and registers finalizers around the fixture's own teardown: the snapshot is applied before it runs and the previous state is put back after. Each fixture tears down under the policy its setup ran under, at every scope, including xunit and unittest class/module teardown, a skipped last test, and teardown deferred by -x. Only the real socket functions and pytest-socket's own guards are swapped, so patches from monkeypatch or mock are left for those tools to undo. pytest_runtest_teardown stays a plain hook. Wrapping it instead (#557) runs class, module and session finalizers under whichever test happens to run last, so outcomes depend on test order, and on pluggy < 1.6 an inner wrapper that raises can leak restrictions into the next test. A fixture first set up by a blocked test now tears down blocked. The README explains this and the workaround: call enable_socket() at the start of its teardown. Fixes #537 Signed-off-by: Mike Fiedler <miketheman@gmail.com>
enable_socket() put socket.socket and the DNS functions back but left the connect() guard from socket_allow_hosts() in place, so a test or a fixture teardown under --allow-hosts still couldn't reach other hosts. It now does the same full reset as the per-test teardown. Signed-off-by: Mike Fiedler <miketheman@gmail.com>
This was referenced Oct 21, 2026
Contributor
Merging this PR will improve performance by 9.71%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #537. Supersedes #557.
Fixture teardown ran with the network open, because
pytest_runtest_teardownlifts the restrictions before pytest runs finalizers. Each fixture now tears down under the socket state its own setup ran under.A
pytest_fixture_setuphookwrapper snapshots the socket state and registers a pair of finalizers around the fixture's own teardown. The first applies the snapshot before the teardown runs, and the second puts the previous state back after it. pytest runs a fixture's finalizers last-in, first-out on every version we support, so the pair brackets the fixture's teardown and anything another plugin registers for it. Only the real socket functions and pytest-socket's own guards are swapped, so patches frommonkeypatchormockare left for those tools to undo.pytest_runtest_teardownis unchanged.This covers every fixture scope,
request.addfinalizerinside fixtures, xunitteardown_moduleand unittesttearDownClass,--allow-hosts, a module whose last test is skipped, and teardown deferred by-x.#557 wrapped
pytest_runtest_teardowninstead. That runs every finalizer under the policy of the test being torn down. Class, module and session fixtures then tear down under whichever test happens to run last, so outcomes depend on test order: under pytest-randomly it errored on 8 of 10 seeds wheremainpassed. On pluggy < 1.6 an inner wrapper that raises also made it leak restrictions into the next test, or lift them partway through a later one. Keeping the teardown hook plain avoids all of that, and direct callers ofpytest_socket.pytest_runtest_teardown()keep working.enable_socket()now also lifts an allow-list (threat-model Q20). Before, it left theconnect()guard fromsocket_allow_hosts()in place.Behavior changes for the changelog:
--disable-socket, a marker or an allow-list is now restricted like setup.pytest_socket.enable_socket()at the start of the teardown.request.addfinalizerwhile the test runs still run unrestricted. That covers the test's ownrequestand a factory inside a fixture.Testing:
make mutmut: no surviving mutants. One equivalent mutant on the guard-marker lookup is skipped indo_not_mutate_patterns, the same waystrict=Falsealready is.main, and they pass on pytest 7.0.1 (pluggy 1.0) and on pytest 7.4.4, 8.0.2, 8.3.5 and 9.1.1 with pluggy 1.5, as well as pytest 9.1.1 with pluggy 1.6.main, Keep socket restrictions active through fixture teardown #557 and this branch. This branch fixes all of them. It also blocks teardown in the-x, skipped-last-test and pytest-rerunfailures cases, which neither of the other two did.Thanks to @MHammett for the detailed report, and to @Gabrieljr42 for #557, whose test cases shaped the coverage here.
I worked on this with Claude Code, which helped review #557, build this approach, and replay the review's reproductions against it.