Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[iOS] Fix deadlock if WXSDKInstance is release in thread safe diction…
Browse files Browse the repository at this point in the history
…ary. (#1757)
  • Loading branch information
wqyfavor authored and wqyfavor committed Nov 20, 2018
1 parent fef4326 commit fabc9ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
13 changes: 2 additions & 11 deletions ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ @implementation WXSDKInstance
BOOL _debugJS;
id<WXBridgeProtocol> _instanceJavaScriptContext; // sandbox javaScript context
CGFloat _defaultPixelScaleFactor;
BOOL _bReleaseInstanceInMainThread;
BOOL _defaultDataRender;
}

Expand Down Expand Up @@ -118,7 +117,6 @@ - (instancetype)init
_apmInstance = [[WXApmForInstance alloc] init];

_defaultPixelScaleFactor = CGFLOAT_MIN;
_bReleaseInstanceInMainThread = YES;
_defaultDataRender = NO;

[self addObservers];
Expand Down Expand Up @@ -443,9 +441,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];
Expand Down Expand Up @@ -659,13 +654,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];
});
}
});
//+}
});

Expand Down
16 changes: 15 additions & 1 deletion ios/sdk/WeexSDK/Sources/Utility/WXThreadSafeMutableDictionary.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit fabc9ae

Please sign in to comment.