swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.29k stars 1.13k forks source link

Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked #4772

Open weissi opened 1 year ago

weissi commented 1 year ago

I spawned two processes /bin/cat -nu one from Foundation.Process (pid 15477) and one from my shell (pid 15480)

root     15477  0.0  0.0   2992   788 pts/1    S    10:30   0:00 /bin/cat -nu
root     15480  0.0  0.0   2992   784 pts/1    S+   10:30   0:00 /bin/cat -nu

and the one from Foundation.Process is unkillable with SIGTERM:

# kill 15477
# kill -TERM 15477
# ps axuw | grep 1547[7]
root     15477  0.0  0.0   2992   788 pts/1    S    10:30   0:00 /bin/cat -nu

where as the other one will die immediately on SIGTERM. Why is that?

15477 (the one from Foundation.Process) has a lot of signals blocked (see SigBlk)

# cat /proc/15477/status | grep ^Sig
SigQ:   3/15256
SigPnd: 0000000000000000
SigBlk: fffffffe3bfbea27
SigIgn: 0000000180001000
SigCgt: 0000000000000000

fffffffe3bfbea27 is 1111111111111111111111111111111000111011111110111110101000100111 which means that all but 12 signals are actually blocked! Only 4: SIGILL, 5: SIGTRAP, 7: SIGBUS, 8: SIGFPE, 9: SIGKILL, 11: SIGSEGV, 13: SIGPIPE, 19: SIGSTOP, 27: SIGPROF and 31: SIGSYS seem not blocked.

That looks very curiously exactly like Dispatch's sigmask for its worker threads: https://github.com/apple/swift-corelibs-libdispatch/blob/9566a131de09a472790f7b69ae661326cdedf306/src/init.c#L91-L110 .

The normal /bin/cat -nu spawned from my shell has (as expected) a wholly 0 SigBlk mask (which means all signals are coming through).

# cat /proc/15480/status | grep ^Sig
SigQ:   3/15256
SigPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000000000000
weissi commented 1 month ago

still happens on 6.0 with swift-foundation.