已合并
分布式回调异步初始化并增加TimeServiceClient空指针校验 #5017
stepend98创建于 12 天前
分布式回调异步初始化并增加TimeServiceClient空指针校验 #5017
已合并
共 2 个文件变更+18-6
| @@ -373,10 +373,12 @@ AdvancedNotificationService::AdvancedNotificationService() | |||
| 373 | systemEventObserver_ = std::make_shared<SystemEventObserver>(iSystemEvent); | 373 | systemEventObserver_ = std::make_shared<SystemEventObserver>(iSystemEvent); |
| 374 | DelayedSingleton<NotificationConfigParse>::GetInstance()->GetReportTrustListConfig(); | 374 | DelayedSingleton<NotificationConfigParse>::GetInstance()->GetReportTrustListConfig(); |
| 375 | 375 | ||
| 376 | - distributedKvStoreDeathRecipient_ = std::make_shared<DistributedKvStoreDeathRecipient>( | 376 | + notificationSvrQueue_.Submit([this]() { |
A | |||
| 377 | - std::bind(&AdvancedNotificationService::OnDistributedKvStoreDeathRecipient, this)); | 377 | + distributedKvStoreDeathRecipient_ = std::make_shared<DistributedKvStoreDeathRecipient>( |
| 378 | - dataManager_.RegisterKvStoreServiceDeathRecipient(distributedKvStoreDeathRecipient_); | 378 | + std::bind(&AdvancedNotificationService::OnDistributedKvStoreDeathRecipient, this)); |
| 379 | - InitDistributeCallBack(); | 379 | + dataManager_.RegisterKvStoreServiceDeathRecipient(distributedKvStoreDeathRecipient_); |
| 380 | + InitDistributeCallBack(); | ||
| 381 | + }); | ||
| 380 | 382 | ||
| 381 | 383 | ||
| 382 | NotificationAnalyticsUtil::CreateCleanExperDataTimerExecute(); | 384 | NotificationAnalyticsUtil::CreateCleanExperDataTimerExecute(); |


🤖 AI 代码检视意见(回复本评论可解决检视意见,点击被检视代码行左侧的小头像可收起检视意见)
🟠 构造函数中异步捕获 this 存在生命周期与竞态风险
位置:
L379-L382| 严重程度: High❓ 问题描述
在构造函数中向队列提交异步任务并捕获
this,存在两个风险:1) 如果对象构造失败或被提前销毁,异步任务执行时this将成为悬空指针;2) 异步执行InitDistributeCallBack()会导致对象在构造完成后处于未完全初始化状态,若其他线程在此期间访问该对象依赖的分布式回调资源,将引发竞态条件。💡 修复建议
修改建议:建议将初始化逻辑移至独立的
Init()方法中在 SA 启动后调用,避免在构造函数中异步执行导致竞态。若确需在构造函数中异步执行,应确保所有依赖该回调的接口均通过notificationSvrQueue_串行化调度。379: // 建议移至 Init() 方法中同步或异步调用,确保生命周期安全 380: // notificationSvrQueue_.Submit(this { 381: // ANS_LOGI("Init distribute callback asynchronously."); 382: // InitDistributeCallBack(); 383: // });