You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Warn developers when a static property in an actor is accessed in a concurrent context.
Provide clear messaging that static properties are not actor-isolated and must be explicitly synchronised if accessed concurrently.
Optionally, recommend alternative approaches.
actorMyActor{staticvarvalue=0}nonisolatedfunc a()async{awaitwithTaskGroup(of:Void.self){ taskGroup infor_in0..<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
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.
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.
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.
actually i'm surprised that it's possible to define static variables on actors without emitting any errors.
for example:
classMyClass{staticvarvalue=0
// ERROR: Static property 'value' is not concurrency-safe because it is nonisolated global shared mutable state
}actorMyActor{staticvarvalue=0}
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?
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:
Expected Diagnostic:
Concurrent access to static property 'value' in actor 'MyActor' is not safe and may result in a race condition.
Alternatives considered
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
The text was updated successfully, but these errors were encountered: