Skip to content

Commit d543dd9

Browse files
authored
Merge pull request #25136 from porglezomp-misc/fix/stdlib-unittest-race
Fix a potential race condition in StdlibUnittest
2 parents 35e4391 + e841425 commit d543dd9

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ fileprivate struct AtomicBool {
117117
func orAndFetch(_ b: Bool) -> Bool {
118118
return _value.orAndFetch(b ? 1 : 0) != 0
119119
}
120+
121+
func fetchAndClear() -> Bool {
122+
return _value.fetchAndAnd(0) != 0
123+
}
120124
}
121125

122126
func _printStackTrace(_ stackTrace: SourceLocStack?) {
@@ -139,11 +143,9 @@ public func expectFailure(
139143
stackTrace: SourceLocStack = SourceLocStack(),
140144
showFrame: Bool = true,
141145
file: String = #file, line: UInt = #line, invoking body: () -> Void) {
142-
let startAnyExpectFailed = _anyExpectFailed.load()
143-
_anyExpectFailed.store(false)
146+
let startAnyExpectFailed = _anyExpectFailed.fetchAndClear()
144147
body()
145-
let endAnyExpectFailed = _anyExpectFailed.load()
146-
_anyExpectFailed.store(false)
148+
let endAnyExpectFailed = _anyExpectFailed.fetchAndClear()
147149
expectTrue(
148150
endAnyExpectFailed, "running `body` should produce an expected failure",
149151
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)

0 commit comments

Comments
 (0)