Skip to content
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

Unit tests crash with "Fatal error: Unexpectedly found nil while unwrapping an Optional value" #101

Open
evandelaney opened this issue Mar 22, 2024 · 1 comment

Comments

@evandelaney
Copy link

Unit tests crash in XCTestCase subclasses inside of override func setUp() async throws at the point where .oAuth(from:authenticate:callback:scope:completion:) is called in production code.

The stack trace shows OAuthService.services.getter force unwrapping the global services variable, which is of type ThreadSpecificVariable<OAuthServiceContainer>.

According to the headerdocs for ThreadSpecificVariable.init(value:):

/// Initialize a new ThreadSpecificVariable with value for the calling thread. After calling this, the calling
/// thread will see currentValue == value but on all other threads currentValue will be nil until changed.

I suspect this is a multithreading issue. Maybe the first instance of a test class (to run testFoo) sets up the OAuth route on Thread A, and the next instance of the test class (to run testBar) tries to set up the OAuth route on Thread B.

I was, in fact, able to confirm this with breakpoints in Xcode. Anytime setUp() jumps to a different thread, the test run will crash.

Here is an example minimal project that reproduces the issue.

@0xTim
Copy link
Member

0xTim commented Mar 25, 2024

I believe this is caused by Swift Concurrency and the use of the thread safe wrapper. In normal code, since we're event loop based we don't hit that, but that's not the case for tests.

One option would be to migrate to a NIOLockedValueBox, that should be a quick fix that can be done in a small PR. The real solution is migrate the library to async/await, but that's going to take more time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants