Skip to content

Commit

Permalink
fix(ios): remove busy loop and change waitForMemoryPanicCleared to no…
Browse files Browse the repository at this point in the history
…op since it's always on main thread

Was previously used in kroll thread entry points to wait for main thread to handle memory warnings.
Now it will just result in an infinite sleep loop because JS runs on main thread.

Fixes TIMOB-27080
  • Loading branch information
sgtcoolguy committed Jun 17, 2019
1 parent 9b8617e commit a12da5b
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 32 deletions.
6 changes: 2 additions & 4 deletions iphone/TestModule/TitaniumKit.framework/Headers/TiApp.h
Expand Up @@ -13,13 +13,11 @@
#import "TiRootViewController.h"
#import "XHRBridge.h"

extern BOOL applicationInMemoryPanic;
extern BOOL applicationInMemoryPanic; // TODO: Remove in SDK 9.0+

// TODO: Remove in SDK 9.0+
TI_INLINE void waitForMemoryPanicCleared() //WARNING: This must never be run on main thread, or else there is a risk of deadlock!
{
while (applicationInMemoryPanic) {
[NSThread sleepForTimeInterval:0.01];
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/KrollBridge.m
Expand Up @@ -294,11 +294,11 @@ - (void)didReceiveMemoryWarning:(NSNotification *)notification
signed long proxiesCount = CFDictionaryGetCount(registeredProxies);
OSSpinLockUnlock(&proxyLock);

//During a memory panic, we may not get the chance to copy proxies.
// During a memory panic, we may not get the chance to copy proxies.
while (keepWarning) {
keepWarning = NO;

for (id proxy in (NSDictionary *)registeredProxies) {
for (id proxy in [(NSDictionary *)registeredProxies allKeys]) {
[proxy didReceiveMemoryWarning:notification];

OSSpinLockLock(&proxyLock);
Expand Down
6 changes: 2 additions & 4 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.h
Expand Up @@ -13,13 +13,11 @@
#import "TiRootViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

extern BOOL applicationInMemoryPanic;
extern BOOL applicationInMemoryPanic; // TODO: Remove in SDK 9.0+

// TODO: Remove in SDK 9.0+
TI_INLINE void waitForMemoryPanicCleared() //WARNING: This must never be run on main thread, or else there is a risk of deadlock!
{
while (applicationInMemoryPanic) {
[NSThread sleepForTimeInterval:0.01];
}
}

/**
Expand Down
15 changes: 2 additions & 13 deletions iphone/TitaniumKit/TitaniumKit/Sources/API/TiApp.m
Expand Up @@ -24,17 +24,16 @@

TiApp *sharedApp;

int TiDebugPort = 2525;

NSString *TITANIUM_VERSION;

extern void UIColorFlushCache();

#define SHUTDOWN_TIMEOUT_IN_SEC 3
#define TIV @"TiVerify"

BOOL applicationInMemoryPanic = NO;
BOOL applicationInMemoryPanic = NO; // TODO: Remove in SDK 9.0+

// TODO: Remove in SDK 9.0+
TI_INLINE void waitForMemoryPanicCleared(); //WARNING: This must never be run on main thread, or else there is a risk of deadlock!

@interface TiApp ()
Expand All @@ -44,11 +43,6 @@ - (void)appBoot;

@implementation TiApp

- (void)clearMemoryPanic
{
applicationInMemoryPanic = NO;
}

@synthesize window, controller;
@synthesize disableNetworkActivityIndicator;
@synthesize remoteNotification;
Expand Down Expand Up @@ -1028,12 +1022,7 @@ - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
[self tryToInvokeSelector:@selector(applicationDidReceiveMemoryWarning:)
withArguments:[NSOrderedSet orderedSetWithObject:application]];

applicationInMemoryPanic = YES;
[Webcolor flushCache];

[self performSelector:@selector(clearMemoryPanic)
withObject:nil
afterDelay:0.0];
}

- (void)applicationWillResignActive:(UIApplication *)application
Expand Down
2 changes: 0 additions & 2 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollMethod.m
Expand Up @@ -16,7 +16,6 @@

JSValueRef KrollCallAsFunction(JSContextRef jsContext, JSObjectRef func, JSObjectRef thisObj, size_t argCount, const JSValueRef arguments[], JSValueRef *exception)
{
waitForMemoryPanicCleared();
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
KrollMethod *o = (KrollMethod *)JSObjectGetPrivate(func);
@try {
Expand Down Expand Up @@ -68,7 +67,6 @@ JSValueRef KrollCallAsFunction(JSContextRef jsContext, JSObjectRef func, JSObjec

JSValueRef KrollCallAsNamedFunction(JSContextRef jsContext, JSObjectRef func, JSObjectRef thisObj, size_t argCount, const JSValueRef arguments[], JSValueRef *exception)
{
waitForMemoryPanicCleared();
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

KrollMethod *o = (KrollMethod *)JSObjectGetPrivate(thisObj);
Expand Down
7 changes: 0 additions & 7 deletions iphone/TitaniumKit/TitaniumKit/Sources/Kroll/KrollObject.m
Expand Up @@ -89,7 +89,6 @@ JSValueRef ConvertIdTiValue(KrollContext *context, id obj)
//
void KrollFinalizer(JSObjectRef ref)
{
waitForMemoryPanicCleared();
id o = (id)JSObjectGetPrivate(ref);

if ((o == nil) || [o isKindOfClass:[KrollContext class]]) {
Expand Down Expand Up @@ -120,7 +119,6 @@ void KrollFinalizer(JSObjectRef ref)

bool KrollDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception)
{
waitForMemoryPanicCleared();
id o = (id)JSObjectGetPrivate(object);
if ([o isKindOfClass:[KrollObject class]]) {
NSString *name = (NSString *)JSStringCopyCFString(kCFAllocatorDefault, propertyName);
Expand All @@ -137,7 +135,6 @@ bool KrollDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef prope
//
void KrollInitializer(JSContextRef ctx, JSObjectRef object)
{
waitForMemoryPanicCleared();
id o = (id)JSObjectGetPrivate(object);
if ([o isKindOfClass:[KrollContext class]]) {
return;
Expand All @@ -155,8 +152,6 @@ void KrollInitializer(JSContextRef ctx, JSObjectRef object)

bool KrollHasProperty(JSContextRef jsContext, JSObjectRef object, JSStringRef propertyName)
{
waitForMemoryPanicCleared();

// Debugger may actually try to get properties off global Kroll property (which is a special case KrollContext singleton)
id privateObject = (id)JSObjectGetPrivate(object);
if ([privateObject isKindOfClass:[KrollContext class]]) {
Expand Down Expand Up @@ -185,7 +180,6 @@ bool KrollHasProperty(JSContextRef jsContext, JSObjectRef object, JSStringRef pr
//TODO: We should fetch from the props object and shortcut some of this. Especially now that callbacks are CURRENTLY write-only.
JSValueRef KrollGetProperty(JSContextRef jsContext, JSObjectRef object, JSStringRef prop, JSValueRef *exception)
{
waitForMemoryPanicCleared();
// Debugger may actually try to get properties off global Kroll property (which is a special case KrollContext singleton)
id privateObject = (id)JSObjectGetPrivate(object);
if ([privateObject isKindOfClass:[KrollContext class]]) {
Expand Down Expand Up @@ -258,7 +252,6 @@ JSValueRef KrollGetProperty(JSContextRef jsContext, JSObjectRef object, JSString
//
bool KrollSetProperty(JSContextRef jsContext, JSObjectRef object, JSStringRef prop, JSValueRef value, JSValueRef *exception)
{
waitForMemoryPanicCleared();
id privateObject = (id)JSObjectGetPrivate(object);
if ([privateObject isKindOfClass:[KrollContext class]]) {
return false;
Expand Down

0 comments on commit a12da5b

Please sign in to comment.