Bug 21575 – Child processes spawned by std.process.spawnProcess accidentally inherit signal masks in parent process
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2021-01-23T14:05:44Z
Last change time
2021-04-19T15:58:22Z
Keywords
pull
Assigned to
No Owner
Creator
Tomoya Tanjo
Comments
Comment #0 by ttanjo — 2021-01-23T14:05:44Z
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.
```
[email protected](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.
Comment #1 by ttanjo — 2021-01-24T05:42:42Z
Sorry, I misunderstood the problem.
As shown in the following link, `execve` that is internally called by spawnProcess does not inherit signal handlers but inherits signal masks of the parent process.
Inheriting the signal masks is the source of this issue.
https://man7.org/linux/man-pages/man7/signal.7.html
I fixed the title of this issue.
Comment #2 by dlang-bot — 2021-01-24T05:57:48Z
@tom-tan created dlang/phobos pull request #7766 "Fix issue 21575 - Child processes spawned by std.process.spawnProcess accidentally inherit signal masks in parent process" fixing this issue:
- Fix issue 21575 - Child processes spawned by std.process.spawnProcess accidentally inherit signal handlers in parent process
- Fix issue 21575 - Child processes spawned by std.process.spawnProcess accidentally inherit signal masks in parent process
https://github.com/dlang/phobos/pull/7766
Comment #3 by dlang-bot — 2021-04-19T15:58:22Z
dlang/phobos pull request #7766 "Fix issue 21575 - Child processes spawned by std.process.spawnProcess accidentally inherit signal masks in parent process" was merged into master:
- 371e3c8606c54da2c343f570a7571176d178166c by Tomoya Tanjo:
Fix issue 21575 - Child processes spawned by std.process.spawnProcess accidentally inherit signal masks in parent process
https://github.com/dlang/phobos/pull/7766