@@ -183,7 +183,6 @@ public struct LifecycleShutdownHandler<State> {
183
183
/// By default, also install shutdown hooks based on `Signal` and backtraces.
184
184
public struct ServiceLifecycle {
185
185
private static let backtracesInstalled = AtomicBoolean ( false )
186
- private static let shutdownHooksInstalled = AtomicBoolean ( false )
187
186
188
187
private let configuration : Configuration
189
188
@@ -202,7 +201,6 @@ public struct ServiceLifecycle {
202
201
self . underlying = ComponentLifecycle ( label: self . configuration. label, logger: self . configuration. logger)
203
202
// setup backtraces as soon as possible, so if we crash during setup we get a backtrace
204
203
self . installBacktrace ( )
205
- self . installShutdownHooks ( )
206
204
}
207
205
208
206
/// Starts the provided `LifecycleTask` array.
@@ -214,6 +212,7 @@ public struct ServiceLifecycle {
214
212
guard self . underlying. idle else {
215
213
preconditionFailure ( " already started " )
216
214
}
215
+ self . setupShutdownHook ( )
217
216
self . underlying. start ( on: self . configuration. callbackQueue, callback)
218
217
}
219
218
@@ -223,6 +222,7 @@ public struct ServiceLifecycle {
223
222
guard self . underlying. idle else {
224
223
preconditionFailure ( " already started " )
225
224
}
225
+ self . setupShutdownHook ( )
226
226
try self . underlying. startAndWait ( on: self . configuration. callbackQueue)
227
227
}
228
228
@@ -247,16 +247,9 @@ public struct ServiceLifecycle {
247
247
}
248
248
}
249
249
250
- private func installShutdownHooks( ) {
251
- if self . configuration. shutdownSignal != nil , ServiceLifecycle . shutdownHooksInstalled. compareAndSwap ( expected: false , desired: true ) {
252
- self . register ( label: " Shutdown hooks " ,
253
- start: . sync( self . installShutdownSignalHooks) ,
254
- shutdown: . none)
255
- }
256
- }
257
-
258
- private func installShutdownSignalHooks( ) {
250
+ private func setupShutdownHook( ) {
259
251
self . configuration. shutdownSignal? . forEach { signal in
252
+ self . log ( " setting up shutdown hook on \( signal) " )
260
253
let signalSource = ServiceLifecycle . trap ( signal: signal, handler: { signal in
261
254
self . log ( " intercepted signal: \( signal) " )
262
255
self . shutdown ( )
0 commit comments