Skip to content

Fix a potential race condition in StdlibUnittest #25136

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ fileprivate struct AtomicBool {
func orAndFetch(_ b: Bool) -> Bool {
return _value.orAndFetch(b ? 1 : 0) != 0
}

func fetchAndClear() -> Bool {
return _value.fetchAndAnd(0) != 0
}
}

func _printStackTrace(_ stackTrace: SourceLocStack?) {
Expand All @@ -139,11 +143,9 @@ public func expectFailure(
stackTrace: SourceLocStack = SourceLocStack(),
showFrame: Bool = true,
file: String = #file, line: UInt = #line, invoking body: () -> Void) {
let startAnyExpectFailed = _anyExpectFailed.load()
_anyExpectFailed.store(false)
let startAnyExpectFailed = _anyExpectFailed.fetchAndClear()
body()
let endAnyExpectFailed = _anyExpectFailed.load()
_anyExpectFailed.store(false)
let endAnyExpectFailed = _anyExpectFailed.fetchAndClear()
expectTrue(
endAnyExpectFailed, "running `body` should produce an expected failure",
stackTrace: stackTrace.pushIf(showFrame, file: file, line: line)
Expand Down