Skip to content

Commit

Permalink
fix(ios): thread race of bundle load op in very small scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Oct 12, 2024
1 parent 26a9d93 commit 543c5b4
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ @interface HippyBridge() {
/// Bundle fetch operation queue (concurrent)
@property (nonatomic, strong) NSOperationQueue *bundleQueue;
/// Record the last execute operation for adding execution dependency.
@property (atomic, strong, nullable) NSOperation *lastExecuteOperation;
@property (nonatomic, strong, nullable) NSOperation *lastExecuteOperation;

/// Cached Dimensions info,will be passed to JS Side.
@property (atomic, strong) NSDictionary *cachedDimensionsInfo;
Expand Down Expand Up @@ -591,13 +591,17 @@ - (void)beginLoadingBundle:(NSURL *)bundleURL
strongSelf.valid, script];
HippyLogError(@"%@", errMsg);
completion(bundleURL, HippyErrorWithMessage(errMsg));
strongSelf.lastExecuteOperation = nil;
@synchronized (self) {
strongSelf.lastExecuteOperation = nil;
}
return;
}
[strongSelf executeJSCode:script sourceURL:bundleURL onCompletion:^(id result, NSError *error) {
HippyLogInfo(@"End executing bundle(%s)",
HP_CSTR_NOT_NULL(bundleURL.absoluteString.lastPathComponent.UTF8String));
strongSelf.lastExecuteOperation = nil;
@synchronized (self) {
strongSelf.lastExecuteOperation = nil;
}
if (completion) {
completion(bundleURL, error);
}
Expand All @@ -624,13 +628,18 @@ - (void)beginLoadingBundle:(NSURL *)bundleURL
// Add dependency, make sure that doing fetch before execute,
// and all execution operations must be queued.
[executeOperation addDependency:fetchOperation];
if (self.lastExecuteOperation) {
[executeOperation addDependency:self.lastExecuteOperation];
@synchronized (self) {
NSOperation *lastOp = self.lastExecuteOperation;
if (lastOp) {
[executeOperation addDependency:lastOp];
}
}

// Enqueue operation
[_bundleQueue addOperations:@[fetchOperation, executeOperation] waitUntilFinished:NO];
self.lastExecuteOperation = executeOperation;
@synchronized (self) {
self.lastExecuteOperation = executeOperation;
}
}

- (void)unloadInstanceForRootView:(NSNumber *)rootTag {
Expand Down

0 comments on commit 543c5b4

Please sign in to comment.