Skip to content

Commit 6bd9bf5

Browse files
authored
ServerQuiescingHelper no longer leaking promises (#192)
* ServerQuiescingHelper no longer leaking promises Motivation: ServerQuiescingHelper leaked promises when promise left scope and not succeeded Modifications: Failing promise within a deinit * Minor fixes * generating linux tests * mini update
1 parent 98378d1 commit 6bd9bf5

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Sources/NIOExtras/QuiescingHelper.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ private final class CollectAcceptedChannelsHandler: ChannelInboundHandler {
216216
/// try fullyShutdownPromise.futureResult.wait()
217217
///
218218
public final class ServerQuiescingHelper {
219+
/// The `ServerQuiescingHelper` was never used to create a channel handler.
220+
public struct UnusedQuiescingHelperError: Error {}
219221
private let channelCollectorPromise: EventLoopPromise<ChannelCollector>
220222

221223
/// Initialize with a given `EventLoopGroup`.
@@ -226,6 +228,10 @@ public final class ServerQuiescingHelper {
226228
self.channelCollectorPromise = group.next().makePromise()
227229
}
228230

231+
deinit {
232+
self.channelCollectorPromise.fail(UnusedQuiescingHelperError())
233+
}
234+
229235
/// Create the `ChannelHandler` for the server `channel` to collect all accepted child `Channel`s.
230236
///
231237
/// - parameters:

Tests/NIOExtrasTests/QuiescingHelperTest+XCTest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftNIO open source project
44
//
5-
// Copyright (c) 2018-2022 Apple Inc. and the SwiftNIO project authors
5+
// Copyright (c) 2018-2023 Apple Inc. and the SwiftNIO project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -30,6 +30,7 @@ extension QuiescingHelperTest {
3030
("testShutdownIsImmediateWhenNoChannelsCollected", testShutdownIsImmediateWhenNoChannelsCollected),
3131
("testQuiesceUserEventReceivedOnShutdown", testQuiesceUserEventReceivedOnShutdown),
3232
("testQuiescingDoesNotSwallowCloseErrorsFromAcceptHandler", testQuiescingDoesNotSwallowCloseErrorsFromAcceptHandler),
33+
("testShutdownIsImmediateWhenPromiseDoesNotSucceed", testShutdownIsImmediateWhenPromiseDoesNotSucceed),
3334
]
3435
}
3536
}

Tests/NIOExtrasTests/QuiescingHelperTest.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,19 @@ public class QuiescingHelperTest: XCTestCase {
136136
XCTAssert(error is DummyError)
137137
}
138138
}
139+
140+
///verifying that the promise fails when goes out of scope for shutdown
141+
func testShutdownIsImmediateWhenPromiseDoesNotSucceed() throws {
142+
let el = EmbeddedEventLoop()
143+
144+
let p: EventLoopPromise<Void> = el.makePromise()
145+
146+
do {
147+
let quiesce = ServerQuiescingHelper(group: el)
148+
quiesce.initiateShutdown(promise: p)
149+
}
150+
XCTAssertThrowsError(try p.futureResult.wait()) { error in
151+
XCTAssertTrue(error is ServerQuiescingHelper.UnusedQuiescingHelperError)
152+
}
153+
}
139154
}

0 commit comments

Comments
 (0)