Skip to content

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

Closed
@weissi

Description

@weissi
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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions