diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 1ae114385..2e7c1abff 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -188,6 +188,12 @@ public final class EmbeddedEventLoop: EventLoop { } } + public func _preconditionSafeToWait(file: StaticString, line: UInt) { + // EmbeddedEventLoop always allows a wait, as waiting will essentially always block + // wait() + return + } + deinit { precondition(scheduledTasks.isEmpty, "Embedded event loop freed with unexecuted scheduled tasks!") } diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index d18a33dd9..9a5d5b02e 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -273,6 +273,16 @@ public protocol EventLoop: EventLoopGroup { /// Contrary to `makeSucceededFuture`, `makeSucceededVoidFuture` is a customization point for `EventLoop`s which /// allows `EventLoop`s to cache a pre-succeded `Void` future to prevent superfluous allocations. func makeSucceededVoidFuture() -> EventLoopFuture + + /// Must crash if it is not safe to call `wait()` on an `EventLoopFuture`. + /// + /// This method is a debugging hook that can be used to override the behaviour of `EventLoopFuture.wait()` when called. + /// By default this simply becomes `preconditionNotInEventLoop`, but some `EventLoop`s are capable of more exhaustive + /// checking and can validate that the wait is not occuring on an entire `EventLoopGroup`, or even more broadly. + /// + /// This method should not be called by users directly, it should only be implemented by `EventLoop` implementers that + /// need to customise the behaviour. + func _preconditionSafeToWait(file: StaticString, line: UInt) } extension EventLoop { @@ -280,6 +290,10 @@ extension EventLoop { public func makeSucceededVoidFuture() -> EventLoopFuture { return EventLoopFuture(eventLoop: self, value: (), file: "n/a", line: 0) } + + public func _preconditionSafeToWait(file: StaticString, line: UInt) { + self.preconditionNotInEventLoop(file: file, line: line) + } } extension EventLoopGroup { diff --git a/Sources/NIO/EventLoopFuture.swift b/Sources/NIO/EventLoopFuture.swift index d018b1740..5675270e5 100644 --- a/Sources/NIO/EventLoopFuture.swift +++ b/Sources/NIO/EventLoopFuture.swift @@ -922,21 +922,7 @@ extension EventLoopFuture { /// - throws: The error value of the `EventLoopFuture` if it errors. @inlinable public func wait(file: StaticString = #file, line: UInt = #line) throws -> Value { - if !(self.eventLoop is EmbeddedEventLoop) { - let explainer: () -> String = { """ -BUG DETECTED: wait() must not be called when on an EventLoop. -Calling wait() on any EventLoop can lead to -- deadlocks -- stalling processing of other connections (Channels) that are handled on the EventLoop that wait was called on - -Further information: -- current eventLoop: \(MultiThreadedEventLoopGroup.currentEventLoop.debugDescription) -- event loop associated to future: \(self.eventLoop) -""" - } - precondition(!eventLoop.inEventLoop, explainer(), file: file, line: line) - precondition(MultiThreadedEventLoopGroup.currentEventLoop == nil, explainer(), file: file, line: line) - } + self.eventLoop._preconditionSafeToWait(file: file, line: line) var v: Result? = nil let lock = ConditionLock(value: 0) diff --git a/Sources/NIO/SelectableEventLoop.swift b/Sources/NIO/SelectableEventLoop.swift index 24633fac9..96ce458fb 100644 --- a/Sources/NIO/SelectableEventLoop.swift +++ b/Sources/NIO/SelectableEventLoop.swift @@ -119,6 +119,23 @@ internal final class SelectableEventLoop: EventLoop { } } + @usableFromInline + internal func _preconditionSafeToWait(file: StaticString, line: UInt) { + let explainer: () -> String = { """ +BUG DETECTED: wait() must not be called when on an EventLoop. +Calling wait() on any EventLoop can lead to +- deadlocks +- stalling processing of other connections (Channels) that are handled on the EventLoop that wait was called on + +Further information: +- current eventLoop: \(MultiThreadedEventLoopGroup.currentEventLoop.debugDescription) +- event loop associated to future: \(self) +""" + } + precondition(!self.inEventLoop, explainer(), file: file, line: line) + precondition(MultiThreadedEventLoopGroup.currentEventLoop == nil, explainer(), file: file, line: line) + } + @usableFromInline internal var _validInternalStateToScheduleTasks: Bool { switch self.internalState {