diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 60e6d6b18..da4b53b34 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -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() -> EventLoopPromise { - return EventLoopPromise(eventLoop: self, checkForPossibleDeadlock: false) - } - - public func newFailedFuture(error: Error) -> EventLoopFuture { - return EventLoopFuture(eventLoop: self, checkForPossibleDeadlock: false, error: error) - } - - public func newSucceedFuture(result: T) -> EventLoopFuture { - return EventLoopFuture(eventLoop: self, checkForPossibleDeadlock: false, result: result) - } - public func scheduleTask(in: TimeAmount, _ task: @escaping () throws-> (T)) -> Scheduled { let promise: EventLoopPromise = newPromise() let readyTime = now + UInt64(`in`.nanoseconds) diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index 0a9084a11..3483771a8 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -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(in: TimeAmount, _ task: @escaping () throws-> (T)) -> Scheduled - - /// Creates and returns a new `EventLoopPromise` that will be notified using this `EventLoop` as execution `Thread`. - func newPromise() -> EventLoopPromise - - /// 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(error: Error) -> EventLoopFuture - - /// 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(result: T) -> EventLoopFuture } /// Represents a time _interval_. @@ -190,7 +173,7 @@ extension TimeAmount: Comparable { extension EventLoop { public func submit(task: @escaping () throws-> (T)) -> EventLoopFuture { - let promise: EventLoopPromise = newPromise() + let promise: EventLoopPromise = newPromise(file: #file, line: #line) execute(task: {() -> () in do { @@ -203,16 +186,27 @@ extension EventLoop { return promise.futureResult } - public func newPromise() -> EventLoopPromise { - return EventLoopPromise(eventLoop: self, checkForPossibleDeadlock: true) + /// Creates and returns a new `EventLoopPromise` that will be notified using this `EventLoop` as execution `Thread`. + public func newPromise(file: StaticString = #file, line: UInt = #line) -> EventLoopPromise { + return EventLoopPromise(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(error: Error) -> EventLoopFuture { - return EventLoopFuture(eventLoop: self, checkForPossibleDeadlock: true, error: error) + return EventLoopFuture(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(result: T) -> EventLoopFuture { - return EventLoopFuture(eventLoop: self, checkForPossibleDeadlock: true, result: result) + return EventLoopFuture(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> /// 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(future: EventLoopFuture, file: StaticString, line: UInt) { + precondition(_isDebugAssertConfiguration()) + self.promiseCreationStoreLock.withLock { + self._promiseCreationStore[ObjectIdentifier(future)] = (file: file, line: line) + } + } + + internal func promiseCreationStoreRemove(future: EventLoopFuture) -> (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 diff --git a/Sources/NIO/EventLoopFuture.swift b/Sources/NIO/EventLoopFuture.swift index 4e29a6700..66ed04580 100644 --- a/Sources/NIO/EventLoopFuture.swift +++ b/Sources/NIO/EventLoopFuture.swift @@ -140,8 +140,8 @@ public struct EventLoopPromise { /** Public initializer */ - init(eventLoop: EventLoop, checkForPossibleDeadlock: Bool) { - futureResult = EventLoopFuture(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock) + init(eventLoop: EventLoop, file: StaticString, line: UInt) { + futureResult = EventLoopFuture(eventLoop: eventLoop, file: file, line: line) } /** @@ -260,7 +260,6 @@ public final class EventLoopFuture { } } fileprivate let _fulfilled: Atomic - fileprivate let checkForPossibleDeadlock: Bool public let eventLoop: EventLoop internal var fulfilled: Bool { @@ -271,30 +270,42 @@ public final class EventLoopFuture { /// 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?, file: StaticString = #file, line: Int = #line) { + private init(eventLoop: EventLoop, value: EventLoopFutureValue?, 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 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 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(callback: @escaping (T) throws -> EventLoopFuture) -> EventLoopFuture { - let next = EventLoopPromise(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock) + public func then(file: StaticString = #file, line: UInt = #line, callback: @escaping (T) throws -> EventLoopFuture) -> EventLoopFuture { + let next = EventLoopPromise(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(callback: @escaping (T) throws -> (U)) -> EventLoopFuture { - return then { return EventLoopFuture(eventLoop: self.eventLoop, checkForPossibleDeadlock: self.checkForPossibleDeadlock, result: try callback($0)) } + public func then(file: StaticString = #file, line: UInt = #line, callback: @escaping (T) throws -> (U)) -> EventLoopFuture { + return then { return EventLoopFuture(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`. */ - public func thenIfError(callback: @escaping (Error) throws -> EventLoopFuture) -> EventLoopFuture { - let next = EventLoopPromise(eventLoop: eventLoop, checkForPossibleDeadlock: checkForPossibleDeadlock) + public func thenIfError(file: StaticString = #file, line: UInt = #line, callback: @escaping (Error) throws -> EventLoopFuture) -> EventLoopFuture { + let next = EventLoopPromise(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 { - return thenIfError { return EventLoopFuture(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 { + return thenIfError { return EventLoopFuture(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(_ other: EventLoopFuture) -> EventLoopFuture<(T,U)> { + public func and(_ other: EventLoopFuture, 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(result: result)) */ - public func and(result: U) -> EventLoopFuture<(T,U)> { - return and(EventLoopFuture(eventLoop: self.eventLoop, checkForPossibleDeadlock: self.checkForPossibleDeadlock, result:result)) + public func and(result: U, file: StaticString = #file, line: UInt = #line) -> EventLoopFuture<(T,U)> { + return and(EventLoopFuture(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") } diff --git a/Tests/NIOTests/EventLoopFutureTest.swift b/Tests/NIOTests/EventLoopFutureTest.swift index ba72a1c33..9612146b4 100644 --- a/Tests/NIOTests/EventLoopFutureTest.swift +++ b/Tests/NIOTests/EventLoopFutureTest.swift @@ -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(eventLoop: eventLoop, checkForPossibleDeadlock: true, error: EventLoopFutureTestError.example) + let f = EventLoopFuture(eventLoop: eventLoop, error: EventLoopFutureTestError.example, file: #file, line: #line) XCTAssertTrue(f.fulfilled) }