mirror of
https://github.com/swift-server/async-http-client.git
synced 2026-06-02 07:37:34 +00:00
Fix Connection Creation Crash (#873)
### Motivation When creating a connection, we wrongfully assumed that `failedToCreateNewConnection` will always be called before `http*ConnectionClosed` in the `HTTPConnectionPoolStateMachine`. However this is far from correct. In NIO Futures are fulfilled before `ChannelHandler` callbacks. Ordering in futures should not be assumed in such a complex project. ### Change We change the `http*ConnectionClosed` methods to be noops, if the connection is in the starting state. We instead wait for the `failedToCreateNewConnection` to create backoff timers and friends. rdar://164674912 --------- Co-authored-by: George Barnett <gbarnett@apple.com>
This commit is contained in:
co-authored by
George Barnett
parent
ce04df0613
commit
3c45dbde2d
+35
-18
@@ -166,10 +166,22 @@ extension HTTPConnectionPool {
|
||||
}
|
||||
}
|
||||
|
||||
mutating func fail() {
|
||||
enum FailAction {
|
||||
case removeConnection
|
||||
case none
|
||||
}
|
||||
|
||||
mutating func fail() -> FailAction {
|
||||
switch self.state {
|
||||
case .starting, .backingOff, .idle, .leased:
|
||||
case .starting:
|
||||
// If the connection fails while we are starting it, the fail call raced with
|
||||
// `failedToConnect` (promises are succeeded or failed before channel handler
|
||||
// callbacks). let's keep the state in `starting`, so that `failedToConnect` can
|
||||
// create a backoff timer.
|
||||
return .none
|
||||
case .backingOff, .idle, .leased:
|
||||
self.state = .closed
|
||||
return .removeConnection
|
||||
case .closed:
|
||||
preconditionFailure("Invalid state: \(self.state)")
|
||||
}
|
||||
@@ -559,23 +571,28 @@ extension HTTPConnectionPool {
|
||||
}
|
||||
|
||||
let use: ConnectionUse
|
||||
self.connections[index].fail()
|
||||
let eventLoop = self.connections[index].eventLoop
|
||||
let starting: Int
|
||||
if index < self.overflowIndex {
|
||||
use = .generalPurpose
|
||||
starting = self.startingGeneralPurposeConnections
|
||||
} else {
|
||||
use = .eventLoop(eventLoop)
|
||||
starting = self.startingEventLoopConnections(on: eventLoop)
|
||||
}
|
||||
switch self.connections[index].fail() {
|
||||
case .removeConnection:
|
||||
let eventLoop = self.connections[index].eventLoop
|
||||
let starting: Int
|
||||
if index < self.overflowIndex {
|
||||
use = .generalPurpose
|
||||
starting = self.startingGeneralPurposeConnections
|
||||
} else {
|
||||
use = .eventLoop(eventLoop)
|
||||
starting = self.startingEventLoopConnections(on: eventLoop)
|
||||
}
|
||||
|
||||
let context = FailedConnectionContext(
|
||||
eventLoop: eventLoop,
|
||||
use: use,
|
||||
connectionsStartingForUseCase: starting
|
||||
)
|
||||
return (index, context)
|
||||
let context = FailedConnectionContext(
|
||||
eventLoop: eventLoop,
|
||||
use: use,
|
||||
connectionsStartingForUseCase: starting
|
||||
)
|
||||
return (index, context)
|
||||
|
||||
case .none:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Migration
|
||||
|
||||
Reference in New Issue
Block a user