Unified implementation for withTimeout family#63
Open
rintaro wants to merge 2 commits into
Open
Conversation
On platforms with SwiftStdlib 6.2 (macOS/iOS/macCatalyst 26+), delegate to withTaskPriorityEscalationHandler. The new API is event-driven (no polling) and integrated with the runtime, so it reacts immediately to escalations. Mark the wrapper @available(..., deprecated: 26.0). Once the min deployment target reaches 26, the deprecation warning fires at every call site, prompting migration to withTaskPriorityEscalationHandler and removal of this wrapper. Change the function from `throws` to `rethrows` following the stdlib version. Also, use `RefBox<Atomic<_>>` for the previous priority storage. Atomic is lighter, and actually simplifies the code. The old codepath is extracted as a function and kept tested even on platforms with built-in withTaskPriorityEscalationHandler.
rintaro
commented
May 29, 2026
| if didHitTimeout.value { | ||
| await resultReceivedAfterTimeout(result) | ||
| } | ||
| continuation.yield(result) |
Member
Author
There was a problem hiding this comment.
I think there was actually a correctness issue here.
After didHitTimeout.value check passes but before continuation.yield(result), the timeout task might have set didHitTimeout and continuation.yield(nil). So the result is timeout but resultReceivedAfterTimeout never get called.
a049383 to
4881218
Compare
Introduce `withTimeoutResult(_:body:resultReceivedAfterTimeout:)` as a unified core of the `withTimeout` family. The existing `withTimeout` overloads become thin `@inlinable` switches over `WithTimeoutResult`, forwarding errors via `rethrows`. `withTimeoutResult` supports an optional late-result callback. Late errors after the timeout are dropped silently because the callback signature accepts only the success value, so errors have nowhere to go. Implementation note: `bodyTask` itself yields its outcome onto the internal `AsyncStream` instead of a separate forwarder task. Adding a forwarder that awaits `bodyTask.result` proved unreliable — priority escalation through the await chain didn't always reach `bodyTask`, leaving `Task.currentPriority` inside `body` stuck at the original priority.
4881218 to
f8a0aa3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduce
withTimeoutResult(_:body:resultReceivedAfterTimeout:)returning aWithTimeoutResult<T>enum (.result(Result<T>)/.timedOut). ExistingwithTimeoutoverloads become thin switches over it. The optional late-result callback receives successful late results only.withTaskPriorityChangedHandlernow delegates to stdlib'swithTaskPriorityEscalationHandleron macOS 26+, giving event-driven priority reaction instead of polling.Also
withTaskPriorityChangedHandlerand all thewithTimeoutoverloads are nowrethrows.Added some test cases to ensure old
withTaskPriorityChangedHandlerimplementation keep working even when testing withwithTaskPriorityEscalationHandler.