TaskStop should send SIGTERM before SIGKILL to allow graceful cleanup
When TaskStop terminates a background task, atexit handlers and signal handlers do not run, indicating the process receives SIGKILL without a prior SIGTERM.
Reproduction:
- Start a Python script as a background task that registers atexit handlers and SIGTERM/SIGINT signal handlers
- Use
TaskStopto stop the task - 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 -TERMto 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)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗