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

fix(ios): potential thread race in HippyJSExecutor #3915

Merged
merged 1 commit into from
Jun 18, 2024
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
37 changes: 25 additions & 12 deletions framework/ios/base/executors/HippyJSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@
@interface HippyJSExecutor () {
// Set at setUp time:
id<HippyContextWrapper> _contextWrapper;
NSMutableArray<dispatch_block_t> *_pendingCalls;
__weak HippyBridge *_bridge;
#ifdef JS_JSC
BOOL _isInspectable;
#endif //JS_JSC
}

@property(readwrite, assign) BOOL ready;
/// Whether JSExecutor has done setup.
@property (nonatomic, assign) BOOL ready;
/// Pending blocks to be executed on JS queue.
@property (nonatomic, strong) NSMutableArray<dispatch_block_t> *pendingCalls;;

@end

Expand Down Expand Up @@ -211,12 +213,19 @@ - (void)setup {
strongSelf.contextCreatedBlock(strongSelf->_contextWrapper);
}
scope->SyncInitialize();
strongSelf.ready = YES;
NSArray<dispatch_block_t> *pendingCalls = [strongSelf->_pendingCalls copy];

// execute pending blocks
NSArray<dispatch_block_t> *pendingCalls;
@synchronized (strongSelf) {
strongSelf.ready = YES;
pendingCalls = [strongSelf.pendingCalls copy];
[strongSelf.pendingCalls removeAllObjects];
}
[pendingCalls enumerateObjectsUsingBlock:^(dispatch_block_t _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[strongSelf executeBlockOnJavaScriptQueue:obj];
}];
[strongSelf->_pendingCalls removeAllObjects];

// performance record
auto entry = scope->GetPerformance()->PerformanceNavigation(hippy::kPerfNavigationHippyInit);
entry->SetHippyJsEngineInitStart(startPoint);
entry->SetHippyJsEngineInitEnd(footstone::TimePoint::SystemNow());
Expand Down Expand Up @@ -245,7 +254,7 @@ - (instancetype)initWithEngineKey:(NSString *)engineKey bridge:(HippyBridge *)br
self.bridge = bridge;

self.ready = NO;
_pendingCalls = [NSMutableArray arrayWithCapacity:4];
self.pendingCalls = [NSMutableArray array];
HippyLogInfo(@"[Hippy_OC_Log][Life_Circle],HippyJSCExecutor Init %p, engineKey:%@", self, engineKey);
}

Expand Down Expand Up @@ -595,9 +604,11 @@ static id executeApplicationScript(NSData *script, NSURL *sourceURL, SharedCtxPt
}

- (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block {
if (!self.ready) {
[_pendingCalls addObject:block];
return;
@synchronized (self) {
if (!self.ready) {
[self.pendingCalls addObject:block];
return;
}
}
auto engine = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.enginekey]->GetEngine();
if (engine) {
Expand All @@ -611,9 +622,11 @@ - (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block {
}

- (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block {
if (!self.ready) {
[_pendingCalls addObject:block];
return;
@synchronized (self) {
if (!self.ready) {
[self.pendingCalls addObject:block];
return;
}
}
auto engine = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.enginekey]->GetEngine();
if (engine) {
Expand Down
Loading