Skip to content

Commit ece5057

Browse files
authored
Fix strict concurrency checking diagnostics in MTELG (#2229)
1 parent bedb9fe commit ece5057

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Sources/NIOPosix/MultiThreadedEventLoopGroup.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@ typealias ThreadInitializer = (NIOThread) -> Void
5353
/// test. A good place to start a `MultiThreadedEventLoopGroup` is the `setUp` method of your `XCTestCase`
5454
/// subclass, a good place to shut it down is the `tearDown` method.
5555
public final class MultiThreadedEventLoopGroup: EventLoopGroup {
56-
56+
#if swift(>=5.7)
57+
private typealias ShutdownGracefullyCallback = @Sendable (Error?) -> Void
58+
#else
59+
private typealias ShutdownGracefullyCallback = (Error?) -> Void
60+
#endif
61+
5762
private enum RunState {
5863
case running
59-
case closing([(DispatchQueue, (Error?) -> Void)])
64+
case closing([(DispatchQueue, ShutdownGracefullyCallback)])
6065
case closed(Error?)
6166
}
6267

@@ -238,7 +243,7 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup {
238243
}
239244
#endif
240245

241-
private func _shutdownGracefully(queue: DispatchQueue, _ handler: @escaping (Error?) -> Void) {
246+
private func _shutdownGracefully(queue: DispatchQueue, _ handler: @escaping ShutdownGracefullyCallback) {
242247
// This method cannot perform its final cleanup using EventLoopFutures, because it requires that all
243248
// our event loops still be alive, and they may not be. Instead, we use Dispatch to manage
244249
// our shutdown signaling, and then do our cleanup once the DispatchQueue is empty.
@@ -293,28 +298,28 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup {
293298
for loop in self.eventLoops {
294299
loop.syncFinaliseClose(joinThread: true)
295300
}
296-
var overallError: Error?
297-
var queueCallbackPairs: [(DispatchQueue, (Error?) -> Void)]? = nil
298-
self.shutdownLock.withLock {
301+
let (overallError, queueCallbackPairs): (Error?, [(DispatchQueue, ShutdownGracefullyCallback)]) = self.shutdownLock.withLock {
299302
switch self.runState {
300303
case .closed, .running:
301304
preconditionFailure("MultiThreadedEventLoopGroup in illegal state when closing: \(self.runState)")
302305
case .closing(let callbacks):
303-
queueCallbackPairs = callbacks
304-
switch result {
305-
case .success:
306-
overallError = nil
307-
case .failure(let error):
308-
overallError = error
309-
}
306+
let overallError: Error? = {
307+
switch result {
308+
case .success:
309+
return nil
310+
case .failure(let error):
311+
return error
312+
}
313+
}()
310314
self.runState = .closed(overallError)
315+
return (overallError, callbacks)
311316
}
312317
}
313318

314319
queue.async {
315320
handler(overallError)
316321
}
317-
for queueCallbackPair in queueCallbackPairs! {
322+
for queueCallbackPair in queueCallbackPairs {
318323
queueCallbackPair.0.async {
319324
queueCallbackPair.1(overallError)
320325
}

0 commit comments

Comments
 (0)