mirror of
https://github.com/apple/swift-nio
synced 2026-11-22 04:30:41 +00:00
Dont hold lock over continuation in TokenBucket (#3455)
Motivation: The various 'withMumbleContinuation' APIs are supposed to be invoked synchronously with the caller. This assumption allows a lock to be acquired before the call and released from the body of the 'withMumbleContinuation' after e.g. storing the continuation. However this isn't the case and the job may be re-enqueued on the executor meaning that this is pattern is vulnerable to deadlocks. Modifications: - Drop and reacquire the lock in TokenBucket. - Switch to NIOLockedValueBox Result: Lower chance of deadlock
This commit is contained in:
1 parent
2bc6627d74
commit
663ddc80f2
3 files changed
+125
-50
No files matched your search
@@ -34,15 +34,46 @@ import NIOConcurrencyHelpers
|
||||
/// ``NIOLock``.
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
final class TokenBucket: @unchecked Sendable {
|
||||
private var tokens: Int
|
||||
private var waiters: Deque<CheckedContinuation<Void, Never>>
|
||||
private let lock: NIOLock
|
||||
private struct State {
|
||||
var tokens: Int
|
||||
var waiters: Deque<CheckedContinuation<Void, Never>>
|
||||
|
||||
enum TakeTokenResult {
|
||||
/// A token is available to use.
|
||||
case tookToken
|
||||
/// No token is available, call back with a continuation.
|
||||
case tryAgainWithContinuation
|
||||
/// No token is available, the continuation will be resumed with one later.
|
||||
case storedContinuation
|
||||
}
|
||||
|
||||
mutating func takeToken(continuation: CheckedContinuation<Void, Never>?) -> TakeTokenResult {
|
||||
if self.tokens > 0 {
|
||||
self.tokens &-= 1
|
||||
return .tookToken
|
||||
} else if let continuation = continuation {
|
||||
self.waiters.append(continuation)
|
||||
return .storedContinuation
|
||||
} else {
|
||||
return .tryAgainWithContinuation
|
||||
}
|
||||
}
|
||||
|
||||
mutating func returnToken() -> CheckedContinuation<Void, Never>? {
|
||||
if let next = self.waiters.popFirst() {
|
||||
return next
|
||||
} else {
|
||||
self.tokens &+= 1
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let lock: NIOLockedValueBox<State>
|
||||
|
||||
init(tokens: Int) {
|
||||
precondition(tokens >= 1, "Need at least one token!")
|
||||
self.tokens = tokens
|
||||
self.waiters = Deque()
|
||||
self.lock = NIOLock()
|
||||
self.lock = NIOLockedValueBox(State(tokens: tokens, waiters: []))
|
||||
}
|
||||
|
||||
/// Executes an `async` closure immediately when a token is available.
|
||||
@@ -60,29 +91,35 @@ final class TokenBucket: @unchecked Sendable {
|
||||
}
|
||||
|
||||
private func getToken() async {
|
||||
self.lock.lock()
|
||||
if self.tokens > 0 {
|
||||
self.tokens -= 1
|
||||
self.lock.unlock()
|
||||
return
|
||||
}
|
||||
switch self.lock.withLockedValue({ $0.takeToken(continuation: nil) }) {
|
||||
case .tookToken:
|
||||
()
|
||||
|
||||
await withCheckedContinuation {
|
||||
self.waiters.append($0)
|
||||
self.lock.unlock()
|
||||
case .tryAgainWithContinuation:
|
||||
// Holding the lock here *should* be safe but because of a bug in the runtime
|
||||
// it isn't, so drop the lock, create the continuation and try again.
|
||||
//
|
||||
// See also: https://github.com/swiftlang/swift/issues/85668
|
||||
await withCheckedContinuation { continuation in
|
||||
switch self.lock.withLockedValue({ $0.takeToken(continuation: continuation) }) {
|
||||
case .tookToken:
|
||||
continuation.resume()
|
||||
case .storedContinuation:
|
||||
()
|
||||
case .tryAgainWithContinuation:
|
||||
// Only possible when 'takeToken(continuation:)' is called with no continuation.
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
case .storedContinuation:
|
||||
// Only possible when 'takeToken(continuation:)' is called with a continuation.
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
private func returnToken() {
|
||||
if let waiter = self.lock.withLock({ () -> CheckedContinuation<Void, Never>? in
|
||||
if let nextWaiter = self.waiters.popFirst() {
|
||||
return nextWaiter
|
||||
}
|
||||
|
||||
self.tokens += 1
|
||||
return nil
|
||||
}) {
|
||||
waiter.resume()
|
||||
}
|
||||
let continuation = self.lock.withLockedValue { $0.returnToken() }
|
||||
continuation?.resume()
|
||||
}
|
||||
}
|
||||
@@ -34,15 +34,46 @@ import NIOConcurrencyHelpers
|
||||
/// ``NIOLock``.
|
||||
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
||||
final class TokenBucket: @unchecked Sendable {
|
||||
private var tokens: Int
|
||||
private var waiters: Deque<CheckedContinuation<Void, Never>>
|
||||
private let lock: NIOLock
|
||||
private struct State {
|
||||
var tokens: Int
|
||||
var waiters: Deque<CheckedContinuation<Void, Never>>
|
||||
|
||||
enum TakeTokenResult {
|
||||
/// A token is available to use.
|
||||
case tookToken
|
||||
/// No token is available, call back with a continuation.
|
||||
case tryAgainWithContinuation
|
||||
/// No token is available, the continuation will be resumed with one later.
|
||||
case storedContinuation
|
||||
}
|
||||
|
||||
mutating func takeToken(continuation: CheckedContinuation<Void, Never>?) -> TakeTokenResult {
|
||||
if self.tokens > 0 {
|
||||
self.tokens &-= 1
|
||||
return .tookToken
|
||||
} else if let continuation = continuation {
|
||||
self.waiters.append(continuation)
|
||||
return .storedContinuation
|
||||
} else {
|
||||
return .tryAgainWithContinuation
|
||||
}
|
||||
}
|
||||
|
||||
mutating func returnToken() -> CheckedContinuation<Void, Never>? {
|
||||
if let next = self.waiters.popFirst() {
|
||||
return next
|
||||
} else {
|
||||
self.tokens &+= 1
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let lock: NIOLockedValueBox<State>
|
||||
|
||||
init(tokens: Int) {
|
||||
precondition(tokens >= 1, "Need at least one token!")
|
||||
self.tokens = tokens
|
||||
self.waiters = Deque()
|
||||
self.lock = NIOLock()
|
||||
self.lock = NIOLockedValueBox(State(tokens: tokens, waiters: []))
|
||||
}
|
||||
|
||||
/// Executes an `async` closure immediately when a token is available.
|
||||
@@ -60,29 +91,35 @@ final class TokenBucket: @unchecked Sendable {
|
||||
}
|
||||
|
||||
private func getToken() async {
|
||||
self.lock.lock()
|
||||
if self.tokens > 0 {
|
||||
self.tokens -= 1
|
||||
self.lock.unlock()
|
||||
return
|
||||
}
|
||||
switch self.lock.withLockedValue({ $0.takeToken(continuation: nil) }) {
|
||||
case .tookToken:
|
||||
()
|
||||
|
||||
await withCheckedContinuation {
|
||||
self.waiters.append($0)
|
||||
self.lock.unlock()
|
||||
case .tryAgainWithContinuation:
|
||||
// Holding the lock here *should* be safe but because of a bug in the runtime
|
||||
// it isn't, so drop the lock, create the continuation and try again.
|
||||
//
|
||||
// See also: https://github.com/swiftlang/swift/issues/85668
|
||||
await withCheckedContinuation { continuation in
|
||||
switch self.lock.withLockedValue({ $0.takeToken(continuation: continuation) }) {
|
||||
case .tookToken:
|
||||
continuation.resume()
|
||||
case .storedContinuation:
|
||||
()
|
||||
case .tryAgainWithContinuation:
|
||||
// Only possible when 'takeToken(continuation:)' is called with no continuation.
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
case .storedContinuation:
|
||||
// Only possible when 'takeToken(continuation:)' is called with a continuation.
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
private func returnToken() {
|
||||
if let waiter = self.lock.withLock({ () -> CheckedContinuation<Void, Never>? in
|
||||
if let nextWaiter = self.waiters.popFirst() {
|
||||
return nextWaiter
|
||||
}
|
||||
|
||||
self.tokens += 1
|
||||
return nil
|
||||
}) {
|
||||
waiter.resume()
|
||||
}
|
||||
let continuation = self.lock.withLockedValue { $0.returnToken() }
|
||||
continuation?.resume()
|
||||
}
|
||||
}
|
||||
@@ -18,3 +18,4 @@ or cases where the Swift compiler was unable to sufficiently optimize the code:
|
||||
- https://github.com/apple/swift-nio/pull/3303
|
||||
- https://github.com/apple/swift-nio/pull/3452
|
||||
- https://github.com/apple/swift-nio/pull/3454
|
||||
- https://github.com/apple/swift-nio/pull/3455
|
||||
Reference in new issue
Block a user