Skip to content
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

[SR-4201] DispatchSourceSignal doesn't work properly on Linux #695

Closed
weissi opened this issue Mar 9, 2017 · 5 comments
Closed

[SR-4201] DispatchSourceSignal doesn't work properly on Linux #695

weissi opened this issue Mar 9, 2017 · 5 comments

Comments

@weissi
Copy link
Member

weissi commented Mar 9, 2017

Previous ID SR-4201
Radar None
Original Reporter @weissi
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s libdispatch
Labels Bug
Assignee dgrove-oss (JIRA)
Priority Medium

md5: cb7e513f2c891ab499ed2e68f4d8a5f4

Issue Description:

Description

Signals don't get delivered on `DispatchSourceSignal` on Linux. As a repro, run the following program

#if os(Linux)
import Glibc
#else
import Darwin
#endif
import Dispatch

let sig = SIGCHLD

signal(sig, SIG_IGN);
let q = DispatchQueue(label: "foo")
let s = DispatchSource.makeSignalSource(signal: sig, queue: q)
s.setEventHandler {
    print("SIGNAL")
    fputs("SIGNAL!\n", stderr)
    kill(getpid(), SIGKILL)
}
s.setRegistrationHandler {
    print("registered in pid \(getpid())")
    print("kill -\(sig) \(getpid())")
}
s.resume()
dispatchMain()

and then execute the line that it writes ("kill -...")

on macOS

$ ./test & 
[1] 49783
registered in pid 49783
kill -20 49783
$ kill -20 49783
SIGNAL
SIGNAL!
[1]+  Killed: 9               ./test
$ 

as expected, the program prints "SIGNAL", then "SIGNAL!" and then kills itself.

On Linux

$ ./test &
[1] 28406
registered in pid 28406
kill -17 28406
$ kill -17 28406
$ kill -17 28406
$ kill -17 28406
$ kill -17 28406

and nothing happens, the program is still alive. Interestingly, in LLDB on Linux is sometimes does receive the signal

On Linux in LLDB

$ lldb ./test
(lldb) target create "./test"
Current executable set to './test' (x86_64).
(lldb) run
Process 28461 launched: './test' (x86_64)
registered in pid 28461
kill -17 28461
Process 28461 stopped and restarted: thread 1 received signal: SIGCHLD
SIGNAL
SIGNAL!
Process 28461 exited with status = 9 (0x00000009) 
(lldb)  

(the kill was run from some other terminal here).

Swift versions:

macOS

$ swiftc --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9

Linux

swiftc --version
Swift version 3.0.2 (swift-3.0.2-RELEASE)
Target: x86_64-unknown-linux-gnu

but this also happens with this Swift version

swiftc --version
Swift version 3.1-dev (LLVM 5c165fb715, Clang e540ba0c30, Swift 3d3fdecbb4)
Target: x86_64-unknown-linux-gnu

which is master from 1st Feb 2017

@swift-ci
Copy link

swift-ci commented Mar 9, 2017

Comment by David Grove (JIRA)

I can reproduce the bug.

@swift-ci
Copy link

swift-ci commented Mar 9, 2017

Comment by David Grove (JIRA)

The problem appears to be in dispatch_sigsuspend (invoked as a result of calling dispatch_main to hang up the main thread and avoid it becoming a zombie).

It calls sigsuspend with an empty sigset. As a result, the signal gets masked out and not delivered to the signalfd. If I include SIGCHLD in the sigset being passed to sigsuspend, then the SignalSource fires correctly.

Will work on cleaning up a possible fix tomorrow.

@swift-ci
Copy link

Comment by David Grove (JIRA)

Submitted a possible fix as #231

@swift-ci
Copy link

Comment by David Grove (JIRA)

sigtest.c is a small C program that shows the same problematic behavior without the full complexity of dispatch.

@swift-ci
Copy link

swift-ci commented May 7, 2017

Comment by David Grove (JIRA)

PR merged.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants