From 99c7de2600d81325fcb6124d6fd9359e52bbd2ef Mon Sep 17 00:00:00 2001 From: Tadeu Zagallo Date: Wed, 30 Mar 2016 08:11:04 -0700 Subject: [PATCH] Stop the runloop from invalidate instead of dealloc Summary:We were calling `CFRunLoopStop` from `-dealloc` in the `JSCExecutor`, but dealloc is not guaranteed to run in the same thread. Move it to `-invalidate` instead. Reviewed By: javache Differential Revision: D3092645 fb-gh-sync-id: 94b51fec4a9fe0784feeb83d1b0c41de1cd7c052 fbshipit-source-id: 94b51fec4a9fe0784feeb83d1b0c41de1cd7c052 --- React/Executors/RCTJSCExecutor.m | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/React/Executors/RCTJSCExecutor.m b/React/Executors/RCTJSCExecutor.m index 720990dbb..4fa6b60a2 100644 --- a/React/Executors/RCTJSCExecutor.m +++ b/React/Executors/RCTJSCExecutor.m @@ -44,19 +44,23 @@ typedef struct ModuleData { @property (nonatomic, strong, readonly) JSContext *context; @property (nonatomic, assign, readonly) JSGlobalContextRef ctx; -- (instancetype)initWithJSContext:(JSContext *)context NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithJSContext:(JSContext *)context + onThread:(NSThread *)javaScriptThread NS_DESIGNATED_INITIALIZER; @end @implementation RCTJavaScriptContext { RCTJavaScriptContext *_selfReference; + NSThread *_javaScriptThread; } - (instancetype)initWithJSContext:(JSContext *)context + onThread:(NSThread *)javaScriptThread { if ((self = [super init])) { _context = context; + _javaScriptThread = javaScriptThread; /** * Explicitly introduce a retain cycle here - The RCTJSCExecutor might @@ -85,14 +89,14 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init) - (void)invalidate { if (self.isValid) { + RCTAssertThread(_javaScriptThread, @"Must be invalidated on JS thread."); + _context = nil; _selfReference = nil; - } -} + _javaScriptThread = nil; -- (void)dealloc -{ - CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]); + CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]); + } } @end @@ -213,7 +217,7 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context) if (!_context) { JSContext *context = [JSContext new]; - _context = [[RCTJavaScriptContext alloc] initWithJSContext:context]; + _context = [[RCTJavaScriptContext alloc] initWithJSContext:context onThread:_javaScriptThread]; } return _context;