Skip to content

Add AsyncThrowingStream.init(unfolding:onCancel:) to handle cancellation. #78723

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

adlich
Copy link

@adlich adlich commented Jan 17, 2025

It shares implementation with and becomes the general case of the existing init(unfolding:), and given the current implementation's use of withTaskCancellationHandler, the onCancel closure can run concurrently with the unfolding closure if cancellation happens during production, or ahead of it when cancellation has already taken place. See doc and the last 3 test cases added in the 1st commit.

Also adds tests for init(unfolding:).

Resolves #77974.

…ion.

It shares implementation with and becomes the general case of the existing `init(unfolding:)`, and given the current implementation's use of `withTaskCancellationHandler`, the `onCancel` closure can run concurrently with the `unfolding` closure if cancellation happens during production, or ahead of it when cancellation has already taken place. See doc and the last 3 test cases added.

Also adds tests for `init(unfolding:)`.

Resolves swiftlang#77974.
@@ -72,6 +72,17 @@ class NotSendable {}
}
}

tests.test("unfold with no awaiting next throwing") {
_ = AsyncThrowingStream(unfolding: { return "hello" })
Copy link
Author

@adlich adlich Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if I didn't quite grasp the underlying philosophy of test naming and test case matrix in this file. Please let me know if you'd suggest fewer, more, different and/or differently named tests.


tests.test("unfold awaiting next and cancel throwing") {
let expectation = Expectation()
let task = Task.detached {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if you have better suggestions for testing cancellation.

///
/// `onCancel` may be executed concurrently with `produce` and will be
/// executed ahead of it if the task had already been cancelled an execution
/// of `produce`.
Copy link
Author

@adlich adlich Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: Behavior mentioned in description (concurrency of cancellation call itself.)

/// print(error)
/// }
///
@available(SwiftStdlib 6.1, *)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read [1] and noticed [2] (6.1 and 6.2 defined) but am still unsure which is appropriate.

[1] https://github.com/swiftlang/swift/blob/main/docs/LibraryEvolution.rst
[2] https://github.com/swiftlang/swift/blob/main/utils/availability-macros.def

) where Failure == Error {
let storage: _AsyncStreamCriticalStorage<
Optional<() async throws -> Element?>
> = .create(produce)
Copy link
Author

@adlich adlich Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My best interpretation of line-breaking guidelines in [1]. Some other possibly "questionable" ones in tests as well. Happy to change as preferred.

[1] https://github.com/swiftlang/swift/blob/main/docs/StandardLibraryProgrammersManual.md#line-breaking

@adlich
Copy link
Author

adlich commented Jan 17, 2025

Please review and trigger tests. Thank you!

@adlich adlich marked this pull request as ready for review January 17, 2025 23:32
@adlich adlich requested review from a team and ktoso as code owners January 17, 2025 23:32
@@ -353,6 +353,59 @@ class NotSendable {}
expectTrue(expectation.fulfilled)
}

tests.test("unfold awaiting next with ignored cancellation throwing") {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps intentionally skipped before, these may be more important now, as regressions could hide in the logic that starts becoming more complex than simply resetting storage state. Once again, I'm very open to feedback w.r.t. testing.

@adlich
Copy link
Author

adlich commented Jan 22, 2025

cc: @phausler
(Per #76571 (review))

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.

AsyncThrowingStream.init(unfolding:) has no onCancel parameter
1 participant