iOS Timer死锁、卡死、主线程

iOS Timer死锁、卡死、主线程

1、现象

一次用户反馈,发生过这样得一种现成,在一个抽奖玩法活动中,部分用户反馈APP出现卡死现象。所谓卡死,就是字面意思,UI卡主不动,只有杀死app才能恢复。
得到反馈,初步猜测是定时器,怀疑是某种机缘巧合结下的因果,复现概率还挺低的,在努力之下还是成功复现了一次。

因为没有crash,控制台没有打印任何有用的信息,所以点击了Pause program execution。

image.png

找了一些有用的信息并且佐证了之前的猜测

image.png

看来问题,就出在调用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倒计时方法里面的主线程调用。
可以在子线程中选择释放。
原理,这只是从现象得出结论,根本原因下一篇分享。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,056评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,842评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,938评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,296评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,292评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,413评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,824评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,493评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,686评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,502评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,553评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,281评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,820评论 3 305
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,873评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,109评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,699评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,257评论 2 341

推荐阅读更多精彩内容