Ensure job cancellation is propagated to executors
The ctx passed to this function is the one that is cancelled when a Ci job is cancelled via the web UI OR if the job times out. If the context is not propagated to the executors, they have no way of knowing the job was terminated at the Rails end, so can't themselves initiate a graceful shutdown of the job process.
This change alone is enough to make job processes running on Linux via the Shell executor to receive SIGTERM so they can attempt to shutdown gracefully. Runner will currently wait for 10 seconds (currently hard coded) after sending SIGTERM before sending SIGKILL.
To enable graceful shutdown of jobs on other executors/OSs will require
further work, but this piece is required. And for some combinations it
may not be possible. Specifically, on Windows there's no tool installed
by default that can send SIGTERM (a.k.a. CTRL_C) to console processes.
We use taskkill
to stop job processes, but for console processes this
only works with the /F
option, which is effectively SIGKILL
. There
is a C++ API to do this (GenerateConsoleCtrlEvent
), but this is not
exposed through any command-line tool or pwsh builtin.
Note: I'm looking into tests...