Fix hanging thread when shutting down an already closed EventLoopGroup (#1085)

* Fix hanging thread when shutting down an already closed EventLoopGroup

* Generate Linux tests

* Remove accidental shutdownLock.unlock

* Move dispatching of callbacks out of lock

* Use Lock.withLock instead of manual lock/unlock

* Remove superfluous lock.unlock
This commit is contained in:
Dario Rexin authored and Cory Benfield committed 2019-07-30 17:18:10 +01:00
1 parent 4fa0921e61
commit 0dc794498f
3 files changed
+80

No files matched your search

+45
View File
@@ -996,10 +996,18 @@ typealias ThreadInitializer = (NIOThread) -> Void
/// subclass, a good place to shut it down is the `tearDown` method.
public final class MultiThreadedEventLoopGroup: EventLoopGroup {
private enum RunState {
case running
case closing([(DispatchQueue, (Error?) -> Void)])
case closed(Error?)
}
private static let threadSpecificEventLoop = ThreadSpecificVariable<SelectableEventLoop>()
private let index = Atomic<Int>(value: 0)
private let eventLoops: [SelectableEventLoop]
private let shutdownLock: Lock = Lock()
private var runState: RunState = .running
private static func setupThreadAndEventLoop(name: String, initializer: @escaping ThreadInitializer) -> SelectableEventLoop {
let lock = Lock()
@@ -1104,6 +1112,27 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup {
// our shutdown signaling, and then do our cleanup once the DispatchQueue is empty.
let g = DispatchGroup()
let q = DispatchQueue(label: "nio.shutdownGracefullyQueue", target: queue)
let wasRunning: Bool = self.shutdownLock.withLock {
switch self.runState {
case .running:
self.runState = .closing([])
return true
case .closing(var callbacks):
callbacks.append((q, handler))
self.runState = .closing(callbacks)
return false
case .closed(let error):
q.async {
handler(error)
}
return false
}
}
guard wasRunning else {
return
}
var error: Error? = nil
for loop in self.eventLoops {
@@ -1125,6 +1154,22 @@ public final class MultiThreadedEventLoopGroup: EventLoopGroup {
}
handler(error)
let callbacks: [(DispatchQueue, (Error?) -> Void)] = self.shutdownLock.withLock {
guard case .closing(let callbacks) = self.runState else {
fatalError("runState was \(self.runState), expected .closing")
}
self.runState = .closed(error)
return callbacks
}
for (q, handler) in callbacks {
q.async {
handler(error)
}
}
}
}
}
@@ -55,6 +55,8 @@ extension EventLoopTest {
("testCancelledScheduledTasksDoNotHoldOnToRunClosure", testCancelledScheduledTasksDoNotHoldOnToRunClosure),
("testIllegalCloseOfEventLoopFails", testIllegalCloseOfEventLoopFails),
("testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks", testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks),
("testCallingSyncShutdownGracefullyMultipleTimesShouldNotHang", testCallingSyncShutdownGracefullyMultipleTimesShouldNotHang),
("testCallingShutdownGracefullyMultipleTimesShouldExecuteAllCallbacks", testCallingShutdownGracefullyMultipleTimesShouldExecuteAllCallbacks),
]
}
}
+33
View File
@@ -804,4 +804,37 @@ public final class EventLoopTest : XCTestCase {
XCTAssertLessThan(older - newer, .nanoseconds(0))
XCTAssertGreaterThan(newer - older, .nanoseconds(0))
}
func testCallingSyncShutdownGracefullyMultipleTimesShouldNotHang() throws {
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
try elg.syncShutdownGracefully()
try elg.syncShutdownGracefully()
try elg.syncShutdownGracefully()
}
func testCallingShutdownGracefullyMultipleTimesShouldExecuteAllCallbacks() throws {
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
let condition: ConditionLock<Int> = ConditionLock(value: 0)
elg.shutdownGracefully { _ in
if condition.lock(whenValue: 0, timeoutSeconds: 1) {
condition.unlock(withValue: 1)
}
}
elg.shutdownGracefully { _ in
if condition.lock(whenValue: 1, timeoutSeconds: 1) {
condition.unlock(withValue: 2)
}
}
elg.shutdownGracefully { _ in
if condition.lock(whenValue: 2, timeoutSeconds: 1) {
condition.unlock(withValue: 3)
}
}
guard condition.lock(whenValue: 3, timeoutSeconds: 1) else {
XCTFail("Not all shutdown callbacks have been executed")
return
}
condition.unlock()
}
}