You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by cx...@apache.org on 2018/11/14 07:49:12 UTC

[incubator-weex] branch master updated: [iOS] Fix deadlock if WXSDKInstance is release in thread safe dictionary. (#1757)

This is an automated email from the ASF dual-hosted git repository.

cxfeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-weex.git


The following commit(s) were added to refs/heads/master by this push:
     new d25ed7a  [iOS] Fix deadlock if WXSDKInstance is release in thread safe dictionary. (#1757)
d25ed7a is described below

commit d25ed7af3b3527310336d0d2308e7dec8a5fca12
Author: wqyfavor <wq...@163.com>
AuthorDate: Wed Nov 14 15:49:08 2018 +0800

    [iOS] Fix deadlock if WXSDKInstance is release in thread safe dictionary. (#1757)
---
 ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m            | 13 ++-----------
 .../Sources/Utility/WXThreadSafeMutableDictionary.m      | 16 +++++++++++++++-
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
index 0a75f5f..7c9529f 100644
--- a/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
+++ b/ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
@@ -80,7 +80,6 @@ typedef enum : NSUInteger {
     BOOL _debugJS;
     id<WXBridgeProtocol> _instanceJavaScriptContext; // sandbox javaScript context    
     CGFloat _defaultPixelScaleFactor;
-    BOOL _bReleaseInstanceInMainThread;
     BOOL _defaultDataRender;
 }
 
@@ -120,7 +119,6 @@ typedef enum : NSUInteger {
         _apmInstance = [[WXApmForInstance alloc] init];
         
         _defaultPixelScaleFactor = CGFLOAT_MIN;
-        _bReleaseInstanceInMainThread = YES;
         _defaultDataRender = NO;
         
         [self addObservers];
@@ -445,9 +443,6 @@ typedef enum : NSUInteger {
         
         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 @@ typedef enum : NSUInteger {
         [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 3c32292..6f31fb9 100644
--- a/ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m
+++ b/ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m
@@ -145,24 +145,30 @@
 
 - (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 @@
 
 - (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