Search code examples
c++windowscmdg++sublimetext3

How to add timeout (on windows) for sublime build system for c++


I have this build system, and I want to modify it so that it will automatically kill the process if it takes more than 10 seconds, how can I do so?

{
    "cmd": ["g++.exe","-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe"],
    "shell":true,
    "working_dir":"$file_path",
    "selector":"source.cpp"
}

Solution

  • Since a sublime-build is just executing an external process, one way to do this would be to have the build execute an external tool (or script or batch file, etc) that keeps track of time and kills itself.

    A second way to do this from within Sublime would be to use a custom build target. This is a command provided via a plugin that you tell Sublime to use in place of the command that it would normally use to execute the build.

    An example of a plugin that does something like this is the below. See this video on how to use plugins if you're unfamiliar with using a custom plugin in Sublime:

    import sublime
    import sublime_plugin
    
    from Default.exec import ExecCommand
    
    
    class TimeoutExecCommand(ExecCommand):
        """
        Drop in replacement for the exec command in Sublime Text build 3210/3211.
        """
        def run(self, **kwargs):
            self.timeout = kwargs.pop("timeout", 0)
    
            super().run(**kwargs)
    
            if self.timeout:
                sublime.set_timeout(self.time_out_build, self.timeout * 1000)
    
        def time_out_build(self):
            if self.proc:
                self.append_string(self.proc, "[Timeout exceeded: %.1f]" % self.timeout)
                self.proc.kill()
                self.proc = None
    

    This implements a new timeout_exec command that can be used in builds to have them time out if they don't complete within a specific period of time. As noted in the comment, requires one of the more recent Sublime Text 3 builds (it will not work in the 4xxx builds since those builds have an enhanced exec command).

    To use this, you need to add some extra keys to your sublime-build file. An example of a build file that uses this is:

    {
        "target": "timeout_exec",
        "cancel": {"kill": true},
    
        "timeout": 4,
    
        "shell_cmd": "echo \"Start\" && sleep 10 && echo \"Done\""
    }
    

    The target key tells Sublime to use this new command instead of the default exec command and the cancel key tells Sublime what extra arguments to provide if you try to cancel the build manually (i.e. from the menu, command palette, etc).

    The command also implements a new build parameter of timeout that is the time in seconds after which the build will cancel itself. If the build is still running when the timeout is hit, it will cancel:

    Start
    [Timeout exceeded: 4.0]
    

    Leaving out the timeout field or setting it's value to 0 disables the timeout, in which case the command behaves exactly as a build normally would.