Skip to content

fix(taskworker) Improve shutdown of taskworkers #94885

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

Merged
merged 7 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions src/sentry/taskworker/worker.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from __future__ import annotations

import atexit
import logging
import multiprocessing
import queue
import signal
import threading
import time
from concurrent.futures import ThreadPoolExecutor
Expand Down Expand Up @@ -81,9 +81,6 @@ def __init__(

self._processing_pool_name: str = processing_pool_name or "unknown"

def __del__(self) -> None:
self.shutdown()

def do_imports(self) -> None:
for module in settings.TASKWORKER_IMPORTS:
__import__(module)
Expand All @@ -99,10 +96,20 @@ def start(self) -> int:
self.start_result_thread()
self.start_spawn_children_thread()

atexit.register(self.shutdown)
# Convert signals into KeyboardInterrupt.
# Running shutdown() within the signal handler can lead to deadlocks
def signal_handler(*args: Any) -> None:
raise KeyboardInterrupt()

while True:
self.run_once()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

try:
while True:
self.run_once()
except KeyboardInterrupt:
self.shutdown()
raise

def run_once(self) -> None:
"""Access point for tests to run a single worker loop"""
Expand All @@ -113,30 +120,33 @@ def shutdown(self) -> None:
Shutdown cleanly
Activate the shutdown event and drain results before terminating children.
"""
if self._shutdown_event.is_set():
return

logger.info("taskworker.worker.shutdown")
logger.info("taskworker.worker.shutdown.start")
self._shutdown_event.set()

logger.info("taskworker.worker.shutdown.spawn_children")
if self._spawn_children_thread:
self._spawn_children_thread.join()

logger.info("taskworker.worker.shutdown.children")
for child in self._children:
child.terminate()
for child in self._children:
child.join()

logger.info("taskworker.worker.shutdown.result")
if self._result_thread:
self._result_thread.join()
# Use a timeout as sometimes this thread can deadlock on the Event.
self._result_thread.join(timeout=5)

# Drain remaining results synchronously, as the thread will have terminated
# when shutdown_event was set.
# Drain any remaining results synchronously
while True:
try:
result = self._processed_tasks.get_nowait()
self._send_result(result, fetch=False)
except queue.Empty:
break

if self._spawn_children_thread:
self._spawn_children_thread.join()
logger.info("taskworker.worker.shutdown.complete")

def _add_task(self) -> bool:
"""
Expand Down Expand Up @@ -179,7 +189,7 @@ def start_result_thread(self) -> None:
"""

def result_thread() -> None:
logger.debug("taskworker.worker.result_thread_started")
logger.debug("taskworker.worker.result_thread.started")
iopool = ThreadPoolExecutor(max_workers=self._concurrency)
with iopool as executor:
while not self._shutdown_event.is_set():
Expand All @@ -193,7 +203,9 @@ def result_thread() -> None:
)
continue

self._result_thread = threading.Thread(target=result_thread)
self._result_thread = threading.Thread(
name="send-result", target=result_thread, daemon=True
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The threads have daemon=true now as we don't need/want to block the worker shutdown on them.

)
self._result_thread.start()

def _send_result(self, result: ProcessingResult, fetch: bool = True) -> bool:
Expand Down Expand Up @@ -253,6 +265,7 @@ def _send_update_task(
)
# Use the shutdown_event as a sleep mechanism
self._shutdown_event.wait(self._setstatus_backoff_seconds)

try:
next_task = self.client.update_task(result, fetch_next)
self._setstatus_backoff_seconds = 0
Expand All @@ -276,14 +289,15 @@ def _send_update_task(

def start_spawn_children_thread(self) -> None:
def spawn_children_thread() -> None:
logger.debug("taskworker.worker.spawn_children_thread_started")
logger.debug("taskworker.worker.spawn_children_thread.started")
while not self._shutdown_event.is_set():
self._children = [child for child in self._children if child.is_alive()]
if len(self._children) >= self._concurrency:
time.sleep(0.1)
continue
for i in range(self._concurrency - len(self._children)):
process = self.mp_context.Process(
name=f"taskworker-child-{i}",
target=child_process,
args=(
self._child_tasks,
Expand All @@ -301,7 +315,9 @@ def spawn_children_thread() -> None:
extra={"pid": process.pid, "processing_pool": self._processing_pool_name},
)

self._spawn_children_thread = threading.Thread(target=spawn_children_thread)
self._spawn_children_thread = threading.Thread(
name="spawn-children", target=spawn_children_thread, daemon=True
)
self._spawn_children_thread.start()

def fetch_task(self) -> InflightTaskActivation | None:
Expand Down
6 changes: 1 addition & 5 deletions src/sentry/taskworker/workerchild.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def handle_alarm(signum: int, frame: FrameType | None) -> None:
f"execution deadline of {deadline} seconds exceeded by {taskname}"
)

while True:
while not shutdown_event.is_set():
if max_task_count and processed_task_count >= max_task_count:
metrics.incr(
"taskworker.worker.max_task_count_reached",
Expand All @@ -171,10 +171,6 @@ def handle_alarm(signum: int, frame: FrameType | None) -> None:
)
break

if shutdown_event.is_set():
logger.info("taskworker.worker.shutdown_event")
break

child_tasks_get_start = time.monotonic()
try:
# If the queue is empty, this could block for a second.
Expand Down
Loading