-
Notifications
You must be signed in to change notification settings - Fork 604
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
swift 6.0.1 crash when running tests swift testing with @MainActor annotation only on linux #919
Comments
@doozMen Thanks for the report. We're running SnapshotTesting on Linux for pointfree.co, but are on a particular branch that uses this logic to dispatch to the main queue instead: swift-snapshot-testing/Sources/SnapshotTesting/AssertSnapshot.swift Lines 352 to 359 in 7de6fa5
Would you be open to exploring if this fix would address the problem for you when applied to the |
This issue is also affecting me in a very similar way. My project is using swift 6, and swift-testing exclusively. All my suites are marked as
However, when I apply the following diff (to diff --git a/Sources/SnapshotTesting/AssertSnapshot.swift b/Sources/SnapshotTesting/AssertSnapshot.swift
index 63be5bf..6e3ec58 100644
--- a/Sources/SnapshotTesting/AssertSnapshot.swift
+++ b/Sources/SnapshotTesting/AssertSnapshot.swift
@@ -516,18 +516,33 @@ func sanitizePathComponent(_ string: String) -> String {
}
#endif
+ extension DispatchQueue {
+ private static let key = DispatchSpecificKey<UInt8>()
+ private static let value: UInt8 = 0
+
+ fileprivate static func mainSync<R>(execute block: () -> R) -> R {
+ Self.main.setSpecific(key: key, value: value)
+ if getSpecific(key: key) == value {
+ return block()
+ } else {
+ return main.sync(execute: block)
+ }
+ }
+}
+
+
// We need to clean counter between tests executions in order to support test-iterations.
private class CleanCounterBetweenTestCases: NSObject, XCTestObservation {
private static var registered = false
static func registerIfNeeded() {
- if Thread.isMainThread {
- doRegisterIfNeeded()
- } else {
- DispatchQueue.main.sync {
- doRegisterIfNeeded()
- }
- }
+ DispatchQueue.mainSync {
+ if !registered {
+ registered = true
+ XCTestObservationCenter.shared.addTestObserver(CleanCounterBetweenTestCases())
+ }
+ }
+
}
private static func doRegisterIfNeeded() { |
Describe the bug
Only happens on linux
You will see it crashes. The docs state that
Thread.isMainThread
is not available. Weirdly it is on macOS just not on linux.To Reproduce
run the test project on linux and maybe add code
This will work on mac and not on linux
Expected behavior
Not creating deadlock because of doing dispach on main in sync from main
Screenshots

Environment
Additional context
Maybe also a bug in swift but not sure as when you the check is unavailable in async context according to the docs. So you should not rely on it?
Maybe have a asser option that is marked with @mainactor might be better?
The text was updated successfully, but these errors were encountered: