Skip to content

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

Merged
merged 2 commits into from
Jan 11, 2021

Conversation

jckarter
Copy link
Contributor

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.

@jckarter jckarter requested a review from DougGregor December 22, 2020 00:12
@jckarter
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7623dbe463af0606f6be35e176bfd222e4b1efca

@jckarter jckarter force-pushed the checked-continuation branch from 7623dbe to fb4ad41 Compare December 22, 2020 17:11
@jckarter
Copy link
Contributor Author

@swift-ci Please test

@jckarter jckarter force-pushed the checked-continuation branch from fb4ad41 to 02238bc Compare December 22, 2020 17:30
@jckarter
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 02238bc538d75b87041a07e86e641f9ddf21d690

@jckarter jckarter force-pushed the checked-continuation branch from 02238bc to 92714ce Compare December 22, 2020 20:34
@jckarter
Copy link
Contributor Author

@swift-ci Please test

Copy link
Member

@DougGregor DougGregor left a 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
Copy link
Member

@kavon kavon Jan 5, 2021

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.
@jckarter jckarter force-pushed the checked-continuation branch from 92714ce to 5f09745 Compare January 8, 2021 22:54
@jckarter
Copy link
Contributor Author

jckarter commented Jan 8, 2021

@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.

@jckarter
Copy link
Contributor Author

jckarter commented Jan 8, 2021

@swift-ci Please test

@jckarter jckarter merged commit f2d28cf into swiftlang:main Jan 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants