Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
weissi opened this issue Jun 19, 2023 · 1 comment
Assignees

Comments

@weissi
Copy link
Contributor

weissi commented Jun 19, 2023

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 weissi changed the title Foundation.Process on Linux seems to inherit the calling thread's signal mask, creating processes that even have SIGTERM blocked Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked Jun 19, 2023
bioball added a commit to apple/pkl-swift that referenced this issue Jan 23, 2024
Process.terminate() has a known issue on corelibs foundation (on linux): swiftlang/swift-corelibs-foundation#4772

Instead, we should SIGKILL the process on Linux as a workaround.

This will lead to reliably cleaning up pkl/jpkl processes.
@weissi
Copy link
Contributor Author

weissi commented Sep 18, 2024

still happens on 6.0 with swift-foundation.

@iCharlesHu iCharlesHu self-assigned this Sep 18, 2024
MaxDesiatov added a commit to swiftlang/swift-sdk-generator that referenced this issue Nov 27, 2024
Namely, this works around following issues in `Foundation.Process`:
 - "Foundation.Process on Linux throws error Error Domain=NSCocoaErrorDomain Code=256 "(null)" if executable not found"
   swiftlang/swift-corelibs-foundation#4810
 - "Foundation.Process on Linux doesn't correctly detect when child process dies (creating zombie processes)"
   swiftlang/swift-corelibs-foundation#4795
 - "Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, even SIGTERM blocked"
   swiftlang/swift-corelibs-foundation#4772
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants