[Issue 21575] New: Child processes spawned by std.process.spawnProcess accidentally inherit signal handlers in parent process

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 23 14:05:44 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=21575

          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 at puremagic.com
          Reporter: ttanjo at 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

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

import std.process;

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

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

    // 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(true);
        },
    );
    assert(received); // Line 35
}
```

Expected behavior: It finishes successfully.
Actual behavior: It fails with the following message.
```
core.exception.AssertError at 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.

--


More information about the Digitalmars-d-bugs mailing list