Sublime Forum

What SIGNAL is sent on "Cancel Build"

#1

I wrote a build script that traps kill signals so I can run some clean up code, and it does not work when I ‘Cancel Build’ in sublime text. Here’s the important part of the build script.

#!/bin/bash
function clean_up {
    echo "all done"
}
trap clean_up SIGHUP SIGINT SIGTERM SIGKILL

but, when I cancel the build in sublime text, I don’t get the trap. All I see in the build window is:
[Cancelled]

Is there a way I can write a build script so that when it’s cancelled in Sublime Text I can ‘clean up’ ?

0 Likes

#2

we can see in Packages/Default/exec.py that ST uses the following Python code to cancel a build:

if sys.platform == "win32":
    # terminate would not kill process opened by the shell cmd.exe,
    # it will only kill cmd.exe leaving the child running
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    subprocess.Popen(
        "taskkill /PID " + str(self.proc.pid),
        startupinfo=startupinfo)
else:
    self.proc.terminate()

so it depends on how the terminate method of the Python object returned by subprocess.Popen is implemented.

also note that cancelling a build doesn’t kill the child processes the build may have spawned:

1 Like

#3

Fantastic. OK – based on this I did a bit more digging. My build script runs xcodebuild and if I cancel the build, the [Cancelled] shows up immediately, but you’re correct that the ‘clang’ process is still running. When that completes, my trap fires! I find that behavior odd, but at least my trap executes, which was the important thing.

0 Likes

#4

How to send a graceful termination signal?

I have to kill and ongoing task with process.kill(nodeprocess) as part of my gulp build system.

The ongoing task is defined like this in MyBuildSystem.sublime-build

{
  "name": "Serve",
  "shell_cmd": "gulp4 serve"
},

The problem is process.kill sends SIGTERM which is unsupported in Windows.
taskkill /im node.exe /t also doesn’t work, it requires /f and that makes the build end with exit code 1 which belches out an info dump with shell_cmd, dir and path :stuck_out_tongue_closed_eyes:

The rub lies in the fact that I have definitely seen it exit gracefully. I don’t know what I did to make that happen. Is there any way to send a graceful termination signal to a build task on Windows?

0 Likes