TaskStop should send SIGTERM before SIGKILL to allow graceful cleanup

Resolved 💬 3 comments Opened Mar 21, 2026 by shivaal-mako Closed Mar 25, 2026

When TaskStop terminates a background task, atexit handlers and signal handlers do not run, indicating the process receives SIGKILL without a prior SIGTERM.

Reproduction:

  1. Start a Python script as a background task that registers atexit handlers and SIGTERM/SIGINT signal handlers
  2. Use TaskStop to stop the task
  3. Observe: no signal handlers fire, no atexit callbacks run, cleanup files are left behind

Evidence:

  • Added signal handlers for SIGTERM, SIGINT, and SIGHUP — none logged any output after TaskStop
  • Added an atexit callback — it did not run after TaskStop
  • Manually sending kill -TERM to the same process group works correctly: signal handler fires, atexit runs, cleanup completes
  • Exit code from TaskStop is consistent with SIGKILL behavior

Expected behavior:
TaskStop should send SIGTERM first and wait a grace period (e.g. 5 seconds) before escalating to SIGKILL, similar to how Docker and Kubernetes handle container termination. This allows processes to clean up resources (temp files, database connections, child processes).

Workaround:
Currently the only workaround is to manually run kill -TERM -<pgid> via Bash before calling TaskStop, which defeats the purpose of TaskStop.

Related: #7841 (advanced process control for background commands)

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗