iOS Timer死锁、卡死、主线程
1、现象
一次用户反馈,发生过这样得一种现成,在一个抽奖玩法活动中,部分用户反馈APP出现卡死现象。所谓卡死,就是字面意思,UI卡主不动,只有杀死app才能恢复。
得到反馈,初步猜测是定时器,怀疑是某种机缘巧合结下的因果,复现概率还挺低的,在努力之下还是成功复现了一次。
因为没有crash,控制台没有打印任何有用的信息,所以点击了Pause program execution。
找了一些有用的信息并且佐证了之前的猜测
看来问题,就出在调用timer的invalidate方法上,我习惯的结题思路就是写个复现demo。
2、复现
一开始猜测是不是invalidate方法的调用需要满足某种条件才能正常运行,首先我构建了几种情况,
2.1 在【主线程】中创建定时器,在【主线程】中销毁定时器
- (void)clickClouseZuse
{
self.count = 10;
NSLog(@"创建定时器%@",[NSThread currentThread]);
self.timerBug = [YYTimer timerWithTimeInterval:0.2 target:self selector:@selector(countDownbug) repeats:YES];
}
- (void)countDownbug {
NSLog(@"%@~~~~%d",[NSThread currentThread],self.count);
if (self.count <= 0) {
if (self.timerBug) {
NSLog(@"销毁定时器%@",[NSThread currentThread]);
[self.timerBug invalidate];
self.timerBug = nil;
}
}
self.count--;
}
console
2021-07-12 15:11:11.113576+0800 TestUI[19147:255529] 创建定时器<NSThread: 0x600000d74180>{number = 1, name = main}
2021-07-12 15:11:11.315391+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~10
2021-07-12 15:11:11.515295+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~9
2021-07-12 15:11:11.715205+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~8
2021-07-12 15:11:11.915222+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~7
2021-07-12 15:11:12.115217+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~6
2021-07-12 15:11:12.315225+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~5
2021-07-12 15:11:12.515212+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~4
2021-07-12 15:11:12.715214+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~3
2021-07-12 15:11:12.915204+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~2
2021-07-12 15:11:13.115199+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~1
2021-07-12 15:11:13.314548+0800 TestUI[19147:255529] <NSThread: 0x600000d74180>{number = 1, name = main}~~~~0
2021-07-12 15:11:13.314790+0800 TestUI[19147:255529] 销毁定时器<NSThread: 0x600000d74180>{number = 1, name = main}
√页面成功复现卡死
2.2 在【主线程】中创建定时器,在【子线程】中销毁定时器
- (void)clickClouseZuse
{
self.count = 10;
NSLog(@"创建定时器%@",[NSThread currentThread]);
self.timerBug = [YYTimer timerWithTimeInterval:0.2 target:self selector:@selector(countDown) repeats:YES];
}
- (void)countDown {
if (self.count <= 0) {
[self selfinvalidate];
}
NSLog(@"%@~~~~%d",[NSThread currentThread],self.count);
self.count--;
}
- (void)selfinvalidate
{
[NSThread detachNewThreadSelector:@selector(invalidateTimer) toTarget:self withObject:nil];
}
- (void)invalidateTimer
{
if (self.timerBug) {
NSLog(@"销毁定时器%@",[NSThread currentThread]);
[self.timerBug invalidate];
self.timerBug = nil;
}
}
console
2021-07-12 15:20:38.238079+0800 TestUI[19235:263305] 创建定时器<NSThread: 0x60000361c1c0>{number = 1, name = main}
2021-07-12 15:20:38.439887+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~10
2021-07-12 15:20:38.639745+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~9
2021-07-12 15:20:38.838810+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~8
2021-07-12 15:20:39.039528+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~7
2021-07-12 15:20:39.238680+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~6
2021-07-12 15:20:39.438624+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~5
2021-07-12 15:20:39.639794+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~4
2021-07-12 15:20:39.838812+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~3
2021-07-12 15:20:40.039724+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~2
2021-07-12 15:20:40.239727+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~1
2021-07-12 15:20:40.439215+0800 TestUI[19235:263305] <NSThread: 0x60000361c1c0>{number = 1, name = main}~~~~0
2021-07-12 15:20:40.439776+0800 TestUI[19235:264009] 销毁定时器<NSThread: 0x600003672800>{number = 7, name = (null)}
x未卡死
2.3 在【子线程】中创建定时器,在【主线程】中销毁定时器
- (void)clickClouse {
self.count = 10;
[NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil];
}
- (void)startTimer
{
NSLog(@"创建定时器%@",[NSThread currentThread]);
self.timerBug = [YYTimer timerWithTimeInterval:0.2 target:self selector:@selector(countDownbug) repeats:YES];
}
- (void)countDownbug {
NSLog(@"%@~~~~%d",[NSThread currentThread],self.count);
if (self.count <= 0) {
if (self.timerBug) {
NSLog(@"销毁定时器%@",[NSThread currentThread]);
[self.timerBug invalidate];
self.timerBug = nil;
}
}
self.count--;
}
console
2021-07-12 15:24:26.319120+0800 TestUI[19297:267790] 创建定时器<NSThread: 0x600001c5f980>{number = 7, name = (null)}
2021-07-12 15:24:26.519599+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~10
2021-07-12 15:24:26.720411+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~9
2021-07-12 15:24:26.919556+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~8
2021-07-12 15:24:27.120604+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~7
2021-07-12 15:24:27.319585+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~6
2021-07-12 15:24:27.520522+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~5
2021-07-12 15:24:27.719543+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~4
2021-07-12 15:24:27.920728+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~3
2021-07-12 15:24:28.120747+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~2
2021-07-12 15:24:28.320334+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~1
2021-07-12 15:24:28.520451+0800 TestUI[19297:267114] <NSThread: 0x600001c34300>{number = 1, name = main}~~~~0
2021-07-12 15:24:28.520700+0800 TestUI[19297:267114] 销毁定时器<NSThread: 0x600001c34300>{number = 1, name = main}
√卡死
2.4 在【子线程】中创建定时器,在【子线程】中销毁定时器
- (void)clickClouse {
self.count = 10;
[NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil];
}
- (void)startTimer
{
NSLog(@"创建定时器%@",[NSThread currentThread]);
self.timerBug = [YYTimer timerWithTimeInterval:0.2 target:self selector:@selector(countDown) repeats:YES];
}
- (void)countDown {
if (self.count <= 0) {
[self selfinvalidate];
}
NSLog(@"%@~~~~%d",[NSThread currentThread],self.count);
self.count--;
}
- (void)selfinvalidate
{
[NSThread detachNewThreadSelector:@selector(invalidateTimer) toTarget:self withObject:nil];
}
- (void)invalidateTimer
{
if (self.timerBug) {
NSLog(@"销毁定时器%@",[NSThread currentThread]);
[self.timerBug invalidate];
self.timerBug = nil;
}
}
console
2021-07-12 15:27:21.969569+0800 TestUI[19346:271210] 创建定时器<NSThread: 0x600001002f00>{number = 7, name = (null)}
2021-07-12 15:27:22.170320+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~10
2021-07-12 15:27:22.370198+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~9
2021-07-12 15:27:22.570228+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~8
2021-07-12 15:27:22.770266+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~7
2021-07-12 15:27:22.970165+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~6
2021-07-12 15:27:23.170208+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~5
2021-07-12 15:27:23.370532+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~4
2021-07-12 15:27:23.570173+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~3
2021-07-12 15:27:23.771303+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~2
2021-07-12 15:27:23.970297+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~1
2021-07-12 15:27:24.170288+0800 TestUI[19346:270615] <NSThread: 0x6000010481c0>{number = 1, name = main}~~~~0
2021-07-12 15:27:24.170779+0800 TestUI[19346:271252] 销毁定时器<NSThread: 0x60000101d440>{number = 8, name = (null)}
x未卡死
统计下结果
�
序号 | 创建 | 销毁 | 结果 |
---|---|---|---|
1 | 主线程 | 主线程 | 卡死 |
2 | 主线程 | 子线程 | 未卡死 |
3 | 子线程 | 主线程 | 卡死 |
4 | 子线程 | 子线程 | 未卡死 |
以为就得出了结论:在主线程中执行invalidate方法就会导致现成死锁,结合实际的业务代码,发现可能有其他原因,于是继续实验。
2.5 在【主线程】中创建定时器,在【主线程】中销毁定时器(实验二)
本次实验调整了一下,不在倒计时方法里面调用invalidate,采用按钮主动调用的方式。
- (void)clickClouseZuse
{
self.count = 10;
NSLog(@"创建定时器%@",[NSThread currentThread]);
self.timerBug = [YYTimer timerWithTimeInterval:0.2 target:self selector:@selector(countDownbug) repeats:YES];
}
- (void)countDownbug {
NSLog(@"%@~~~~%d",[NSThread currentThread],self.count);
self.count--;
}
- (void)isLiving
{
NSLog(@"销毁定时器%@",[NSThread currentThread]);
[self.timerBug invalidate];
self.timerBug = nil;
}
console
2021-07-12 15:38:11.325684+0800 TestUI[19456:277925] 创建定时器<NSThread: 0x600002160300>{number = 1, name = main}
2021-07-12 15:38:11.527429+0800 TestUI[19456:277925] <NSThread: 0x600002160300>{number = 1, name = main}~~~~10
2021-07-12 15:38:11.726209+0800 TestUI[19456:277925] <NSThread: 0x600002160300>{number = 1, name = main}~~~~9
2021-07-12 15:38:11.927338+0800 TestUI[19456:277925] <NSThread: 0x600002160300>{number = 1, name = main}~~~~8
2021-07-12 15:38:12.128063+0800 TestUI[19456:277925] <NSThread: 0x600002160300>{number = 1, name = main}~~~~7
2021-07-12 15:38:12.225207+0800 TestUI[19456:277925] 销毁定时器<NSThread: 0x600002160300>{number = 1, name = main}
x未卡死
2.6 在【子线程】中创建定时器,在【主线程】中销毁定时器(实验二)
本次实验调整了一下,不在倒计时方法里面调用invalidate,采用按钮主动调用的方式。
- (void)clickClouse {
self.count = 10;
[NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil];
}
- (void)startTimer
{
NSLog(@"创建定时器%@",[NSThread currentThread]);
self.timerBug = [YYTimer timerWithTimeInterval:0.2 target:self selector:@selector(countDownbug) repeats:YES];
}
- (void)countDownbug {
NSLog(@"%@~~~~%d",[NSThread currentThread],self.count);
self.count--;
}
- (void)isLiving
{
NSLog(@"销毁定时器%@",[NSThread currentThread]);
[self.timerBug invalidate];
self.timerBug = nil;
}
console
2021-07-12 15:42:02.089586+0800 TestUI[19488:281156] 创建定时器<NSThread: 0x600001c989c0>{number = 6, name = (null)}
2021-07-12 15:42:02.291443+0800 TestUI[19488:280495] <NSThread: 0x600001cf4180>{number = 1, name = main}~~~~10
2021-07-12 15:42:02.491334+0800 TestUI[19488:280495] <NSThread: 0x600001cf4180>{number = 1, name = main}~~~~9
2021-07-12 15:42:02.691316+0800 TestUI[19488:280495] <NSThread: 0x600001cf4180>{number = 1, name = main}~~~~8
2021-07-12 15:42:02.890263+0800 TestUI[19488:280495] <NSThread: 0x600001cf4180>{number = 1, name = main}~~~~7
2021-07-12 15:42:03.090279+0800 TestUI[19488:280495] <NSThread: 0x600001cf4180>{number = 1, name = main}~~~~6
2021-07-12 15:42:03.290567+0800 TestUI[19488:280495] <NSThread: 0x600001cf4180>{number = 1, name = main}~~~~5
2021-07-12 15:42:03.473946+0800 TestUI[19488:280495] 销毁定时器<NSThread: 0x600001cf4180>{number = 1, name = main}
3、解决办法
这时候因为得出一个结论,yytimer的invalidate,不能在timer倒计时方法里面的主线程调用。
可以在子线程中选择释放。
原理,这只是从现象得出结论,根本原因下一篇分享。