Skip to content
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

Adopt Swift 6.0 #isolation; Resolves async closure behavior in withSpan #148

Merged
merged 3 commits into from
Jul 19, 2024

Conversation

ktoso
Copy link
Member

@ktoso ktoso commented Jul 18, 2024

Motivation:

This has been a problem for a long time, where we could not safely non sendable actor state from withSpan because the closure would "hop off". Now with #isolation we can handle it properly in Swift 6.0

Modifications:
Adopt #isolation parameter in all with... APIs with async closures.

Result:

Usages like this will not warn nor produce errors anymore:

actor Foo {
    var bar = 0

    func foo() async {
        var num = 0
         await withSpan(#function) { _ in
            // await self.withSpanWorkaround(#function) { _ in
            self.bar += 1
            await work()
            num += 1
        }
    }
}

resolves rdar://113848512

**Motivation:**

This has been a problem for a long time, where we could not safely
non sendable actor state from withSpan because the closure would "hop
off". Now with #isolation we can handle it properly in Swift 6.0

**Modifications:**
Adopt #isolation parameter in all with... APIs with async closures.

**Result:**

Usages like this will not warn nor produce errors anymore:

```
actor Foo {
    var bar = 0

    func foo() async {
        var num = 0
         await withSpan(#function) { _ in
            // await self.withSpanWorkaround(#function) { _ in
            self.bar += 1
            await work()
            num += 1
        }
    }
}
```
Copy link

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

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

The doc comments could do with a look as they won't be consistent depending on your Swift version

@@ -315,18 +315,21 @@ public func withSpan<T>(
/// - instant: the time instant at which the span started
/// - context: The `ServiceContext` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
Copy link

Choose a reason for hiding this comment

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

This doc comment is wrong, should be ofKind to match the public API

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you sure? https://www.swift.org/documentation/docc/writing-symbol-documentation-in-your-source-files shows to document the actual second name, as those are unique and the prior labels aren't.

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) // for TaskLocal ServiceContext
public func withSpan<T, Instant: TracerInstant>(
_ operationName: String,
at instant: @autoclosure () -> Instant,
context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
ofKind kind: SpanKind = .internal,
isolation: isolated (any Actor)? = #isolation,
Copy link

Choose a reason for hiding this comment

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

This parameter isn't documented now right?

Copy link
Member Author

Choose a reason for hiding this comment

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

hm?

this adds docs above here: /// - isolation: Defaulted parameter for inheriting isolation of calling actor

@ktoso
Copy link
Member Author

ktoso commented Jul 19, 2024

I kept "old" API around but made it deprecated and disfavored in 6.0+.

I'll merge but if anyone has any concerns let me know and we'll follow up

@ktoso ktoso enabled auto-merge (squash) July 19, 2024 05:59
@ktoso ktoso disabled auto-merge July 19, 2024 05:59
@ktoso ktoso merged commit 876d711 into main Jul 19, 2024
6 checks passed
@ktoso ktoso deleted the wip.6 branch July 19, 2024 05:59
Copy link
Contributor

@porglezomp porglezomp left a comment

Choose a reason for hiding this comment

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

Post-merge review, I tried this out on the main branch and ran into lots of warnings.

/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - operation: The operation that this span should be measuring
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
#if swift(>=6.0)
Copy link
Contributor

@porglezomp porglezomp Aug 5, 2024

Choose a reason for hiding this comment

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

Is #if swift(>=6.0) correct here? Should this use #if compiler(>=6.0) instead, if a Swift 6 compiler in 5.x mode understands #isolation? I just tried to adopt this in pre-6 code with a 6.0 compiler and it was using the previous overload.

#endif

@_disfavoredOverload
@available(*, deprecated, message: "Prefer #isolation version of this API")
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the deprecated attribute be applied here when the #isolation API isn't available? Should this be in the same #if as above?

@ktoso
Copy link
Member Author

ktoso commented Aug 6, 2024

Thanks @porglezomp lemme check this

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.

None yet

3 participants