From 663ddc80f2081c8f22e417cbac5f80270a93795e Mon Sep 17 00:00:00 2001 From: George Barnett Date: Mon, 1 Dec 2025 15:46:46 +0000 Subject: [PATCH] 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 --- .../Concurrency Primitives/TokenBucket.swift | 87 +++++++++++++------ .../Concurrency Primitives/TokenBucket.swift | 87 +++++++++++++------ docs/workarounds.md | 1 + 3 files changed, 125 insertions(+), 50 deletions(-) diff --git a/Sources/NIOFS/Internal/Concurrency Primitives/TokenBucket.swift b/Sources/NIOFS/Internal/Concurrency Primitives/TokenBucket.swift index 3a998f5b2..d80848a8a 100644 --- a/Sources/NIOFS/Internal/Concurrency Primitives/TokenBucket.swift +++ b/Sources/NIOFS/Internal/Concurrency Primitives/TokenBucket.swift @@ -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> - private let lock: NIOLock + private struct State { + var tokens: Int + var waiters: Deque> + + 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?) -> 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? { + if let next = self.waiters.popFirst() { + return next + } else { + self.tokens &+= 1 + return nil + } + } + } + + private let lock: NIOLockedValueBox 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? 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() } } diff --git a/Sources/_NIOFileSystem/Internal/Concurrency Primitives/TokenBucket.swift b/Sources/_NIOFileSystem/Internal/Concurrency Primitives/TokenBucket.swift index 3a998f5b2..d80848a8a 100644 --- a/Sources/_NIOFileSystem/Internal/Concurrency Primitives/TokenBucket.swift +++ b/Sources/_NIOFileSystem/Internal/Concurrency Primitives/TokenBucket.swift @@ -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> - private let lock: NIOLock + private struct State { + var tokens: Int + var waiters: Deque> + + 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?) -> 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? { + if let next = self.waiters.popFirst() { + return next + } else { + self.tokens &+= 1 + return nil + } + } + } + + private let lock: NIOLockedValueBox 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? 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() } } diff --git a/docs/workarounds.md b/docs/workarounds.md index d22442481..8d45ebcd2 100644 --- a/docs/workarounds.md +++ b/docs/workarounds.md @@ -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