Skip to content

Commit da628e3

Browse files
authored
Merge pull request #71080 from hborla/data-race-safety-changelog
[Release Note] Swift 5.10 closes all known holes in compile-time strict concurrency checking.
2 parents 246da83 + 37f635c commit da628e3

File tree

1 file changed

+110
-20
lines changed

1 file changed

+110
-20
lines changed

CHANGELOG.md

Lines changed: 110 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,142 @@
6161

6262
## Swift 5.10
6363

64+
* Swift 5.10 closes all known static data-race safey holes in complete strict
65+
concurrency checking.
66+
67+
When writing code against `-strict-concurrency=complete`, Swift 5.10 will
68+
diagnose all potential for data races at compile time unless an explicit
69+
unsafe opt out, such as `nonisolated(unsafe)` or `@unchecked Sendable`, is
70+
used.
71+
72+
For example, in Swift 5.9, the following code crashes at runtime due to a
73+
`@MainActor`-isolated initializer being evaluated outside the actor, but it
74+
was not diagnosed under `-strict-concurrency=complete`:
75+
76+
```swift
77+
@MainActor
78+
class MyModel {
79+
init() {
80+
MainActor.assertIsolated()
81+
}
82+
83+
static let shared = MyModel()
84+
}
85+
86+
func useShared() async {
87+
let model = MyModel.shared
88+
}
89+
90+
await useShared()
91+
```
92+
93+
The above code admits data races because a `@MainActor`-isolated static
94+
variable, which evaluates a `@MainActor`-isolated initial value upon first
95+
access, is accessed synchronously from a `nonisolated` context. In Swift
96+
5.10, compiling the code with `-strict-concurrency=complete` produces a
97+
warning that the access must be done asynchronously:
98+
99+
```
100+
warning: expression is 'async' but is not marked with 'await'
101+
let model = MyModel.shared
102+
^~~~~~~~~~~~~~
103+
await
104+
```
105+
106+
Swift 5.10 fixed numerous other bugs in `Sendable` and actor isolation
107+
checking to strengthen the guarantees of complete concurrency checking.
108+
109+
Note that the complete concurrency model in Swift 5.10 is conservative.
110+
Several Swift Evolution proposals are in active development to improve the
111+
usability of strict concurrency checking ahead of Swift 6.
112+
64113
* [SE-0412][]:
65114

66-
Under strict concurrency checking, every global or static variable must be either isolated to a global actor or be both immutable and of `Sendable` type.
115+
Global and static variables are prone to data races because they provide memory that can be accessed from any program context. Strict concurrency checking in Swift 5.10 prevents data races on global and static variables by requiring them to be either:
116+
117+
1. isolated to a global actor, or
118+
2. immutable and of `Sendable` type.
119+
120+
For example:
67121

68122
```swift
69123
var mutableGlobal = 1
70124
// warning: var 'mutableGlobal' is not concurrency-safe because it is non-isolated global shared mutable state
71125
// (unless it is top-level code which implicitly isolates to @MainActor)
72126

73-
final class NonsendableType {
74-
init() {}
127+
@MainActor func mutateGlobalFromMain() {
128+
mutableGlobal += 1
129+
}
130+
131+
nonisolated func mutateGlobalFromNonisolated() async {
132+
mutableGlobal += 10
75133
}
76134

77135
struct S {
78-
static let immutableNonsendable = NonsendableType()
79-
// warning: static property 'immutableNonsendable' is not concurrency-safe because it is not either conforming to 'Sendable' or isolated to a global actor
136+
static let immutableSendable = 10
137+
// okay; 'immutableSendable' is safe to access concurrently because it's immutable and 'Int' is 'Sendable'
80138
}
81139
```
82140

83-
The attribute `nonisolated(unsafe)` can be used to annotate a global variable (or any form of storage) to disable static checking of data isolation, but note that without correct implementation of a synchronization mechanism to achieve data isolation, dynamic run-time analysis from exclusivity enforcement or tools such as Thread Sanitizer could still identify failures.
141+
A new `nonisolated(unsafe)` modifier can be used to annotate a global or static variable to suppress data isolation violations when manual synchronization is provided:
84142

85143
```swift
86-
nonisolated(unsafe) var global: String
144+
// This global is only set in one part of the program
145+
nonisolated(unsafe) var global: String!
87146
```
88147

148+
`nonisolated(unsafe)` can be used on any form of storage, including stored properties and local variables, as a more granular opt out for `Sendable` checking, eliminating the need for `@unchecked Sendable` wrapper types in many use cases:
149+
150+
```swift
151+
import Dispatch
152+
153+
// 'MutableData' is not 'Sendable'
154+
class MutableData { ... }
155+
156+
final class MyModel: Sendable {
157+
private let queue = DispatchQueue(...)
158+
// 'protectedState' is manually isolated by 'queue'
159+
nonisolated(unsafe) private var protectedState: MutableData
160+
}
161+
```
162+
163+
Note that without correct implementation of a synchronization mechanism to achieve data isolation, dynamic run-time analysis from exclusivity enforcement or tools such as the Thread Sanitizer could still identify failures.
164+
89165
* [SE-0411][]:
90166

91-
Default value expressions can now have the same isolation as the enclosing
92-
function or the corresponding stored property:
167+
Swift 5.10 closes a data-race safety hole that previously permitted isolated
168+
default stored property values to be synchronously evaluated from outside the
169+
actor. For example, the following code compiles warning-free under
170+
`-strict-concurrency=complete` in Swift 5.9, but it will crash at runtime at
171+
the call to `MainActor.assertIsolated()`:
93172

94173
```swift
95-
@MainActor
96-
func requiresMainActor() -> Int { ... }
174+
@MainActor func requiresMainActor() -> Int {
175+
MainActor.assertIsolated()
176+
return 0
177+
}
97178

98-
class C {
99-
@MainActor
100-
var x: Int = requiresMainActor()
179+
@MainActor struct S {
180+
var x = requiresMainActor()
181+
var y: Int
182+
}
183+
184+
nonisolated func call() async {
185+
let s = await S(y: 10)
101186
}
102187

103-
@MainActor func defaultArg(value: Int = requiresMainActor()) { ... }
188+
await call()
104189
```
105190

106-
For isolated default values of stored properties, the implicit initialization
107-
only happens in the body of an `init` with the same isolation. This closes
108-
an important data-race safety hole where global-actor-isolated default values
109-
could inadvertently run synchronously from outside the actor.
191+
This happens because `requiresMainActor()` is used as a default argument to
192+
the member-wise initializer of `S`, but default arguments are always
193+
evaluated in the caller. In this case, the caller runs on the generic
194+
executor, so the default argument evaluation crashes.
195+
196+
Under `-strict-concurrency=complete` in Swift 5.10, default argument values
197+
can safely share the same isolation as the enclosing function or stored
198+
property. The above code is still valid, but the isolated default argument is
199+
guaranteed to be evaluated in the callee's isolation domain.
110200

111201
## Swift 5.9.2
112202

@@ -9998,4 +10088,4 @@ using the `.dynamicType` member to retrieve the type of an expression should mig
999810088
[#57225]: <https://github.com/apple/swift/issues/57225>
999910089
[#56139]: <https://github.com/apple/swift/issues/56139>
1000010090
[#70065]: <https://github.com/apple/swift/pull/70065>
10001-
[swift-syntax]: https://github.com/apple/swift-syntax
10091+
[swift-syntax]: https://github.com/apple/swift-syntax

0 commit comments

Comments
 (0)