Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Sources/Lifecycle/Lifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ public struct ServiceLifecycle {
}

extension ServiceLifecycle {
private static var trapped: Set<Int32> = []
private static let trappedLock = Lock()

/// Setup a signal trap.
///
/// - parameters:
Expand All @@ -342,14 +345,20 @@ extension ServiceLifecycle {
/// - cancelAfterTrap: Defaults to false, which means the signal handler can be run multiple times. If true, the DispatchSignalSource will be cancelled after being trapped once.
/// - returns: a `DispatchSourceSignal` for the given trap. The source must be cancelled by the caller.
public static func trap(signal sig: Signal, handler: @escaping (Signal) -> Void, on queue: DispatchQueue = .global(), cancelAfterTrap: Bool = false) -> DispatchSourceSignal {
// on linux, we can call singal() once per process
self.trappedLock.withLockVoid {
if !trapped.contains(sig.rawValue) {
signal(sig.rawValue, SIG_IGN)
trapped.insert(sig.rawValue)
}
}
let signalSource = DispatchSource.makeSignalSource(signal: sig.rawValue, queue: queue)
signal(sig.rawValue, SIG_IGN)
signalSource.setEventHandler(handler: {
signalSource.setEventHandler {
if cancelAfterTrap {
signalSource.cancel()
}
handler(sig)
})
}
signalSource.resume()
return signalSource
}
Expand Down
1 change: 1 addition & 0 deletions Tests/LifecycleTests/ServiceLifecycleTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extension ServiceLifecycleTests {
("testNesting2", testNesting2),
("testSignalDescription", testSignalDescription),
("testBacktracesInstalledOnce", testBacktracesInstalledOnce),
("testRepeatShutdown", testRepeatShutdown),
]
}
}
38 changes: 38 additions & 0 deletions Tests/LifecycleTests/ServiceLifecycleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,42 @@ final class ServiceLifecycleTests: XCTestCase {
_ = ServiceLifecycle(configuration: config)
_ = ServiceLifecycle(configuration: config)
}

func testRepeatShutdown() {
if ProcessInfo.processInfo.environment["SKIP_SIGNAL_TEST"].flatMap(Bool.init) ?? false {
print("skipping testRepeatShutdown")
return
}

var count = 0

struct Service {
static let signal = ServiceLifecycle.Signal.ALRM

let lifecycle: ServiceLifecycle

init() {
self.lifecycle = ServiceLifecycle(configuration: .init(shutdownSignal: [Service.signal]))
self.lifecycle.register(GoodItem())
}
}

func gracefulShutdown() {
let service = Service()
service.lifecycle.start { error in
XCTAssertNil(error, "not expecting error")
kill(getpid(), Service.signal.rawValue)
}

service.lifecycle.wait()
count = count + 1 // not thread safe but fine for this purpose
}

let attempts = Int.random(in: 2 ..< 5)
for _ in 0 ..< attempts {
gracefulShutdown()
}

XCTAssertEqual(attempts, count)
}
}