cxfeng1 closed pull request #1757: [iOS] Fix deadlock if WXSDKInstance is released in lock area of thread safe diction… URL: https://github.com/apache/incubator-weex/pull/1757
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m index 0a75f5f16c..7c9529f8ff 100644 --- a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m +++ b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m @@ -80,7 +80,6 @@ @implementation WXSDKInstance BOOL _debugJS; id<WXBridgeProtocol> _instanceJavaScriptContext; // sandbox javaScript context CGFloat _defaultPixelScaleFactor; - BOOL _bReleaseInstanceInMainThread; BOOL _defaultDataRender; } @@ -120,7 +119,6 @@ - (instancetype)init _apmInstance = [[WXApmForInstance alloc] init]; _defaultPixelScaleFactor = CGFLOAT_MIN; - _bReleaseInstanceInMainThread = YES; _defaultDataRender = NO; [self addObservers]; @@ -445,9 +443,6 @@ - (BOOL)_handleConfigCenter BOOL useJSCApiForCreateInstance = [[configCenter configForKey:@"iOS_weex_ext_config.useJSCApiForCreateInstance" defaultValue:@(YES) isDefault:NULL] boolValue]; [WXUtility setUseJSCApiForCreateInstance:useJSCApiForCreateInstance]; - - //Reading config from orange for Release instance in Main Thread or not - _bReleaseInstanceInMainThread = [[configCenter configForKey:@"iOS_weex_ext_config.releaseInstanceInMainThread" defaultValue:@(YES) isDefault:nil] boolValue]; BOOL shoudMultiContext = NO; shoudMultiContext = [[configCenter configForKey:@"iOS_weex_ext_config.createInstanceUsingMutliContext" defaultValue:@(YES) isDefault:NULL] boolValue]; @@ -666,13 +661,9 @@ - (void)destroyInstance [WXCoreBridge closePage:instanceId]; // Reading config from orange for Release instance in Main Thread or not, for Bug #15172691 +{ - if (!_bReleaseInstanceInMainThread) { + dispatch_async(dispatch_get_main_queue(), ^{ [WXSDKManager removeInstanceforID:instanceId]; - } else { - dispatch_async(dispatch_get_main_queue(), ^{ - [WXSDKManager removeInstanceforID:instanceId]; - }); - } + }); //+} }); diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m b/ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m index 3c32292269..6f31fb990e 100644 --- a/ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m +++ b/ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m @@ -145,24 +145,30 @@ - (NSEnumerator *)keyEnumerator - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey { + id originalObject = nil; // make sure that object is not released in lock @try { pthread_mutex_lock(&_safeThreadDictionaryMutex); + originalObject = [_dict objectForKey:aKey]; [_dict setObject:anObject forKey:aKey]; } @finally { pthread_mutex_unlock(&_safeThreadDictionaryMutex); } + originalObject = nil; } - (void)setObject:(id)anObject forKeyedSubscript:(id <NSCopying>)key { + id originalObject = nil; // make sure that object is not released in lock @try { pthread_mutex_lock(&_safeThreadDictionaryMutex); + originalObject = [_dict objectForKey:key]; [_dict setObject:anObject forKeyedSubscript:key]; } @finally { pthread_mutex_unlock(&_safeThreadDictionaryMutex); } + originalObject = nil; } - (NSArray *)allKeys @@ -189,24 +195,32 @@ - (NSArray *)allValues - (void)removeObjectForKey:(id)aKey { + id originalObject = nil; // make sure that object is not released in lock @try { pthread_mutex_lock(&_safeThreadDictionaryMutex); - [_dict removeObjectForKey:aKey]; + originalObject = [_dict objectForKey:aKey]; + if (originalObject) { + [_dict removeObjectForKey:aKey]; + } } @finally { pthread_mutex_unlock(&_safeThreadDictionaryMutex); } + originalObject = nil; } - (void)removeAllObjects { + NSArray* allValues = nil; // make sure that objects are not released in lock @try { pthread_mutex_lock(&_safeThreadDictionaryMutex); + allValues = [_dict allValues]; [_dict removeAllObjects]; } @finally { pthread_mutex_unlock(&_safeThreadDictionaryMutex); } + allValues = nil; } - (id)copy ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services