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

Improve Diagnostics for Concurrent Access of Static Properties in Actors #78435

Open
nashysolutions opened this issue Jan 4, 2025 · 4 comments · May be fixed by #78652
Open

Improve Diagnostics for Concurrent Access of Static Properties in Actors #78435

nashysolutions opened this issue Jan 4, 2025 · 4 comments · May be fixed by #78652
Labels
feature A feature request or implementation triage needed This issue needs more specific labels

Comments

@nashysolutions
Copy link

Motivation

Currently, when a static property is declared in an actor and accessed concurrently from multiple tasks, the Swift compiler does not provide any warnings or errors about potential race conditions. This lack of diagnostics can lead to subtle and hard-to-diagnose bugs, especially for developers who assume that all properties within an actor are isolated, including static ones.

In contrast, when a class is used instead of an actor, the compiler correctly identifies and prevents race conditions caused by concurrent access. This inconsistent behaviour between actors and classes can create confusion and lead to incorrect assumptions about the safety of static properties in actors.

Proposed solution

Introduce compiler diagnostics for concurrent access to static properties in actors. Specifically:

  1. Warn developers when a static property in an actor is accessed in a concurrent context.
  2. Provide clear messaging that static properties are not actor-isolated and must be explicitly synchronised if accessed concurrently.
  3. Optionally, recommend alternative approaches.
actor MyActor {
    static var value = 0
}

nonisolated func a() async {
    await withTaskGroup(of: Void.self) { taskGroup in
        for _ in 0..<1000 {
            taskGroup.addTask {
                MyActor.value += 1 // This should trigger a compiler warning about potential race conditions.
            }
        }
    }
    print(MyActor.value)
}

Expected Diagnostic:
Concurrent access to static property 'value' in actor 'MyActor' is not safe and may result in a race condition.

Alternatives considered

  1. Status Quo: Continue without compiler diagnostics. This approach leaves developers vulnerable to race conditions when using static properties in actors without realising the lack of isolation.
  2. Documentation Update: Update the Swift documentation to clarify that static properties in actors are not isolated. While this would improve awareness, it relies on developers reading the documentation and may not be as effective as compiler diagnostics.
  3. Enhanced Actor Model: Extend the actor isolation model to include static properties. However, this would likely introduce significant complexity and break existing code, making it a less feasible option.

Additional information

This feature would align with Swift’s focus on safety and concurrency. By making such race conditions explicit at compile time, it would prevent subtle bugs and reduce confusion for developers adopting the actor model.

This issue was discovered by the developers in this thread: https://forums.swift.org/t/why-does-the-compiler-allow-static-properties-in-actors-without-await/76973

@nashysolutions nashysolutions added feature A feature request or implementation triage needed This issue needs more specific labels labels Jan 4, 2025
@iMostfa
Copy link
Contributor

iMostfa commented Jan 4, 2025

actually i'm surprised that it's possible to define static variables on actors without emitting any errors.

for example:

class MyClass {
   static var value = 0
// ERROR: Static property 'value' is not concurrency-safe because it is nonisolated global shared mutable state
}

actor MyActor {
    static var value = 0
}

@jamieQ
Copy link
Contributor

jamieQ commented Jan 6, 2025

i would characterize this as a bug rather than feature request, since the lack of an error diagnostic for this pattern is a data race safety hole. cc @hborla – assuming you agree with this assessment, could you (or someone with appropriate permissions) adjust the labels when you have a moment please?

@krishpranav
Copy link

Hi, is this still open?

@krishpranav
Copy link

@ maintainers if possible can you assign it to me so that i will raise an PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants