﻿# A pytest session fixture can create security test resources in every worker

A practical investigation of a pytest session fixture can create security test resources in every worker, with explicit observations, control cases and evidence limits.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/field-security-xdist-session-fixture-security-resource-ownership/
Language: en

## The operational scenario

A security regression suite creates a temporary user and protected endpoint through a session-scoped fixture. After enabling parallel workers, some tests lose credentials during execution or leave additional resources behind. The fixture author assumed a single setup and teardown for the whole invocation. Independent worker sessions can instead create competing owners for the same resource name, making a passing denial test conceal a harness failure.

## The trust-boundary mechanism

pytest-xdist documents that each worker performs its own collection and tests in different workers can instantiate high-scope fixtures separately. There is no built-in guarantee that a session fixture runs globally once. Ordinary pytest teardown also depends on setup progress: code after yield is not run for that fixture when it fails before yielding. Parallel ownership and partial-setup cleanup therefore need explicit design rather than an assumed scope label.

## Define the control contract

Decide whether each worker should receive a distinct disposable identity and endpoint or share one deliberately coordinated resource. Namespace resources using run and worker identity when isolation is appropriate. If sharing is required, assign creation, use and final cleanup separately; a creation lock alone does not establish the correct time to revoke access. Never let a security fixture select a production resource merely because its preferred temporary name already exists.

## A step-by-step verification

First use a local fake administrative service that records creation and deletion without live credentials. Compare sequential and parallel collection of harmless tests. Record which worker creates, uses and removes each resource. Next force a setup failure after the first controlled state change and observe cleanup ownership. Finally repeat with the intended isolated or coordinated design, verifying denied and permitted cases while keeping the harness state visible.

## Evidence required for acceptance

Acceptance requires a resource ledger linked to test-run and worker identity, setup outcomes and final cleanup evidence. A denial caused by another worker deleting the account must not count as correct authorization enforcement. Preserve connection failures and fixture errors separately from expected policy denials. Verify no unrelated resources were touched and mark unresolved leftovers explicitly, even when every test body that actually ran reported a passing assertion.

## Failure cases and limits

This procedure does not promise cleanup after every machine failure or forcibly killed process. External expiry or reconciliation can be separate safeguards with their own evidence. The article investigates parallel fixture ownership without creating live accounts or exposing a public test endpoint. It makes the security regression result depend on the intended control, rather than an accidental race in the test infrastructure.

## Primary references and related reading

Parallel test resources need explicit worker ownership or coordination; session scope alone does not mean global once. [pytest-xdist per-worker session fixtures](https://pytest-xdist.readthedocs.io/en/stable/how-to.html); [pytest fixture lifecycle and safe teardown](https://docs.pytest.org/en/stable/how-to/fixtures.html).

The proposed investigation of xdist session fixture security resource ownership is a proposed control review; no live security test was executed. Related reading: [Local fault injection: interrupting an upstream at a known stream boundary](/journal/provider-midstream-fault-injection-tests/); [Golden inference API fixtures: test the contract rather than prose](/journal/provider-golden-contract-fixtures/).
