Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTBundleManager.h>
#import <React/RCTJavaScriptLoader.h>
#import <React/RCTSurfacePresenter.h>
#import <ReactCommon/RCTHermesInstance.h>
#import <ReactCommon/RCTInstance.h>
#import <ReactCommon/RCTTurboModule.h>
Expand Down Expand Up @@ -241,4 +242,30 @@ - (void)testBundleLoadAwaitsMainQueueModuleSetup
@"FakeEagerModule -init must have fully returned before the JS bundle finishes loading");
}

- (void)testInvalidateDoesNotDeadlockWhenSuspendCallsIntoJS
{
OCMStub([_mockDelegate unstableModulesRequiringMainQueueSetup]).andReturn(@[]);

RCTInstance *instance = [self makeInstance];
__weak RCTInstance *weakInstance = instance;

// Stands in for a surface observer that calls into JS while suspend stops surfaces.
id presenterMock = OCMPartialMock(instance.surfacePresenter);
OCMStub([(RCTSurfacePresenter *)presenterMock suspend]).andDo(^(NSInvocation *invocation) {
[weakInstance callFunctionOnJSModule:@"RCTLog" method:@"logIfNoNativeHook" args:@[]];
BOOL result = YES;
[invocation setReturnValue:&result];
});

// Invalidate off-main so a deadlock fails the wait instead of hanging the test runner.
XCTestExpectation *invalidated = [self expectationWithDescription:@"invalidate returned"];
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
[instance invalidate];
[invalidated fulfill];
});
[self waitForExpectations:@[ invalidated ] timeout:5.0];

[presenterMock stopMocking];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,15 @@ - (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method

- (void)invalidate
{
std::lock_guard<std::mutex> lock(_invalidationMutex);
_valid = false;
if (_reactInstance) {
_reactInstance->unregisterFromInspector();
{
std::lock_guard<std::mutex> lock(_invalidationMutex);
_valid = false;
if (_reactInstance) {
_reactInstance->unregisterFromInspector();
}
}
// suspend shouldn't be called under _invalidationMutex: it notifies surface observers,
// which can call callFunctionOnJSModule and deadlock on the same mutex.
[_surfacePresenter suspend];
[_jsThreadManager dispatchToJSThread:^{
/**
Expand Down
Loading