Skip to content

Commit b78674f

Browse files
committed
Only crash in debug mode, if HTTPClient was not shutdown
1 parent ce01ff2 commit b78674f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Diff for: Sources/AsyncHTTPClient/HTTPClient.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class HTTPClient {
6868
let eventLoopGroupProvider: EventLoopGroupProvider
6969
let configuration: Configuration
7070
let poolManager: HTTPConnectionPool.Manager
71-
var state: State
71+
private var state: State
7272
private let stateLock = Lock()
7373

7474
internal static let loggingDisabled = Logger(label: "AHC-do-not-log", factory: { _ in SwiftLogNoOpLogHandler() })
@@ -118,8 +118,14 @@ public class HTTPClient {
118118
}
119119

120120
deinit {
121-
guard case .shutDown = self.state else {
122-
preconditionFailure("Client not shut down before the deinit. Please call client.syncShutdown() when no longer needed.")
121+
debugOnly {
122+
// We want to crash only in debug mode.
123+
switch self.state {
124+
case .shutDown:
125+
break
126+
case .shuttingDown, .upAndRunning:
127+
preconditionFailure("Client not shut down before the deinit. Please call client.syncShutdown() when no longer needed.")
128+
}
123129
}
124130
}
125131

Diff for: Sources/AsyncHTTPClient/Utils.swift

+10
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,13 @@ public final class HTTPClientCopyingDelegate: HTTPClientResponseDelegate {
3232
return ()
3333
}
3434
}
35+
36+
/// A utility function that runs the body code only in debug builds, without
37+
/// emitting compiler warnings.
38+
///
39+
/// This is currently the only way to do this in Swift: see
40+
/// https://forums.swift.org/t/support-debug-only-code/11037 for a discussion.
41+
@inlinable
42+
internal func debugOnly(_ body: () -> Void) {
43+
assert({ body(); return true }())
44+
}

0 commit comments

Comments
 (0)