-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Concurrency] improve cancellation handler to not hop and use caller execution context #80753
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
ktoso
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
ktoso:wip-improve-withTaskCancellationHandler-with-caller-execution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−20
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,10 +67,26 @@ import Swift | |
/// Therefore, if a cancellation handler must acquire a lock, other code should | ||
/// not cancel tasks or resume continuations while holding that lock. | ||
@available(SwiftStdlib 5.1, *) | ||
#if !$Embedded | ||
@backDeployed(before: SwiftStdlib 6.0) | ||
#endif | ||
@_alwaysEmitIntoClient | ||
nonisolated(nonsending) | ||
public func withTaskCancellationHandler<T>( | ||
operation: nonisolated(nonsending) () async throws -> T, | ||
onCancel handler: @Sendable () -> Void | ||
) async rethrows -> T { | ||
// unconditionally add the cancellation record to the task. | ||
// if the task was already cancelled, it will be executed right away. | ||
let record = unsafe _taskAddCancellationHandler(handler: handler) | ||
defer { unsafe _taskRemoveCancellationHandler(record: record) } | ||
|
||
return try await operation() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way we stay on the caller for as long as possible, and the closure is also invoked on it. This improves the way we don't hop away from the caller unnecessarily anymore |
||
|
||
// Note: Deprecated version which would still hop if we did not close over an `isolated` parameter | ||
// with the operation closure. Instead, we should do what the docs of this method promise - and not hop at all, | ||
// by using the new nonisolated(nonsending) | ||
@available(SwiftStdlib 5.1, *) | ||
@_silgen_name("$ss27withTaskCancellationHandler9operation8onCancel9isolationxxyYaKXE_yyYbXEScA_pSgYitYaKlF") | ||
public func _isolatedParam_withTaskCancellationHandler<T>( | ||
operation: () async throws -> T, | ||
onCancel handler: @Sendable () -> Void, | ||
isolation: isolated (any Actor)? = #isolation | ||
|
70 changes: 70 additions & 0 deletions
70
test/Concurrency/Runtime/cancellation_handler_operation_does_not_hop.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// RUN: %target-run-simple-swift( -target %target-swift-5.1-abi-triple %import-libdispatch) | %FileCheck %s | ||
// REQUIRES: concurrency | ||
// REQUIRES: executable_test | ||
|
||
// REQUIRES: concurrency_runtime | ||
// UNSUPPORTED: back_deployment_runtime | ||
// UNSUPPORTED: freestanding | ||
|
||
actor Canceller { | ||
var hello: String = "checking..." | ||
|
||
func testFunc() async { | ||
await withTaskCancellationHandler { | ||
self.assertIsolated("wat in \(#function)!") | ||
print("testFunc.withTaskCancellationHandler") // CHECK: testFunc.withTaskCancellationHandler | ||
self.hello = "done!" | ||
} onCancel: { | ||
// noop | ||
} | ||
|
||
// just a simple check to see we executed the closure | ||
|
||
await globalTestFunc() | ||
} | ||
} | ||
func globalTestFunc(isolation: isolated (any Actor)? = #isolation) async { | ||
isolation!.assertIsolated("wat in \(#function)!") | ||
await withTaskCancellationHandler { | ||
isolation!.assertIsolated("wat in \(#function)!") | ||
print("globalTestFunc.withTaskCancellationHandler") // CHECK: globalTestFunc.withTaskCancellationHandler | ||
} onCancel: { | ||
// noop | ||
} | ||
} | ||
|
||
@MainActor | ||
func testMainActor() async { | ||
MainActor.preconditionIsolated("Expected main actor") | ||
await withTaskCancellationHandler { | ||
MainActor.preconditionIsolated("expected MainActor") | ||
} onCancel: { | ||
// noop | ||
} | ||
} | ||
|
||
// FIXME: rdar://155313349 - nonisolated(nonsending) closure does not pick up isolated parameter when the closure is throwing | ||
func testMainActorIsolated(isolation: isolated (any Actor)? = #isolation) async { | ||
return // FIXME: until rdar://155313349 is fixed | ||
|
||
isolation!.preconditionIsolated("Expected main actor") | ||
MainActor.preconditionIsolated("Expected main actor") | ||
await withTaskCancellationHandler { | ||
print("_unsafeInheritExecutor_withTaskCancellationHandler") | ||
MainActor.preconditionIsolated("expected MainActor") | ||
} onCancel: { | ||
// noop | ||
} | ||
} | ||
|
||
_ = await Canceller().testFunc() | ||
|
||
_ = await Task { @MainActor in | ||
await testMainActor() | ||
}.value | ||
|
||
_ = await Task { @MainActor in | ||
await testMainActorIsolated() | ||
}.value | ||
|
||
print("done") // CHECK: done |
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
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
going with just AEIC those; we keep changing concurrency annotations and those methods are so small that AEIC for them is going to be fine anyway (as most of concurrency API already are like this)