Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix signal leak in taskworker child_worker #88742

Closed
markstory opened this issue Apr 3, 2025 · 0 comments · Fixed by #88799
Closed

Fix signal leak in taskworker child_worker #88742

markstory opened this issue Apr 3, 2025 · 0 comments · Fixed by #88799
Assignees

Comments

@markstory
Copy link
Member

diff --git a/tests/conftest.py b/tests/conftest.py
index 74ff8dbb8de..788cf0a6a9e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,6 +1,7 @@
 import os
+import signal
 import sys
-from collections.abc import MutableMapping
+from collections.abc import Generator, MutableMapping
 
 import psutil
 import pytest
@@ -146,3 +147,29 @@ def check_leaked_responses_mocks():
             f"`responses` were leaked outside of the test context:\n{leaked_s}"
             f"(make sure to use `@responses.activate` or `with responses.mock:`)"
         )
+
+
+def _leaked_signals() -> list[str]:
+    leaked = []
+    for signum in signal.Signals:
+        got = signal.getsignal(signum)
+        if (
+            got is not None
+            and not isinstance(got, int)
+            and got is not signal.default_int_handler
+            # prevent the debugger from interrupting teardown
+            and "Pdb.sigint_handler" not in str(got)
+        ):
+            leaked.append(f"- {signum}: {got}")
+    return leaked
+
+
+@pytest.fixture(autouse=True)
+def check_leaked_signals() -> Generator[None]:
+    before = frozenset(_leaked_signals())
+    yield
+    after = _leaked_signals()
+
+    leaked = [msg for msg in after if msg not in before]
+    if leaked:
+        raise AssertionError(f"test leaked os signal handlers:\n{'\n'.join(leaked)}")

when applied can result in tests failing

$ pytest tests/sentry/taskworker/test_worker.py::test_child_worker_failure_task
============================= test session starts ==============================
platform darwin -- Python 3.13.1, pytest-8.1.2, pluggy-1.5.0
django: version: 5.1.7
rootdir: /Users/asottile/workspace/sentry
configfile: pyproject.toml
plugins: fail-slow-0.3.0, repeat-0.9.3, time-machine-2.16.0, json-report-1.5.0, metadata-3.1.1, xdist-3.0.2, django-4.9.0, pytest_sentry-0.3.0, anyio-3.7.1, rerunfailures-15.0, cov-4.0.0
collected 1 item                                                               

tests/sentry/taskworker/test_worker.py .E                                [100%]

==================================== ERRORS ====================================
_____________ ERROR at teardown of test_child_worker_failure_task ______________
tests/conftest.py:175: in check_leaked_signals
    raise AssertionError(f"test leaked os signal handlers:\n{'\n'.join(leaked)}")
E   AssertionError: test leaked os signal handlers:
E   - 14: <function child_worker.<locals>.handle_alarm at 0x12d0c65c0>
=========================== short test summary info ============================
ERROR tests/sentry/taskworker/test_worker.py::test_child_worker_failure_task - AssertionError: test leaked os signal handlers:
========================== 1 passed, 1 error in 4.56s ==========================
@markstory markstory self-assigned this Apr 4, 2025
markstory added a commit that referenced this issue Apr 4, 2025
There are some tests for `child_worker` that are not in child processes,
and we're currently leaking signal handlers. Use a context manager to
add/cancel the signal handler.

Fixes #88742
markstory added a commit that referenced this issue Apr 4, 2025
There are some tests for `child_worker` that are not in child processes,
and we're currently leaking signal handlers. Use a context manager to
add/cancel the signal handler.

Fixes #88742
andrewshie-sentry pushed a commit that referenced this issue Apr 8, 2025
There are some tests for `child_worker` that are not in child processes,
and we're currently leaking signal handlers. Use a context manager to
add/cancel the signal handler.

Fixes #88742
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant