Skip to content

Commit

Permalink
improvement: pass down file/line via makeFutureWithResultOfTask
Browse files Browse the repository at this point in the history
In some circumstances it can be difficult to know which specific call to
makeFutureWithTask leaked a promise. Adds a new function that passes
down file/line to help aid debugging such situations.
  • Loading branch information
Austinpayne committed May 17, 2024
1 parent 609469f commit 25d29f5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/NIOCore/AsyncAwaitSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,32 @@ struct AsyncSequenceFromIterator<AsyncIterator: AsyncIteratorProtocol>: AsyncSeq

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension EventLoop {
@available(*, deprecated, renamed: "makeFutureWithResultOfTask", message: "Prefer makeFutureWithResultOfTask as it will track the original creator of the promise.")
@inlinable
public func makeFutureWithTask<Return>(_ body: @Sendable @escaping () async throws -> Return) -> EventLoopFuture<Return> {
let promise = self.makePromise(of: Return.self)
promise.completeWithTask(body)
return promise.futureResult
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension EventLoop {
/// Make a future whose body runs in an `async` function `body`.
///
/// This function can be used to bridge the `async` world into an `EventLoopFuture`.
///
/// - parameters:
/// - body: The `async` function to run.
/// - returns: A `Future` which completes when `body` finishes running.
@inlinable
public func makeFutureWithResultOfTask<Return>(
file: StaticString = #fileID,
line: UInt = #line,
_ body: @Sendable @escaping () async throws -> Return
) -> EventLoopFuture<Return> {
let promise = self.makePromise(of: Return.self, file: file, line: line)
promise.completeWithTask(body)
return promise.futureResult
}
}

0 comments on commit 25d29f5

Please sign in to comment.