Skip to content

[Concurrency] Make initial executor construction fully thread safe. #80896

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 1 commit into from
Apr 22, 2025

Conversation

al45tair
Copy link
Contributor

I had originally thought that we'd get away without this because the executor construction generally happens in the async main function, but of course if a program doesn't already use Swift Concurrency, and someone uses it from a plug-in or library it loads, there's a risk that we might have a race condition.

rdar://149346132

I had originally thought that we'd get away without this because the
executor construction generally happens in the async `main` function,
but of course if a program doesn't already use Swift Concurrency,
and someone uses it from a plug-in or library it loads, there's a
risk that we might have a race condition.

rdar://149346132
@al45tair al45tair requested a review from ktoso as a code owner April 17, 2025 20:50
@al45tair
Copy link
Contributor Author

@swift-ci Please test

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Makes sense, lgtm

if _defaultExecutor == nil {
_defaultExecutor = DefaultExecutorFactory.defaultExecutor
}
// It would be good if there was a Swift way to do this
Copy link
Contributor

@beccadax beccadax Apr 17, 2025

Choose a reason for hiding this comment

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

Wouldn't something like this get thread-safe lazy initialization? I believe we do that for static lets.

public static let defaultExector: any TaskExecutor = DefaultExecutorFactory.defaultExecutor

edit: come to think of it this might need to be moved into a backing global, but the point stands.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a bit more complicated than it looks :-)

Yes, initialising a static variable does happen in a thread-safe manner, but we don't actually want to initialise these to the default executor (since that means constructing the default executor always, even when we don't actually want it, and the act of constructing it might have side effects).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we could leverage static variable initialisation, but I think to do so we'd need to add an extra variable and make the static initialisation be a closure call. I'm not sure that's really an improvement overall. What would be an improvement is the ability to do a once explicitly and directly in Swift, since that's what we really want here.

@al45tair
Copy link
Contributor Author

@swift-ci Please test macOS platform

@al45tair al45tair merged commit 77eb4f0 into swiftlang:main Apr 22, 2025
5 checks passed
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.

3 participants