Skip to content

Commit

Permalink
Fix crash in MLDelayableTimer when calling invalidate
Browse files Browse the repository at this point in the history
Calling the invalidate method from some other thread than the thread
which added the timer to our runloop, will crash.
--> Add and remove the timer to/from the runloop by executing a block in
exactly this runloop.
  • Loading branch information
tmolitor-stud-tu committed Nov 9, 2024
1 parent 59486b7 commit 0348330
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions Monal/Classes/MLDelayableTimer.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ -(void) start
return;
}
DDLogDebug(@"Starting timer: %@", self);
[[HelperTools getExtraRunloopWithIdentifier:MLRunLoopIdentifierTimer] addTimer:_wrappedTimer forMode:NSRunLoopCommonModes];
//scheduling and unscheduling of a timer must be done from the same thread --> use our runloop
[self scheduleBlockInRunLoop:^{
[[HelperTools getExtraRunloopWithIdentifier:MLRunLoopIdentifierTimer] addTimer:self->_wrappedTimer forMode:NSRunLoopCommonModes];
}];
}
}

Expand Down Expand Up @@ -131,8 +134,28 @@ -(void) invalidate
return;
}
//DDLogVerbose(@"Invalidating timer: %@", self);
[_wrappedTimer invalidate];
//scheduling and unscheduling of a timer must be done from the same thread --> use our runloop
[self scheduleBlockInRunLoop:^{
[self->_wrappedTimer invalidate];
}];
}
}

-(void) scheduleBlockInRunLoop:(monal_void_block_t) block
{
NSRunLoop* runLoop = [HelperTools getExtraRunloopWithIdentifier:MLRunLoopIdentifierTimer];
// NSCondition* condition = [NSCondition new];
// [condition lock];
CFRunLoopPerformBlock([runLoop getCFRunLoop], (__bridge CFStringRef)NSDefaultRunLoopMode, ^{
block();
// [condition lock];
// [condition signal];
// [condition unlock];
});
CFRunLoopWakeUp([runLoop getCFRunLoop]); //trigger wakeup of runloop to execute the block as soon as possible
// //wait for our block to finish executing
// [condition wait];
// [condition unlock];
}

@end

0 comments on commit 0348330

Please sign in to comment.