mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
promise leak debugging
This commit is contained in:
1 parent
d0a7b539a8
commit
4a2425f5b7
4 files changed
+71
-62
No files matched your search
@@ -48,7 +48,6 @@ extension EmbeddedScheduledTask: Comparable {
|
||||
/// responsible for ensuring they never call into the `EmbeddedEventLoop` in an
|
||||
/// unsynchronized fashion.
|
||||
public class EmbeddedEventLoop: EventLoop {
|
||||
|
||||
/// The current "time" for this event loop. This is an amount in nanoseconds.
|
||||
private var now: UInt64 = 0
|
||||
|
||||
@@ -60,18 +59,6 @@ public class EmbeddedEventLoop: EventLoop {
|
||||
|
||||
var tasks = CircularBuffer<() -> ()>(initialRingCapacity: 2)
|
||||
|
||||
public func newPromise<T>() -> EventLoopPromise<T> {
|
||||
return EventLoopPromise<T>(eventLoop: self, checkForPossibleDeadlock: false)
|
||||
}
|
||||
|
||||
public func newFailedFuture<T>(error: Error) -> EventLoopFuture<T> {
|
||||
return EventLoopFuture<T>(eventLoop: self, checkForPossibleDeadlock: false, error: error)
|
||||
}
|
||||
|
||||
public func newSucceedFuture<T>(result: T) -> EventLoopFuture<T> {
|
||||
return EventLoopFuture<T>(eventLoop: self, checkForPossibleDeadlock: false, result: result)
|
||||
}
|
||||
|
||||
public func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws-> (T)) -> Scheduled<T> {
|
||||
let promise: EventLoopPromise<T> = newPromise()
|
||||
let readyTime = now + UInt64(`in`.nanoseconds)
|
||||
|
||||
+33
-22
@@ -94,23 +94,6 @@ public protocol EventLoop: EventLoopGroup {
|
||||
|
||||
/// Schedule a `task` that is executed by this `SelectableEventLoop` after the given amount of time.
|
||||
func scheduleTask<T>(in: TimeAmount, _ task: @escaping () throws-> (T)) -> Scheduled<T>
|
||||
|
||||
/// Creates and returns a new `EventLoopPromise` that will be notified using this `EventLoop` as execution `Thread`.
|
||||
func newPromise<T>() -> EventLoopPromise<T>
|
||||
|
||||
/// Creates and returns a new `EventLoopFuture` that is already marked as failed. Notifications will be done using this `EventLoop` as execution `Thread`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - error: the `Error` that is used by the `EventLoopFuture`.
|
||||
/// - returns: a failed `EventLoopFuture`.
|
||||
func newFailedFuture<T>(error: Error) -> EventLoopFuture<T>
|
||||
|
||||
/// Creates and returns a new `EventLoopFuture` that is already marked as success. Notifications will be done using this `EventLoop` as execution `Thread`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - result: the value that is used by the `EventLoopFuture`.
|
||||
/// - returns: a failed `EventLoopFuture`.
|
||||
func newSucceedFuture<T>(result: T) -> EventLoopFuture<T>
|
||||
}
|
||||
|
||||
/// Represents a time _interval_.
|
||||
@@ -190,7 +173,7 @@ extension TimeAmount: Comparable {
|
||||
|
||||
extension EventLoop {
|
||||
public func submit<T>(task: @escaping () throws-> (T)) -> EventLoopFuture<T> {
|
||||
let promise: EventLoopPromise<T> = newPromise()
|
||||
let promise: EventLoopPromise<T> = newPromise(file: #file, line: #line)
|
||||
|
||||
execute(task: {() -> () in
|
||||
do {
|
||||
@@ -203,16 +186,27 @@ extension EventLoop {
|
||||
return promise.futureResult
|
||||
}
|
||||
|
||||
public func newPromise<T>() -> EventLoopPromise<T> {
|
||||
return EventLoopPromise<T>(eventLoop: self, checkForPossibleDeadlock: true)
|
||||
/// Creates and returns a new `EventLoopPromise` that will be notified using this `EventLoop` as execution `Thread`.
|
||||
public func newPromise<T>(file: StaticString = #file, line: UInt = #line) -> EventLoopPromise<T> {
|
||||
return EventLoopPromise<T>(eventLoop: self, file: file, line: line)
|
||||
}
|
||||
|
||||
/// Creates and returns a new `EventLoopFuture` that is already marked as failed. Notifications will be done using this `EventLoop` as execution `Thread`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - error: the `Error` that is used by the `EventLoopFuture`.
|
||||
/// - returns: a failed `EventLoopFuture`.
|
||||
public func newFailedFuture<T>(error: Error) -> EventLoopFuture<T> {
|
||||
return EventLoopFuture<T>(eventLoop: self, checkForPossibleDeadlock: true, error: error)
|
||||
return EventLoopFuture<T>(eventLoop: self, error: error, file: "n/a", line: 0)
|
||||
}
|
||||
|
||||
/// Creates and returns a new `EventLoopFuture` that is already marked as success. Notifications will be done using this `EventLoop` as execution `Thread`.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - result: the value that is used by the `EventLoopFuture`.
|
||||
/// - returns: a failed `EventLoopFuture`.
|
||||
public func newSucceedFuture<T>(result: T) -> EventLoopFuture<T> {
|
||||
return EventLoopFuture<T>(eventLoop: self, checkForPossibleDeadlock: true, result: result)
|
||||
return EventLoopFuture<T>(eventLoop: self, result: result, file: "n/a", line: 0)
|
||||
}
|
||||
|
||||
public func next() -> EventLoop {
|
||||
@@ -292,6 +286,23 @@ internal final class SelectableEventLoop : EventLoop {
|
||||
let storageRefs: UnsafeMutableBufferPointer<Unmanaged<AnyObject>>
|
||||
|
||||
/// Creates a new `SelectableEventLoop` instance that is tied to the given `pthread_t`.
|
||||
|
||||
private let promiseCreationStoreLock = Lock()
|
||||
private var _promiseCreationStore: [ObjectIdentifier: (file: StaticString, line: UInt)] = [:]
|
||||
internal func promiseCreationStoreAdd<T>(future: EventLoopFuture<T>, file: StaticString, line: UInt) {
|
||||
precondition(_isDebugAssertConfiguration())
|
||||
self.promiseCreationStoreLock.withLock {
|
||||
self._promiseCreationStore[ObjectIdentifier(future)] = (file: file, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
internal func promiseCreationStoreRemove<T>(future: EventLoopFuture<T>) -> (file: StaticString, line: UInt) {
|
||||
precondition(_isDebugAssertConfiguration())
|
||||
return self.promiseCreationStoreLock.withLock {
|
||||
self._promiseCreationStore[ObjectIdentifier(future)]!
|
||||
}
|
||||
}
|
||||
|
||||
public init(thread: pthread_t) throws {
|
||||
self.selector = try NIO.Selector()
|
||||
self.thread = thread
|
||||
|
||||
@@ -140,8 +140,8 @@ public struct EventLoopPromise<T> {
|
||||
/**
|
||||
Public initializer
|
||||
*/
|
||||
init(eventLoop: EventLoop, checkForPossibleDeadlock: Bool) {
|
||||
futureResult = EventLoopFuture<T>(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock)
|
||||
init(eventLoop: EventLoop, file: StaticString, line: UInt) {
|
||||
futureResult = EventLoopFuture<T>(eventLoop: eventLoop, file: file, line: line)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -260,7 +260,6 @@ public final class EventLoopFuture<T> {
|
||||
}
|
||||
}
|
||||
fileprivate let _fulfilled: Atomic<Bool>
|
||||
fileprivate let checkForPossibleDeadlock: Bool
|
||||
public let eventLoop: EventLoop
|
||||
|
||||
internal var fulfilled: Bool {
|
||||
@@ -271,30 +270,42 @@ public final class EventLoopFuture<T> {
|
||||
/// These callbacks may give values to other EventLoopFutures; if that happens, they return any callbacks from those EventLoopFutures so that we can run the entire chain from the top without recursing.
|
||||
fileprivate var callbacks: CallbackList = CallbackList()
|
||||
|
||||
private init(eventLoop: EventLoop, checkForPossibleDeadlock: Bool, value: EventLoopFutureValue<T>?, file: StaticString = #file, line: Int = #line) {
|
||||
private init(eventLoop: EventLoop, value: EventLoopFutureValue<T>?, file: StaticString, line: UInt) {
|
||||
self.eventLoop = eventLoop
|
||||
self.checkForPossibleDeadlock = checkForPossibleDeadlock
|
||||
self.value = value
|
||||
self._fulfilled = Atomic(value: value != nil)
|
||||
|
||||
if _isDebugAssertConfiguration() {
|
||||
if let me = eventLoop as? SelectableEventLoop {
|
||||
me.promiseCreationStoreAdd(future: self, file: file, line: line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fileprivate convenience init(eventLoop: EventLoop, checkForPossibleDeadlock: Bool, file: StaticString = #file, line: Int = #line) {
|
||||
self.init(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock, value: nil, file: file, line: line)
|
||||
fileprivate convenience init(eventLoop: EventLoop, file: StaticString, line: UInt) {
|
||||
self.init(eventLoop: eventLoop, value: nil, file: file, line: line)
|
||||
}
|
||||
|
||||
/// A EventLoopFuture<T> that has already succeeded
|
||||
convenience init(eventLoop: EventLoop, checkForPossibleDeadlock: Bool, result: T, file: StaticString = #file, line: Int = #line) {
|
||||
self.init(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock, value: .success(result), file: file, line: line)
|
||||
convenience init(eventLoop: EventLoop, result: T, file: StaticString, line: UInt) {
|
||||
self.init(eventLoop: eventLoop, value: .success(result), file: file, line: line)
|
||||
}
|
||||
|
||||
/// A EventLoopFuture<T> that has already failed
|
||||
convenience init(eventLoop: EventLoop, checkForPossibleDeadlock: Bool, error: Error, file: StaticString = #file, line: Int = #line) {
|
||||
self.init(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock, value: .failure(error), file: file, line: line)
|
||||
convenience init(eventLoop: EventLoop, error: Error, file: StaticString, line: UInt) {
|
||||
self.init(eventLoop: eventLoop, value: .failure(error), file: file, line: line)
|
||||
}
|
||||
|
||||
deinit {
|
||||
precondition(fulfilled, "leaking an unfulfilled EventLoopPromise")
|
||||
if _isDebugAssertConfiguration(), let eventLoop = self.eventLoop as? SelectableEventLoop {
|
||||
let creation = eventLoop.promiseCreationStoreRemove(future: self)
|
||||
if !fulfilled {
|
||||
fatalError("leaking promise created at \(creation)", file: creation.file, line: creation.line)
|
||||
}
|
||||
} else {
|
||||
precondition(fulfilled, "leaking an unfulfilled Promise")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,8 +341,8 @@ extension EventLoopFuture {
|
||||
- returns: A future that will receive the eventual value
|
||||
*/
|
||||
|
||||
public func then<U>(callback: @escaping (T) throws -> EventLoopFuture<U>) -> EventLoopFuture<U> {
|
||||
let next = EventLoopPromise<U>(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock)
|
||||
public func then<U>(file: StaticString = #file, line: UInt = #line, callback: @escaping (T) throws -> EventLoopFuture<U>) -> EventLoopFuture<U> {
|
||||
let next = EventLoopPromise<U>(eventLoop: eventLoop, file: file, line: line)
|
||||
_whenComplete {
|
||||
switch self.value! {
|
||||
case .success(let t):
|
||||
@@ -373,8 +384,8 @@ extension EventLoopFuture {
|
||||
Generally, a simple closure provided to `then()` should never block. If you need to do something time-consuming, your closure can schedule the operation on another queue and return another `EventLoopFuture<>` object instead. See `then(queue:callback:)` for a convenient way to do this.
|
||||
*/
|
||||
|
||||
public func then<U>(callback: @escaping (T) throws -> (U)) -> EventLoopFuture<U> {
|
||||
return then { return EventLoopFuture<U>(eventLoop: self.eventLoop, checkForPossibleDeadlock: self.checkForPossibleDeadlock, result: try callback($0)) }
|
||||
public func then<U>(file: StaticString = #file, line: UInt = #line, callback: @escaping (T) throws -> (U)) -> EventLoopFuture<U> {
|
||||
return then { return EventLoopFuture<U>(eventLoop: self.eventLoop, result: try callback($0), file: file, line: line) }
|
||||
}
|
||||
|
||||
|
||||
@@ -395,8 +406,8 @@ extension EventLoopFuture {
|
||||
|
||||
This supports the same overloads as `then()`, including allowing the callback to return a `EventLoopFuture<T>`.
|
||||
*/
|
||||
public func thenIfError(callback: @escaping (Error) throws -> EventLoopFuture<T>) -> EventLoopFuture<T> {
|
||||
let next = EventLoopPromise<T>(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock)
|
||||
public func thenIfError(file: StaticString = #file, line: UInt = #line, callback: @escaping (Error) throws -> EventLoopFuture<T>) -> EventLoopFuture<T> {
|
||||
let next = EventLoopPromise<T>(eventLoop: eventLoop, file: file, line: line)
|
||||
_whenComplete {
|
||||
switch self.value! {
|
||||
case .success(let t):
|
||||
@@ -415,8 +426,8 @@ extension EventLoopFuture {
|
||||
return next.futureResult
|
||||
}
|
||||
|
||||
public func thenIfError(callback: @escaping (Error) throws -> T) -> EventLoopFuture<T> {
|
||||
return thenIfError { return EventLoopFuture<T>(eventLoop: self.eventLoop, checkForPossibleDeadlock: self.checkForPossibleDeadlock, result: try callback($0)) }
|
||||
public func thenIfError(file: StaticString = #file, line: UInt = #line, callback: @escaping (Error) throws -> T) -> EventLoopFuture<T> {
|
||||
return thenIfError { return EventLoopFuture<T>(eventLoop: self.eventLoop, result: try callback($0), file: file, line: line) }
|
||||
}
|
||||
|
||||
|
||||
@@ -487,9 +498,9 @@ extension EventLoopFuture {
|
||||
* provided EventLoopFuture both succeed. It then provides the pair
|
||||
* of results. If either one fails, the combined EventLoopFuture will fail.
|
||||
*/
|
||||
public func and<U>(_ other: EventLoopFuture<U>) -> EventLoopFuture<(T,U)> {
|
||||
public func and<U>(_ other: EventLoopFuture<U>, file: StaticString = #file, line: UInt = #line) -> EventLoopFuture<(T,U)> {
|
||||
let andlock = Lock()
|
||||
let promise = EventLoopPromise<(T,U)>(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock)
|
||||
let promise = EventLoopPromise<(T,U)>(eventLoop: eventLoop, file: file, line: line)
|
||||
var tvalue: T?
|
||||
var uvalue: U?
|
||||
|
||||
@@ -535,8 +546,8 @@ extension EventLoopFuture {
|
||||
* This is just syntactic sugar for
|
||||
* future.and(Future<U>(result: result))
|
||||
*/
|
||||
public func and<U>(result: U) -> EventLoopFuture<(T,U)> {
|
||||
return and(EventLoopFuture<U>(eventLoop: self.eventLoop, checkForPossibleDeadlock: self.checkForPossibleDeadlock, result:result))
|
||||
public func and<U>(result: U, file: StaticString = #file, line: UInt = #line) -> EventLoopFuture<(T,U)> {
|
||||
return and(EventLoopFuture<U>(eventLoop: self.eventLoop, result:result, file: file, line: line))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,7 +573,7 @@ extension EventLoopFuture {
|
||||
|
||||
extension EventLoopFuture {
|
||||
public func wait() throws -> T {
|
||||
if self.checkForPossibleDeadlock {
|
||||
if !(self.eventLoop is EmbeddedEventLoop) {
|
||||
precondition(!eventLoop.inEventLoop, "wait() must not be called when on the EventLoop")
|
||||
}
|
||||
|
||||
|
||||
@@ -22,13 +22,13 @@ enum EventLoopFutureTestError : Error {
|
||||
class EventLoopFutureTest : XCTestCase {
|
||||
func testFutureFulfilledIfHasResult() throws {
|
||||
let eventLoop = EmbeddedEventLoop()
|
||||
let f = EventLoopFuture(eventLoop: eventLoop, checkForPossibleDeadlock: true, result: 5)
|
||||
let f = EventLoopFuture(eventLoop: eventLoop, result: 5, file: #file, line: #line)
|
||||
XCTAssertTrue(f.fulfilled)
|
||||
}
|
||||
|
||||
func testFutureFulfilledIfHasError() throws {
|
||||
let eventLoop = EmbeddedEventLoop()
|
||||
let f = EventLoopFuture<Void>(eventLoop: eventLoop, checkForPossibleDeadlock: true, error: EventLoopFutureTestError.example)
|
||||
let f = EventLoopFuture<Void>(eventLoop: eventLoop, error: EventLoopFutureTestError.example, file: #file, line: #line)
|
||||
XCTAssertTrue(f.fulfilled)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user