Proper fixture resource cleanup by handling signals
Project description
pytest-sigil
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:
- Execute fixture teardown code
- Run cleanup callbacks
- 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,
SIGTERMwon'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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30cc81aca46b0f0abc0186985c4544ea336f61116e94a4d7488673e95b1ad0d4
|
|
| MD5 |
764ebb1d8ee50b27c5720a0a9963a9d7
|
|
| BLAKE2b-256 |
bbb2f7902603c187855ed8761c484a62b5c1f3e7f317626f32cfaa08390bf638
|
Provenance
The following attestation bundles were made for pytest_sigil-1.0.0.tar.gz:
Publisher:
publish_package.yml on hei-work/pytest-sigil
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_sigil-1.0.0.tar.gz -
Subject digest:
30cc81aca46b0f0abc0186985c4544ea336f61116e94a4d7488673e95b1ad0d4 - Sigstore transparency entry: 628508489
- Sigstore integration time:
-
Permalink:
hei-work/pytest-sigil@59cd7e507b4020d8537a0aa393902e51828c9b5b -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/hei-work
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_package.yml@59cd7e507b4020d8537a0aa393902e51828c9b5b -
Trigger Event:
workflow_dispatch
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36cbf67fed14d2cb7095b338d95c5b8086c76baf2c78af2f9007b4a8e6759aa1
|
|
| MD5 |
f2dd29dc1cde2dbf1e69ba277c86146e
|
|
| BLAKE2b-256 |
5deb8fa9e2fd6eb5e837fd31c0d949977cf60dfcb6eb5e5244173d51cf799f6d
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_sigil-1.0.0-py3-none-any.whl -
Subject digest:
36cbf67fed14d2cb7095b338d95c5b8086c76baf2c78af2f9007b4a8e6759aa1 - Sigstore transparency entry: 628508504
- Sigstore integration time:
-
Permalink:
hei-work/pytest-sigil@59cd7e507b4020d8537a0aa393902e51828c9b5b -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/hei-work
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_package.yml@59cd7e507b4020d8537a0aa393902e51828c9b5b -
Trigger Event:
workflow_dispatch
-
Statement type: