-
Couldn't load subscription status.
- Fork 10.6k
[Concurrency] Prevent task re-enqueues in continuation APIs #84944
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
6
commits into
swiftlang:main
Choose a base branch
from
ktoso:wip-withContinuation-nonisolated-nonsending
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13c453b
[Concurrency] Prevent task re-enqueues in continuation APIs
ktoso 0c58cc4
workaround for @abi issue in test, use silgen_name again
ktoso c7ffe99
disable verifyUSRToDeclReconstruction on @abi for now
ktoso 58bafba
Revert "workaround for @abi issue in test, use silgen_name again"
ktoso 13cc716
add github issue to fixme
ktoso f2eb567
test fixup, also handle @abi issue in SwiftDocSupport until issue
ktoso 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
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 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 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
160 changes: 160 additions & 0 deletions
160
test/Concurrency/Runtime/must_not_hop_withContinuation.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,160 @@ | ||
| // RUN: %target-run-simple-swift( -target %target-swift-5.1-abi-triple %import-libdispatch) | %FileCheck %s | ||
| // RUN: %target-run-simple-swift( -O -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 | ||
| // REQUIRES: libdispatch | ||
| // REQUIRES: synchronization | ||
|
|
||
| import Synchronization | ||
|
|
||
|
|
||
| if #available(SwiftStdlib 6.0, *) { | ||
| print("=== foo() async") | ||
| print("---------------------------------------") | ||
| await foo() | ||
| } | ||
|
|
||
| // CHECK: === foo() async | ||
| // CHECK-NEXT: --------------------------------------- | ||
| // We hop to the task executor: | ||
| // CHECK-NEXT: [executor][task-executor] Enqueue (1) | ||
|
|
||
| // CHECK-NEXT: foo - withTaskExecutorPreference | ||
|
|
||
| // CHECK: foo - withTaskExecutorPreference - withCheckedContinuation | ||
| // CHECK-NEXT: foo - withTaskExecutorPreference - withCheckedContinuation done | ||
|
|
||
| // CHECK: foo - withTaskExecutorPreference - withUnsafeContinuation | ||
| // CHECK-NEXT: foo - withTaskExecutorPreference - withUnsafeContinuation done | ||
|
|
||
| // CHECK: foo - withTaskExecutorPreference - withCheckedThrowingContinuation | ||
| // CHECK-NEXT: foo - withTaskExecutorPreference - withCheckedThrowingContinuation done | ||
|
|
||
| // CHECK: foo - withTaskExecutorPreference - withUnsafeThrowingContinuation | ||
| // CHECK-NEXT: foo - withTaskExecutorPreference - withUnsafeThrowingContinuation done | ||
|
|
||
| // By checking that this is the second enqueue here, | ||
| // we check that there was no stray enqueues between with... invocations: | ||
| // CHECK-NEXT: [executor][task-executor] Enqueue (2) | ||
|
|
||
| // CHECK-NEXT: foo - withTaskExecutorPreference done | ||
|
|
||
| // CHECK-NEXT: == Make: actor Foo | ||
| // CHECK-NEXT: --------------------------------------- | ||
| // CHECK-NEXT: [executor][actor-executor] Enqueue (1) | ||
| // CHECK-NEXT: actor.foo | ||
|
|
||
| // CHECK: actor.foo - withCheckedContinuation | ||
| // CHECK-NEXT: actor.foo - withCheckedContinuation done | ||
|
|
||
| // CHECK: actor.foo - withUnsafeContinuation | ||
| // CHECK-NEXT: actor.foo - withUnsafeContinuation done | ||
|
|
||
| // CHECK: actor.foo - withCheckedThrowingContinuation | ||
| // CHECK-NEXT: actor.foo - withCheckedThrowingContinuation done | ||
|
|
||
| // CHECK: actor.foo - withUnsafeThrowingContinuation | ||
| // CHECK-NEXT: actor.foo - withUnsafeThrowingContinuation done | ||
| // CHECK-NEXT: actor.foo done | ||
|
|
||
| // No more enqueues are expected afterwards | ||
| // CHECK-NOT: [executor] | ||
|
|
||
| @available(SwiftStdlib 6.0, *) | ||
| @concurrent | ||
| func foo() async { | ||
| await withTaskExecutorPreference(EnqueueCountExecutor(name: "task-executor")) { | ||
| print("foo - withTaskExecutorPreference") | ||
| await withCheckedContinuation { cont in | ||
| print("foo - withTaskExecutorPreference - withCheckedContinuation") | ||
| cont.resume() | ||
| } | ||
| print("foo - withTaskExecutorPreference - withCheckedContinuation done") | ||
|
|
||
| await withUnsafeContinuation { cont in | ||
| print("foo - withTaskExecutorPreference - withUnsafeContinuation") | ||
| cont.resume() | ||
| } | ||
| print("foo - withTaskExecutorPreference - withUnsafeContinuation done") | ||
|
|
||
| try! await withCheckedThrowingContinuation { cont in | ||
| print("foo - withTaskExecutorPreference - withCheckedThrowingContinuation") | ||
| cont.resume() | ||
| } | ||
| print("foo - withTaskExecutorPreference - withCheckedThrowingContinuation done") | ||
|
|
||
| try! await withUnsafeThrowingContinuation { cont in | ||
| print("foo - withTaskExecutorPreference - withUnsafeThrowingContinuation") | ||
| cont.resume() | ||
| } | ||
| print("foo - withTaskExecutorPreference - withUnsafeThrowingContinuation done") | ||
| } | ||
| print("foo - withTaskExecutorPreference done") | ||
|
|
||
| print("== Make: actor Foo") | ||
| print("---------------------------------------") | ||
| await Foo().foo() | ||
| } | ||
|
|
||
| @available(SwiftStdlib 6.0, *) | ||
| actor Foo { | ||
| let exec = EnqueueCountExecutor(name: "actor-executor") | ||
|
|
||
| nonisolated var unownedExecutor: UnownedSerialExecutor { | ||
| self.exec.asUnownedSerialExecutor() | ||
| } | ||
|
|
||
| func foo() async { | ||
| print("actor.foo") | ||
|
|
||
| await withCheckedContinuation { cont in | ||
| print("actor.foo - withCheckedContinuation") | ||
| cont.resume() | ||
| } | ||
| print("actor.foo - withCheckedContinuation done") | ||
|
|
||
| await withUnsafeContinuation { cont in | ||
| print("actor.foo - withUnsafeContinuation") | ||
| cont.resume() | ||
| } | ||
| print("actor.foo - withUnsafeContinuation done") | ||
|
|
||
| try! await withCheckedThrowingContinuation { cont in | ||
| print("actor.foo - withCheckedThrowingContinuation") | ||
| cont.resume() | ||
| } | ||
| print("actor.foo - withCheckedThrowingContinuation done") | ||
|
|
||
| try! await withUnsafeThrowingContinuation { cont in | ||
| print("actor.foo - withUnsafeThrowingContinuation") | ||
| cont.resume() | ||
| } | ||
| print("actor.foo - withUnsafeThrowingContinuation done") | ||
|
|
||
| print("actor.foo done") | ||
| } | ||
| } | ||
|
|
||
| @available(SwiftStdlib 6.0, *) | ||
| final class EnqueueCountExecutor: TaskExecutor, SerialExecutor { | ||
| let enqueueCount: Atomic<Int> | ||
|
|
||
| let name: String | ||
|
|
||
| init(name: String) { | ||
| self.enqueueCount = .init(0) | ||
| self.name = name | ||
| } | ||
|
|
||
| public func enqueue(_ job: consuming ExecutorJob) { | ||
| let newEnqueueValue = self.enqueueCount.add(1, ordering: .relaxed).newValue | ||
| print("[executor][\(self.name)] Enqueue (\(newEnqueueValue))") | ||
| job.runSynchronously(on: self.asUnownedSerialExecutor()) | ||
| } | ||
| } | ||
|
|
||
| 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 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
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.
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.
I hope we can get away with this "technically" source break... Let's see what the source compat suite says.