Skip to content

fix: tear fixtures down under their setup-time socket state - #564

Merged
miketheman merged 3 commits into
mainfrom
fix/teardown-mirrors-setup
Oct 21, 2026
Merged

miketheman merged 3 commits into
mainfrom
fix/teardown-mirrors-setup

Conversation

@miketheman

Copy link
Copy Markdown
Owner

Fixes #537. Supersedes #557.

Fixture teardown ran with the network open, because pytest_runtest_teardown lifts the restrictions before pytest runs finalizers. Each fixture now tears down under the socket state its own setup ran under.

A pytest_fixture_setup hookwrapper 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 from monkeypatch or mock are left for those tools to undo. pytest_runtest_teardown is unchanged.

This covers every fixture scope, request.addfinalizer inside fixtures, xunit teardown_module and unittest tearDownClass, --allow-hosts, a module whose last test is skipped, and teardown deferred by -x.

#557 wrapped pytest_runtest_teardown instead. 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 where main passed. 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 of pytest_socket.pytest_runtest_teardown() keep working.

enable_socket() now also lifts an allow-list (threat-model Q20). Before, it left the connect() guard from socket_allow_hosts() in place.

Behavior changes for the changelog:

  • Fixture teardown under --disable-socket, a marker or an allow-list is now restricted like setup.
  • A fixture first set up by a blocked test tears down blocked, even if a later test that uses it allows the network. The README explains this and the workaround: call pytest_socket.enable_socket() at the start of the teardown.
  • Callbacks registered with request.addfinalizer while the test runs still run unrestricted. That covers the test's own request and a factory inside a fixture.

Testing:

  • 136 tests pass with 100% plugin coverage; mypy is clean.
  • make mutmut: no surviving mutants. One equivalent mutant on the guard-marker lookup is skipped in do_not_mutate_patterns, the same way strict=False already is.
  • The new tests fail on 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.
  • I replayed the reproductions from the Keep socket restrictions active through fixture teardown #557 review against 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.

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>
@codspeed

codspeed Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 9.71%

1 warning

⚠️ Base and head ran on different runner types

Some benchmarks with significant performance changes were compared across different runner types, so the changes might not come from the code. Run base and head on the same runner type, or use CodSpeed Macro Runners for more stable results.

Learn more about Macro Runners

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
❌ 1 (👁 1) regressed benchmark
✅ 9 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_bench_host_from_connect_args_unix 25.9 µs 17.8 µs +45.98%
⚡ test_bench_host_from_address 19.9 µs 15.9 µs +25.11%
👁 test_bench_host_from_connect_args 20.2 µs 27.9 µs -27.69%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing fix/teardown-mirrors-setup (f4f8999) with main (1d05745)

Open in CodSpeed

@miketheman
miketheman merged commit 49db7da into main Oct 21, 2026
32 checks passed
@miketheman
miketheman deleted the fix/teardown-mirrors-setup branch October 21, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fixture teardown runs after the restrictions are lifted

1 participant