From 40043a175e50f66df7bb6664d0d1fef340b0bd6e Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Thu, 20 Jun 2019 14:51:50 -0700 Subject: [PATCH] Animations: attempt to mitigate crashes in T43628589 Summary: Trying to mitigate animation-related crashes in T43628589. Clues: all the crashes are off the main thread, and most operations in this class happen explicitly in blocks executed on the main thread. I think there's a category of race conditions caused by animations not being allocated yet when this code runs / being deallocated as it's running. We shouldn't need to add locks if everything just runs on the main thread. Reviewed By: PeteTheHeat Differential Revision: D15924310 fbshipit-source-id: d82f5434e53fd394c4a7548d52f59a0f63961779 --- Libraries/NativeAnimation/RCTNativeAnimatedModule.m | 12 ++++++++---- .../NativeAnimation/RCTNativeAnimatedNodesManager.m | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedModule.m b/Libraries/NativeAnimation/RCTNativeAnimatedModule.m index 0bbc9c78711..cbaa41509b8 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedModule.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedModule.m @@ -87,10 +87,14 @@ RCT_EXPORT_METHOD(startAnimatingNode:(nonnull NSNumber *)animationId [self addOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) { [nodesManager startAnimatingNode:animationId nodeTag:nodeTag config:config endCallback:callBack]; }]; - if ([_nodesManager isNodeManagedByFabric:nodeTag]) { - _animIdIsManagedByFabric[animationId] = @YES; - [self flushOperationQueues]; - } + __weak RCTNativeAnimatedModule *weakSelf = self; + RCTExecuteOnMainQueue(^{ + __strong RCTNativeAnimatedModule *strongSelf = weakSelf; + if (strongSelf && [strongSelf->_nodesManager isNodeManagedByFabric:nodeTag]) { + strongSelf->_animIdIsManagedByFabric[animationId] = @YES; + [strongSelf flushOperationQueues]; + } + }); } RCT_EXPORT_METHOD(stopAnimation:(nonnull NSNumber *)animationId) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m index 1a47f1b7abd..9920e8a6ed6 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m @@ -53,7 +53,10 @@ - (BOOL)isNodeManagedByFabric:(nonnull NSNumber *)tag { RCTAnimatedNode *node = _animationNodes[tag]; - return [node isManagedByFabric]; + if (node) { + return [node isManagedByFabric]; + } + return false; } #pragma mark -- Graph