Possible race condition when loading _XCTFailureHandler symbol #72
flockoffiles
started this conversation in
General
Replies: 1 comment 2 replies
-
To get more or less predictable loading, I had to resort to the following helper function: private func loadXCTFailureHandler() -> XCTFailureHandler? {
#if targetEnvironment(simulator)
guard let xcodeDeveloperPath = ProcessInfo.processInfo.environment["SIMULATOR_CAPABILITIES"]?.components(separatedBy: "/Platforms/").first else {
// Cannot get the Xcode Library Developer path.
return nil
}
let xcTestCorePath = "\(xcodeDeveloperPath)/Platforms/iPhoneSimulator.platform/Developer/Library/PrivateFrameworks/XCTestCore.framework/XCTestCore"
guard let handle = dlopen(xcTestCorePath, RTLD_GLOBAL),
let symPtr = dlsym(handle, "_XCTFailureHandler") else {
// Cannot load XCTestCore or _XCTFailureHandler is no longer present.
return nil
}
return unsafeBitCast(symPtr, to: XCTFailureHandler.self)
#else
// Cannot load for device (yet)
return nil
#endif
} I know, it rather fragile, but that's the best I could come up with at the moment. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am experiencing a race condition when the symbol _XCTFailureHandler is being resolved with dlsym.
In most cases it works, but sometimes by the time it tries to resolve, the library that contains the symbol has already been unloaded, and the symbol is not resolved, so my app crashes.
I was wondering whether the stability could be improved, perhaps by trying the load the underlying XCTest library explicitly before calling dlsym?
If not, at least, add a guard for the result of dlsym being nil, and then just doing a runtime warning instead of crashing.
I am on MacOS Sonoma and Xcode 15 (tried with both the AppStore version and Xcode 15.1 beta).
Beta Was this translation helpful? Give feedback.
All reactions