www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21575] New: Child processes spawned by


          Issue ID: 21575
           Summary: Child processes spawned by std.process.spawnProcess
                    accidentally inherit signal handlers in parent process
           Product: D
           Version: D2
          Hardware: x86
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: ttanjo gmail.com

The following code should work successfully but it does not.
I confirmed this issue on Linux systems.

Here is a link to the code: https://run.dlang.io/is/LgiMdL

import core.sys.posix.signal;
import std.concurrency;
import std.datetime : seconds;

import std.process;

void main()
    sigset_t ss;
    sigaddset(&ss, SIGINT);
    pthread_sigmask(SIG_BLOCK, &ss, null);

    auto pid = spawnProcess(["sleep", "infinity"]);
    scope(failure) {
        kill(pid, SIGKILL);

    // kill the spawned process with SIGINT
    // and send its return code
    spawn((shared Pid pid) {
        auto p = cast()pid;
        kill(p, SIGINT);
        auto code = wait(p);
        assert(code < 0);
        send(ownerTid, code);
    }, cast(shared)pid);

    auto received = receiveTimeout(3.seconds,
        (int) {
    assert(received); // Line 35

Expected behavior: It finishes successfully.
Actual behavior: It fails with the following message.
core.exception.AssertError onlineapp.d(35): Assertion failure
??:? _d_assertp [0x55d6e91f5611]
onlineapp.d:30 _Dmain [0x55d6e91df832]

The reason is that the spawned process inherits the signal handler of the
parent process.
That is, spawned `sleep infinity` is never stopped with `SIGINT`.
In the most cases, it is not intended behavior, I guess.

Jan 23 2021