-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Concurrency: Introduce a CheckedContinuation
adapter.
#35188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@swift-ci Please test |
Build failed |
7623dbe
to
fb4ad41
Compare
@swift-ci Please test |
fb4ad41
to
02238bc
Compare
@swift-ci Please test |
Build failed |
02238bc
to
92714ce
Compare
@swift-ci Please test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fantastic! Should this be the user-visible implementation of withUnsafe(Throwing)Continuation
?
if let c = continuation { | ||
c.resume(returning: x) | ||
// Clear out the continuation so we don't try to resume again | ||
continuation = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps CheckedContinuation
should be thread-safe? Here we use an ordinary write to clear out the continuation once its fired, which could be racy in some of the most remarkable misuses of continuations, something like:
var cont : CheckedContinuation<Void>? = nil
func capture() async {
await withCheckedContinuation { k in cont = k }
}
func access() async {
async let x = cont!.resume(returning: ())
async let y = cont!.resume(returning: ())
}
To help catch runtime issues adopting `withUnsafeContinuation`, such as callback-based APIs that misleadingly invoke their callback multiple times and/or not at all, provide a couple of classes that can take ownership of a fresh `UnsafeContinuation` or `UnsafeThrowingContinuation`, and log attempts to resume the continuation multiple times or discard the object without ever resuming the continuation.
92714ce
to
5f09745
Compare
@kavon Alright, I made it thread-safe by doing an atomic xchg with null when we take the continuation. @slavapestov I also made the type resilient so we can use more clever mechanisms for checking if we get them later. |
@swift-ci Please test |
To help catch runtime issues adopting
withUnsafeContinuation
, such as callback-based APIs that misleadinglyinvoke their callback multiple times and/or not at all, provide a couple of classes that can take ownership of
a fresh
UnsafeContinuation
orUnsafeThrowingContinuation
, and log attempts to resume the continuation multiple timesor discard the object without ever resuming the continuation.