Thread overview
Spawning a process, then killing it on SIGINT
Nov 23, 2019
aliak
Nov 23, 2019
mipri
Nov 24, 2019
aliak
Nov 24, 2019
aliak
November 23, 2019
Is there a way to go about killing a process after spawning it on a SIGINT?

I can't do this for e.g. because kill is not @nogc.

Pid currentSpawnedPid;
extern(C) void killCurrentPidHandler(int sig) nothrow @nogc @system {
  kill(currentSpawnedPid, sig);
}

int main() {
  currentSpawnedPid = spawnProcess(["docker-compose", "up"], stdin, stdout, stderr);
  signal(SIGINT, &killCurrentPidHandler);
  return wait(currentSpawnedPid);
}

Any other ways to go about this?

Cheers,
- Ali
November 23, 2019
On Saturday, 23 November 2019 at 09:54:48 UTC, aliak wrote:
> Is there a way to go about killing a process after spawning it on a SIGINT?
>
> I can't do this for e.g. because kill is not @nogc.

Well, this works:

import std;
import core.stdc.signal;

extern(C) int kill(int pid, int sig) nothrow @nogc @system;

int currentSpawnedPid;
extern(C) void killCurrentPidHandler(int sig) nothrow @nogc @system {
  if (currentSpawnedPid > 1)
    kill(currentSpawnedPid, sig);
}

int main() {
  auto pid = spawnProcess(["sleep", "50s"], stdin, stdout, stderr);
  currentSpawnedPid = pid.processID;
  signal(SIGINT, &killCurrentPidHandler);
  return wait(pid);
}

November 23, 2019
On 11/23/19 4:54 AM, aliak wrote:
> Is there a way to go about killing a process after spawning it on a SIGINT?
> 
> I can't do this for e.g. because kill is not @nogc.
> 
> Pid currentSpawnedPid;
> extern(C) void killCurrentPidHandler(int sig) nothrow @nogc @system {
>    kill(currentSpawnedPid, sig);
> }
> 
> int main() {
>    currentSpawnedPid = spawnProcess(["docker-compose", "up"], stdin, stdout, stderr);
>    signal(SIGINT, &killCurrentPidHandler);
>    return wait(currentSpawnedPid);
> }
> 
> Any other ways to go about this?

Yeah, fix phobos. kill should be @nogc/nothrow, and probably @safe.

-Steve
November 24, 2019
On Saturday, 23 November 2019 at 10:09:51 UTC, mipri wrote:
> On Saturday, 23 November 2019 at 09:54:48 UTC, aliak wrote:
>> Is there a way to go about killing a process after spawning it on a SIGINT?
>>
>> I can't do this for e.g. because kill is not @nogc.
>
> Well, this works:
>
> import std;
> import core.stdc.signal;
>
> extern(C) int kill(int pid, int sig) nothrow @nogc @system;
>
> int currentSpawnedPid;
> extern(C) void killCurrentPidHandler(int sig) nothrow @nogc @system {
>   if (currentSpawnedPid > 1)
>     kill(currentSpawnedPid, sig);
> }
>
> int main() {
>   auto pid = spawnProcess(["sleep", "50s"], stdin, stdout, stderr);
>   currentSpawnedPid = pid.processID;
>   signal(SIGINT, &killCurrentPidHandler);
>   return wait(pid);
> }

Thanks, looks like I'll have to go that route.
November 24, 2019
On Saturday, 23 November 2019 at 12:19:27 UTC, Steven Schveighoffer wrote:
> On 11/23/19 4:54 AM, aliak wrote:
>> Is there a way to go about killing a process after spawning it on a SIGINT?
>> 
>> I can't do this for e.g. because kill is not @nogc.
>> 
>> Pid currentSpawnedPid;
>> extern(C) void killCurrentPidHandler(int sig) nothrow @nogc @system {
>>    kill(currentSpawnedPid, sig);
>> }
>> 
>> int main() {
>>    currentSpawnedPid = spawnProcess(["docker-compose", "up"], stdin, stdout, stderr);
>>    signal(SIGINT, &killCurrentPidHandler);
>>    return wait(currentSpawnedPid);
>> }
>> 
>> Any other ways to go about this?
>
> Yeah, fix phobos. kill should be @nogc/nothrow, and probably @safe.
>
> -Steve

Looked in to it, seems step one is getting phobos compiling with dip1008 :/
Kill uses enforce.
November 24, 2019
On 11/24/19 10:36 AM, aliak wrote:
> On Saturday, 23 November 2019 at 12:19:27 UTC, Steven Schveighoffer wrote:
>> On 11/23/19 4:54 AM, aliak wrote:
>>> Is there a way to go about killing a process after spawning it on a SIGINT?
>>>
>>> I can't do this for e.g. because kill is not @nogc.
>>>
>>> Pid currentSpawnedPid;
>>> extern(C) void killCurrentPidHandler(int sig) nothrow @nogc @system {
>>>    kill(currentSpawnedPid, sig);
>>> }
>>>
>>> int main() {
>>>    currentSpawnedPid = spawnProcess(["docker-compose", "up"], stdin, stdout, stderr);
>>>    signal(SIGINT, &killCurrentPidHandler);
>>>    return wait(currentSpawnedPid);
>>> }
>>>
>>> Any other ways to go about this?
>>
>> Yeah, fix phobos. kill should be @nogc/nothrow, and probably @safe.
>>
> 
> Looked in to it, seems step one is getting phobos compiling with dip1008 :/
> Kill uses enforce.

Oof, yeah. I saw the short kill function and thought it was simply calling the kill syscall, but it's calling something more complex. Error handling in general in Phobos is somewhat of a mixed bag. Would be great to have a clearly defined error handling scheme, or even one that's configurable.

Or as you said, make dip1008 mandatory ;)

-Steve