-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Investigation: timers synchronization #33
base: master
Are you sure you want to change the base?
Changes from 6 commits
d5de41e
129ed8c
d3c2af4
2100b7b
b5677c7
716e2a7
a4aa2d5
ef92890
8b9d012
ee5c6a0
f2b47c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ static void _DTXCFTimerTrampoline(CFRunLoopTimerRef timer, void *info) | |
// NSLog(@"❤️ %p", timer); | ||
|
||
id<DTXTimerProxy> tp = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)]; | ||
[tp fire:(__bridge NSTimer*)timer]; | ||
[tp fire]; | ||
} | ||
|
||
static CFRunLoopTimerRef (*__orig_CFRunLoopTimerCreate)(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, CFRunLoopTimerCallBack callout, CFRunLoopTimerContext *context); | ||
|
@@ -72,6 +72,9 @@ CFRunLoopTimerRef __detox_sync_CFRunLoopTimerCreate(CFAllocatorRef allocator, CF | |
static CFRunLoopTimerRef (*__orig_CFRunLoopTimerCreateWithHandler)(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, void (^block) (CFRunLoopTimerRef timer)); | ||
CFRunLoopTimerRef __detox_sync_CFRunLoopTimerCreateWithHandler(CFAllocatorRef allocator, CFAbsoluteTime fireDate, CFTimeInterval interval, CFOptionFlags flags, CFIndex order, void (^block) (id timer)) | ||
{ | ||
NSLog(@"🤔 CFRunLoopTimerCreateWithHandler"); | ||
|
||
// What is this doing? We don't seem to be creating a trampoline here - I feel like we should be? | ||
return (__bridge_retained CFRunLoopTimerRef)[[NSTimer alloc] initWithFireDate:CFBridgingRelease(CFDateCreate(allocator, fireDate)) interval:interval repeats:interval > 0 block:block]; | ||
} | ||
|
||
|
@@ -94,9 +97,9 @@ void __detox_sync_CFRunLoopRemoveTimer(CFRunLoopRef rl, CFRunLoopTimerRef timer, | |
// NSLog(@"🤦♂️ removeTimer: %@", NS(timer)); | ||
|
||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)]; | ||
[trampoline untrack]; | ||
|
||
__orig_CFRunLoopRemoveTimer(rl, timer, mode); | ||
|
||
[trampoline untrack]; | ||
Comment on lines
99
to
+102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've observed that By switching these, I was hoping that the timer would be cancelled (ensuring that the This doesn't seem to be the case, and I'm still seeing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Untracking the timer only after removing it from the runloop does make sense, but I'm not sure if it matters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's very weird. 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the thread where this timer was created and the thread where the timer was called to invalidate are different? Apple docs (invalidate()):
|
||
} | ||
|
||
static void (*__orig_CFRunLoopTimerInvalidate)(CFRunLoopTimerRef timer); | ||
|
@@ -105,9 +108,9 @@ void __detox_sync_CFRunLoopTimerInvalidate(CFRunLoopTimerRef timer) | |
// NSLog(@"🤦♂️ invalidate: %@", NS(timer)); | ||
|
||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:NS(timer)]; | ||
[trampoline untrack]; | ||
|
||
__orig_CFRunLoopTimerInvalidate(timer); | ||
|
||
[trampoline untrack]; | ||
} | ||
|
||
static void (*__orig___NSCFTimer_invalidate)(NSTimer* timer); | ||
|
@@ -116,11 +119,20 @@ void __detox_sync___NSCFTimer_invalidate(NSTimer* timer) | |
// NSLog(@"🤦♂️ invalidate: %@", timer); | ||
|
||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:timer]; | ||
[trampoline untrack]; | ||
|
||
__orig___NSCFTimer_invalidate(timer); | ||
|
||
[trampoline untrack]; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
id<DTXTimerProxy> trampoline = [DTXTimerSyncResource existingTimerProxyWithTimer:self]; | ||
|
||
if(trampoline) { | ||
NSLog(@"🤦♂️ dealloc, but trampoline was still active: %@", self); | ||
[trampoline untrack]; | ||
} | ||
} | ||
Comment on lines
+127
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding this makes it safe to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense. |
||
|
||
+ (void)load | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,45 +113,45 @@ - (void)_untrackForParam:(NSUInteger*)param eventIdentifier:(NSString*(NS_NOESCA | |
- (void)trackViewNeedsLayout:(UIView *)view | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewNeedsLayoutCount eventDescription:_DTXStringReturningBlock(@"View Layout") objectDescription:_DTXStringReturningBlock(view.__detox_sync_safeDescription)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_viewNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. being explicit in such cases is indeed better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but except for being more explicit, is there any other goal in this change? What exactly is the fix here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, yes. Sorry. This fixes an XCode warning: |
||
}); | ||
} | ||
|
||
- (void)trackViewNeedsDisplay:(UIView *)view | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewNeedsDisplayCount eventDescription:_DTXStringReturningBlock(@"View Display") objectDescription:_DTXStringReturningBlock(view.__detox_sync_safeDescription)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_viewNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
- (void)trackLayerNeedsLayout:(CALayer *)layer | ||
{ | ||
NSString* identifier = [self _trackForParam:&_layerNeedsLayoutCount eventDescription:_DTXStringReturningBlock(@"Layer Layout") objectDescription:_DTXStringReturningBlock(layer.description)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_layerNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_layerNeedsLayoutCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
- (void)trackLayerNeedsDisplay:(CALayer *)layer | ||
{ | ||
NSString* identifier = [self _trackForParam:&_layerNeedsDisplayCount eventDescription:_DTXStringReturningBlock(@"Layer Display") objectDescription:_DTXStringReturningBlock(layer.description)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_layerNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_layerNeedsDisplayCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
- (void)trackLayerPendingAnimation:(CALayer*)layer | ||
{ | ||
NSString* identifier = [self _trackForParam:&_layerPendingAnimationCount eventDescription:_DTXStringReturningBlock(@"Layer Pending Animation") objectDescription:_DTXStringReturningBlock(layer.description)]; | ||
|
||
__detox_sync_orig_dispatch_async(dispatch_get_main_queue(), ^ { | ||
[self _untrackForParam:&_layerPendingAnimationCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_layerPendingAnimationCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}); | ||
} | ||
|
||
|
@@ -160,9 +160,9 @@ - (void)trackViewControllerWillAppear:(UIViewController *)vc | |
if(vc.transitionCoordinator) | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewControllerWillAppearCount eventDescription:_DTXStringReturningBlock(@"View Layout") objectDescription:_DTXStringReturningBlock(vc.description)]; | ||
|
||
[vc.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | ||
[self _untrackForParam:&_viewControllerWillAppearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewControllerWillAppearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}]; | ||
} | ||
} | ||
|
@@ -172,9 +172,9 @@ - (void)trackViewControllerWillDisappear:(UIViewController *)vc | |
if(vc.transitionCoordinator) | ||
{ | ||
NSString* identifier = [self _trackForParam:&_viewControllerWillDisappearCount eventDescription:_DTXStringReturningBlock(@"View Layout") objectDescription:_DTXStringReturningBlock(vc.description)]; | ||
|
||
[vc.transitionCoordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | ||
[self _untrackForParam:&_viewControllerWillDisappearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
[self _untrackForParam:&self->_viewControllerWillDisappearCount eventIdentifier:_DTXStringReturningBlock(identifier)]; | ||
}]; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,19 +16,19 @@ @implementation _DTXTimerTrampoline | |
{ | ||
id _target; | ||
SEL _sel; | ||
ball-hayden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
//NSTimer | ||
__weak NSTimer* _timer; | ||
CFRunLoopTimerCallBack _callback; | ||
CFRunLoopRef _runLoop; | ||
NSString* _timerDescription; | ||
NSTimeInterval _timeUntilFire; | ||
|
||
//CADisplayLink | ||
__weak CADisplayLink* _displayLink; | ||
|
||
BOOL _tracking; | ||
|
||
#if DEBUG | ||
NSString* _history; | ||
#endif | ||
|
@@ -53,7 +53,7 @@ - (instancetype)initWithTarget:(id)target selector:(SEL)selector fireDate:(NSDat | |
_timeUntilFire = [fireDate timeIntervalSinceNow]; | ||
_ti = ti; | ||
_repeats = rep; | ||
|
||
#if DEBUG | ||
_history = [NSString stringWithFormat:@"%@\n%@", NSStringFromSelector(_cmd), NSThread.callStackSymbols]; | ||
#endif | ||
|
@@ -71,7 +71,7 @@ - (instancetype)initWithCallback:(CFRunLoopTimerCallBack)callback fireDate:(NSDa | |
_timeUntilFire = [fireDate timeIntervalSinceNow]; | ||
_ti = ti; | ||
_repeats = rep; | ||
|
||
#if DEBUG | ||
_history = [NSString stringWithFormat:@"%@\n%@", NSStringFromSelector(_cmd), NSThread.callStackSymbols]; | ||
#endif | ||
|
@@ -86,8 +86,8 @@ - (BOOL)isDead | |
|
||
- (void)dealloc | ||
{ | ||
[self untrack]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand properly, the only time we should be For us to have done that, we must have called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I understand as well. I will revisit this and make sure that's indeed the case but generally I think you're right. |
||
NSLog(@"🤦♂️ trampoline dealloc: %@ (tracking: %d)", self, _tracking); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I added this to verify the assumption above. I've not seen this called with tracking YES). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
objc_setAssociatedObject(_timer, __DTXTimerTrampolineKey, nil, OBJC_ASSOCIATION_RETAIN); | ||
} | ||
|
||
|
@@ -96,7 +96,7 @@ - (void)setTimer:(NSTimer*)timer | |
_timer = timer; | ||
_timerDescription = [[timer debugDescription] copy]; | ||
objc_setAssociatedObject(timer, __DTXTimerTrampolineKey, self, OBJC_ASSOCIATION_RETAIN); | ||
|
||
#if DEBUG | ||
_history = [NSString stringWithFormat:@"%@\n%@", _history, [timer debugDescription]]; | ||
#endif | ||
|
@@ -112,47 +112,24 @@ - (void)setDisplayLink:(CADisplayLink*)displayLink | |
#endif | ||
} | ||
|
||
- (void)fire:(id)timer | ||
- (void)fire | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following changes are a bit more involved. Firstly, there's no need for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can understand why the |
||
{ | ||
//This is to ensure the timer is still valid after fire. | ||
CFRunLoopRef runloop = CFRunLoopGetCurrent(); | ||
CFRunLoopMode mode = CFRunLoopCopyCurrentMode(runloop); | ||
CFRunLoopPerformBlock(runloop, mode, ^{ | ||
if(CFRunLoopTimerIsValid((__bridge CFRunLoopTimerRef)timer) == NO) | ||
{ | ||
[self untrack]; | ||
|
||
CFRelease(mode); | ||
|
||
return; | ||
} | ||
|
||
CFRunLoopPerformBlock(runloop, mode, ^{ | ||
if(CFRunLoopTimerIsValid((__bridge CFRunLoopTimerRef)timer) == NO) | ||
{ | ||
[self untrack]; | ||
|
||
CFRelease(mode); | ||
|
||
return; | ||
} | ||
|
||
CFRelease(mode); | ||
}); | ||
}); | ||
Comment on lines
-117
to
-142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand right, the purpose of these are to see if the timer is still valid after it has fired. If we ask if the timer is valid inline here, Here I'm proposing a different method. We track the invalidation and removal of timers (in NSTimer+DTXSpy.m), which means we know when a repeating timer is stopping. We also know that a timer that doesn't repeat is finished once we have called its callback (or selector). Therefore, we can simply call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understand, this code checks whether the timer needs to be untracked (for example, as you mentioned - in case of a non-repeating timer) immediately after finishing the fire.
Generally, this sounds right, but I'm not sure if recurring / non-recurring is our only flow. What about calling to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling |
||
|
||
if(_callback) | ||
{ | ||
CFRunLoopTimerContext ctx; | ||
CFRunLoopTimerGetContext((__bridge CFRunLoopTimerRef)timer, &ctx); | ||
_callback((__bridge CFRunLoopTimerRef)timer, ctx.info); | ||
return; | ||
CFRunLoopTimerGetContext((__bridge CFRunLoopTimerRef)_timer, &ctx); | ||
_callback((__bridge CFRunLoopTimerRef)_timer, ctx.info); | ||
} | ||
|
||
if(_target && _sel) { | ||
IMP impl = [_target methodForSelector:_sel]; | ||
void (*func)(id, SEL, NSTimer*) = (void *)impl; | ||
func(_target, _sel, _timer); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a small tidy-up recommended by https://stackoverflow.com/a/20058585 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
if(!_repeats) { | ||
[self untrack]; | ||
} | ||
Comment on lines
+141
to
143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this will have the same result as the previous check. |
||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | ||
[_target performSelector:_sel withObject:timer]; | ||
#pragma clang diagnostic pop | ||
Comment on lines
-152
to
-155
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you removed this? I guess it was there for a reason There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is replaced by ln 131-138 - gaining a reference to I think my previous comment was lost - sorry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It wasn't, you're right #33 (comment), forgot about this for a moment 😅 |
||
} | ||
|
||
- (void)track | ||
|
@@ -161,7 +138,7 @@ - (void)track | |
{ | ||
return; | ||
} | ||
|
||
_tracking = YES; | ||
[DTXTimerSyncResource.sharedInstance trackTimerTrampoline:self]; | ||
} | ||
|
@@ -172,9 +149,9 @@ - (void)untrack | |
{ | ||
return; | ||
} | ||
|
||
// NSLog(@"🤦♂️ untrack: %@", _timer); | ||
|
||
[DTXTimerSyncResource.sharedInstance untrackTimerTrampoline:self]; | ||
_tracking = NO; | ||
} | ||
|
@@ -192,12 +169,12 @@ + (NSDateFormatter*)_descriptionDateFormatter | |
} | ||
|
||
- (DTXBusyResource *)jsonDescription { | ||
return @{ | ||
@"fire_date": _fireDate ? [_DTXTimerTrampoline._descriptionDateFormatter stringFromDate:_fireDate] : @"none", | ||
@"time_until_fire": @(_timeUntilFire), | ||
@"is_recurring": @(_repeats), | ||
@"repeat_interval": @(_ti) | ||
}; | ||
return @{ | ||
@"fire_date": _fireDate ? [_DTXTimerTrampoline._descriptionDateFormatter stringFromDate:_fireDate] : @"none", | ||
@"time_until_fire": @(_timeUntilFire), | ||
@"is_recurring": @(_repeats), | ||
@"repeat_interval": @(_ti) | ||
}; | ||
Comment on lines
-195
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rest of the file is indented using tabs. Fixed for consistency (and editor happiness). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Legit 👍 |
||
} | ||
|
||
#if DEBUG | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we're swizzling
CFRunLoopTimerCreateWithHandler
without adding any tracking for these timers (as far as I can tell).Unless
initWithFireDate
somehow ends up callingCFRunLoopTimerCreate
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This happens a lot when running the Detox example app.
I don't understand what this is doing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably yes. This can be easily checked by putting a breakpoint on
__detox_sync_CFRunLoopTimerCreate
.https://github.com/wix/DetoxSync/tree/master/ExampleApp