Skip to content

[SerialExecutor] SerialExecutor.checkIsolated() to check its own tracking for isolation checks #71172

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 10 commits into from
Mar 28, 2024

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented Jan 26, 2024

This implements the proposal SE-0424

resolves rdar://112637467

Related thread: https://forums.swift.org/t/dispatchqueue-serialexecutor-isolation-check-is-not-consistent-on-darwin/69389/4

In addition, we need to be more careful when upgrading the executor check to "default mode is crashing", and should only do so in the upcoming release.

This adds a runtime check that we'll replace with checking what version of the SDKs an app was linked against. If it is the release with Swift 6 (future), we'll make it crash by default, but by doing it dynamically like this -- old apps, linked against ols SDKs will be allowed to keep working, until they rebuild with a new SDK and get the new behavior.

The changes related to that semi-undo the flag swap from: 8787846

And prepare the "hook" we'll have to implement once the the releases are announced in the future in Bincompat.cpp.

Resolves rdar://125107864

@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch from d513606 to ce4abce Compare January 26, 2024 06:44
@ktoso ktoso added the concurrency Feature: umbrella label for concurrency language features label Jan 26, 2024
let executor: NaiveQueueExecutor

init() {
self.queue = DispatchSerialQueue(label: "MyQueue")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a similar test for DispatchWorkloop being used as serial executor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can give that a go.

It's a bit unfortunate that I have to keep writing "wrappers" since the conformances don't ship in old enough OS or on Linux, so such "wrapper executor around X" is the best I can do, rather than directly using the queue or workloop...

but I'll give that a go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO: I've not done this bit yet but will follow up on it

@ktoso
Copy link
Contributor Author

ktoso commented Feb 9, 2024

@swift-ci please smoke test

@ktoso ktoso changed the title [SerialExecutor] PoC: Allow a SerialExecutor to check its own tracking for isolation checks [WIP][SerialExecutor] Allow a SerialExecutor to check its own tracking for isolation checks Feb 22, 2024
@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch from f65fafb to b263491 Compare February 22, 2024 09:01
@ktoso ktoso marked this pull request as draft February 22, 2024 09:05
@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch from b263491 to 63cf486 Compare February 22, 2024 09:17
@ktoso
Copy link
Contributor Author

ktoso commented Feb 22, 2024

@swift-ci please build toolchain

return false;
}

if (expectedExecutor.isMainExecutor() && !currentExecutor.isMainExecutor()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here - We need to consider dispatch serial queue targeting the main queue, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looping back to this, sorry to keep you waiting.

Agreed in pricinple, but we should carefully roll out this change -- if we did this now, we would get much worse error messages. The current message includes "expected MainActor executor" which the default implementation that'd SerialDispatchQueue would bene up using would not have.

We need dispatch to implement these APIs with good error messages "you expected main but was something else" I think?

If we think this doesn't matter I could already make this call checkIsolated though, but I think we should stagger the rollout ^

@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch 2 times, most recently from b88f14e to ba6fa6c Compare March 15, 2024 10:20
@ktoso ktoso changed the title [WIP][SerialExecutor] Allow a SerialExecutor to check its own tracking for isolation checks [SerialExecutor] SerialExecutor.checkIsolated() to check its own tracking for isolation checks Mar 15, 2024
@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch 3 times, most recently from 17d1afe to ded2187 Compare March 15, 2024 12:57
@ktoso
Copy link
Contributor Author

ktoso commented Mar 15, 2024

@swift-ci please test

@ktoso ktoso requested a review from PriyaAvhad March 15, 2024 12:59
@ktoso
Copy link
Contributor Author

ktoso commented Mar 15, 2024

@swift-ci please test

@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch from 6a7a8b8 to f86cd1b Compare March 18, 2024 03:54
@ktoso
Copy link
Contributor Author

ktoso commented Mar 18, 2024

Failure was a missing %import-libdispatch in just one test i think

@ktoso ktoso force-pushed the wip-concurrency-assert-dispatch branch from f86cd1b to 84891f8 Compare March 18, 2024 03:56
@ktoso
Copy link
Contributor Author

ktoso commented Mar 18, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Mar 18, 2024

hmm real failure specific to x86 macos

*** Program crashed: Bad pointer dereference at 0x0000000000000000 ***

Thread 0 crashed:

0                  0x0000000000000000
1 [ra] [thunk]     0x00000001088897a1 ActorOnNaiveQueueExecutor.__allocating_init() + 33 in a.out
2 [ra]             0x000000010888a9ca static Main.main() + 202 in a.out
3 [async] [system] 0x000000010888b700 static Main.$main() in a.out
4 [async] [system] 0x000000010888b830 async_MainTQ0_ in a.out
5 [async] [thunk]  0x000000010888b960 thunk for @escaping @convention(thin) @async () -> () in a.out
6 [async] [thunk]  0x000000010888ba70 partial apply for thunk for @escaping @convention(thin) @async () -> () in a.out
7 [async] [system] 0x00000001089baeb0 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) in libswift_Concurrency.dylib at /Users/ec2-user/jenkins/workspace/swift-PR-macos-smoke-test/branch-main/swift/stdlib/public/Concurrency/Task.cpp:471

ktoso added 6 commits March 26, 2024 16:14
Allow serial executors to perform a "last resort" check before crashing
in calls like preconditionIsolated and assumeIsolated.

This allows custom excecutors to use external information to claim that
while the synchronous code is not executing in a Swift Task, it IS
executing on the apropriate thread or queue the code is expecting.

add swift_task_checkIsolatedImpl to CooperativeGlobalExecutor
/// an application was linked to. Since Swift 6 the default is to crash,
/// and the logging behavior is no longer available.
static unsigned unexpectedExecutorLogLevel =
swift_bincompat_useLegacyNonCrashingExecutorChecks()
Copy link
Contributor Author

@ktoso ktoso Mar 27, 2024

Choose a reason for hiding this comment

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

This specifically handles "app compiled against old sdk should still be able to get the warnings"

fyi @xedin

#else
return false; // Always use the new behavior on non-Apple OSes
#endif
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dyld compatibility trickery would go here once we have a release

@ktoso
Copy link
Contributor Author

ktoso commented Mar 27, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Mar 28, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Mar 28, 2024

@swift-ci please smoke test

@ktoso
Copy link
Contributor Author

ktoso commented Mar 28, 2024

@swift-ci please smoke test Linux

@ktoso ktoso merged commit 86f5441 into swiftlang:main Mar 28, 2024
@ktoso ktoso deleted the wip-concurrency-assert-dispatch branch March 28, 2024 22:06
ktoso added a commit to ktoso/swift that referenced this pull request Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants