Skip to main content

Proper fixture resource cleanup by handling signals

Project description

pytest-sigil

License: MIT Python: ^3.10

Proper fixture resource cleanup by handling signals

A pytest plugin that ensures your test fixtures clean up properly when receiving termination signals (SIGTERM), preventing resource leaks and ensuring graceful shutdowns.

Problem

When pytest receives a SIGTERM signal (e.g., from cancelled CI/CD jobs), it terminates immediately without running fixture teardown code. This can leave resources dangling:

  • Database connections remain open
  • Temporary files aren't cleaned up
  • Docker containers keep running
  • File locks persist

Related: See pytest issues #5243 & #9142 for discussion on this behavior.

Solution

pytest-sigil intercepts SIGTERM signals and reroutes them to SIGINT handlers, allowing pytest to:

  1. Execute fixture teardown code
  2. Run cleanup callbacks
  3. Exit gracefully with proper status codes

Installation

pip install pytest-sigil

Usage

The plugin activates automatically once installed—no configuration needed.

import pytest


@pytest.fixture
def database_connection():
    conn = create_connection()
    yield conn
    conn.close()  # ✓ Now runs even on SIGTERM

Disable the Plugin

If needed, disable it for specific test runs:

pytest -p no:sigil

CI/CD Configuration

⚠️ Important: Requires proper signal forwarding in containerized environments. Without an init process, SIGTERM won't reach pytest.

GitLab CI

variables:
  FF_USE_INIT_WITH_DOCKER_EXECUTOR: "true"

This flag ensures GitLab's Docker executor uses an init process (tini) that properly forwards SIGTERM to pytest. See GitLab's feature flag documentation for details.

Note: Without proper signal forwarding, the plugin cannot intercept SIGTERM. Check your CI provider's documentation for signal handling behavior.

How It Works

# On session start: SIGTERM → SIGINT
signal.signal(signal.SIGTERM, signal.getsignal(signal.SIGINT))

# On session finish: Restore original SIGTERM handler
signal.signal(signal.SIGTERM, original_handler)

This simple rerouting leverages pytest's existing interrupt handling, ensuring all cleanup hooks execute properly.

License

MIT © lovetheguitar

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_sigil-1.0.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_sigil-1.0.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_sigil-1.0.0.tar.gz.

File metadata

  • Download URL: pytest_sigil-1.0.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_sigil-1.0.0.tar.gz
Algorithm Hash digest
SHA256 30cc81aca46b0f0abc0186985c4544ea336f61116e94a4d7488673e95b1ad0d4
MD5 764ebb1d8ee50b27c5720a0a9963a9d7
BLAKE2b-256 bbb2f7902603c187855ed8761c484a62b5c1f3e7f317626f32cfaa08390bf638

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_sigil-1.0.0.tar.gz:

Publisher: publish_package.yml on hei-work/pytest-sigil

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_sigil-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_sigil-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_sigil-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36cbf67fed14d2cb7095b338d95c5b8086c76baf2c78af2f9007b4a8e6759aa1
MD5 f2dd29dc1cde2dbf1e69ba277c86146e
BLAKE2b-256 5deb8fa9e2fd6eb5e837fd31c0d949977cf60dfcb6eb5e5244173d51cf799f6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_sigil-1.0.0-py3-none-any.whl:

Publisher: publish_package.yml on hei-work/pytest-sigil

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page