Skip to content

fix: start notification expire timer only after bubble is displayed - #1691

Open
Ivy233 wants to merge 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/notification-expire-after-display
Open

fix: start notification expire timer only after bubble is displayed#1691
Ivy233 wants to merge 1 commit into
linuxdeepin:masterfrom
Ivy233:fix/notification-expire-after-display

Conversation

@Ivy233

@Ivy233 Ivy233 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor
  1. Move the pending-timeout machinery from NotificationManager to a frontend ExpireTimer singleton, keeping one shared QTimer and absolute deadlines in a single QMultiHash while removing all server-side timer state
  2. Start a countdown only when a notification is actually inserted into the bubble model or the visible staging model, so queued, hidden and overflow notifications no longer expire before being displayed
  3. Make ExpireTimer::push idempotent for the same entity (same id + cTime), so the bubble and the staging area share the first deadline instead of restarting it, and cancel the old bubble-slot countdown when a replacement arrives
  4. Move hover blocking into ExpireTimer::setBlockId with a short grace period, without spinning the shared timer at a zero interval
  5. Keep the server authoritative for the notification lifecycle: expiration is reported to NotificationManager::notificationClosed via a queued invocation, and countdowns are stopped from the server's NotificationStateChanged instead of frontend model removal; notificationClosed now reports a close only once per notification
  6. Derive effective timeouts from NotifyEntity (timeout 0 or Critical urgency never expires, -1 falls back to the 5000 ms default) and carry bubbleId in the expired signal
  7. Match staging-area replacements by bubble id (the key preserved across a replace), like BubbleModel::replaceBubbleIndex
  8. Remove the obsolete pending-timeout code from NotificationManager/NotifyServerApplet and its outdated tests
  9. Also fix taskbar icons not updating when the Icon field of the desktop file changes (forward dataChanged in DockGlobalElementModel, fix refresh of multi-mapped rows in RoleCombineModel)

Log: Start the notification expire countdown only after the notification is displayed

Influence:

  1. A displayed notification disappears after its expire timeout (default 5 seconds); queued notifications no longer expire while hidden
  2. Hovering a bubble keeps it on screen, and it lingers about 1 second after the hover ends
  3. The same notification shown in the bubble and the staging area shares one countdown and is not timed twice
  4. Run the notification server and taskmanager unit tests

fix: 通知显示后才启动过期计时

  1. NotificationManager 中的 pending-timeout 机制整体迁移到前端 ExpireTimer 单例:保留单个共享 QTimer 与按绝对截止时间组织的 QMultiHash,移除服务端全部定时器状态
  2. 仅在通知真正插入气泡模型或可见的暂存区模型时才启动倒计时,排队、隐藏和溢出的通知不再提前过期
  3. ExpireTimer::push 对同一通知(相同 id + cTime)幂等:气泡与暂存区共享首次截止时间、不重复计时;替换通知按 bubbleId 取消旧槽位倒计时后再启动新计时
  4. 悬停阻断逻辑下沉到 ExpireTimer::setBlockId,移开悬停后有短暂宽限期,共享 QTimer 不会以 0 间隔空转
  5. 生命周期仍由服务端统一管理:到期经 queued 调用转入 NotificationManager::notificationClosed,前端根据服务端 NotificationStateChanged 停止计时而非自行移除;notificationClosed 增加去重,避免同一通知重复上报关闭
  6. 有效超时时间由 NotifyEntity 推导(timeout 为 0 或 Critical 紧急级别永不过期,-1 回退默认 5000ms),expired 信号携带 bubbleId
  7. 暂存区替换匹配改用 bubbleId(替换时唯一不变的键),与 BubbleModel::replaceBubbleIndex 保持一致
  8. 删除 NotificationManager/NotifyServerApplet 中过时的 pending-timeout 代码及其旧测试
  9. 同时修复 desktop 文件 Icon 字段变化后任务栏图标不更新的问题(DockGlobalElementModel 转发 dataChanged,RoleCombineModel 修复多对一映射行的刷新)

Log: 通知显示后才启动过期计时

Influence:

  1. 气泡显示后默认 5 秒消失;大量通知排队时不再在显示前提前过期
  2. 鼠标悬停气泡时不消失,移开后停留约 1 秒
  3. 同一通知在横幅与暂存区共用同一倒计时,不会重复计时
  4. 运行通知服务端与任务管理器单元测试

PMS: BUG-372279

@deepin-ci-robot

Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Ivy233

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR defers starting the notification expiration timer until a bubble is actually inserted into the UI model, wiring a new bubbleDisplayed signal through BubblePanel to NotificationManager, which now schedules timeouts based on the stored client expire timeout only when notifications are displayed, with thread-safe forwarding from the applet and added unit tests.

Sequence diagram for deferred notification timeout start when bubble is displayed

sequenceDiagram
    participant BubbleModel
    participant BubblePanel
    participant NotifyServerApplet
    participant NotificationManager

    BubbleModel->>BubbleModel: insertBubble / replaceBubble
    BubbleModel-->>BubblePanel: bubbleDisplayed(id)
    BubblePanel->>NotifyServerApplet: notificationDisplayed(id)
    NotifyServerApplet->>NotificationManager: notificationDisplayed(id)
    NotificationManager->>NotificationManager: fetchEntity(id)
    NotificationManager->>NotificationManager: pushPendingEntity(entity, entity.timeout())
Loading

File-Level Changes

Change Details Files
Start notification expiration only when the bubble is displayed instead of on receipt.
  • Removed immediate scheduling of pending notification timeouts in Notify() based on hints expireTimeout.
  • Added a notificationDisplayed(qint64) slot in NotificationManager that fetches the entity, checks validity/urgency, reads its timeout(), and calls pushPendingEntity only for non-critical, expiring notifications.
  • Introduced a timeout() accessor on NotifyEntity to expose the stored expire timeout instead of passing it around separately.
panels/notification/server/notificationmanager.cpp
panels/notification/server/notificationmanager.h
panels/notification/common/notifyentity.cpp
panels/notification/common/notifyentity.h
Signal when a bubble is actually shown and propagate that to the notification server with correct threading semantics.
  • Added a bubbleDisplayed(qint64) signal to BubbleModel and emit it when inserting or replacing bubbles in the model.
  • Connected BubbleModel::bubbleDisplayed in BubblePanel to forward the ID to the notification server via notificationDisplayed using a direct connection.
  • Implemented NotifyServerApplet::notificationDisplayed to forward the call into NotificationManager::notificationDisplayed using Qt::QueuedConnection so the timeout QTimer starts on the worker thread.
panels/notification/bubble/bubblemodel.h
panels/notification/bubble/bubblemodel.cpp
panels/notification/bubble/bubblepanel.cpp
panels/notification/server/notifyserverapplet.h
panels/notification/server/notifyserverapplet.cpp
Add unit coverage for the new notificationDisplayed path to ensure robustness for various IDs.
  • Added a basic NotifyServerApplet test that calls notificationDisplayed with a valid ID to ensure no crashes.
  • Added edge-case tests that call notificationDisplayed with 0, -1, and max qint64, verifying the applet handles these IDs without throwing.
tests/panels/notification/server/notifyserverapplet_test.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Ivy233
Ivy233 marked this pull request as ready for review August 6, 2026 06:59

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @Ivy233, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from 430dd63 to fcf70d8 Compare August 6, 2026 11:52
Comment thread panels/notification/bubble/bubblemodel.cpp Outdated
Comment thread panels/notification/bubble/bubblemodel.cpp Outdated
Comment thread panels/notification/bubble/bubblemodel.cpp Outdated
@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch 2 times, most recently from 0b0eb8c to 2ae625f Compare August 7, 2026 06:21
@Ivy233
Ivy233 requested a review from 18202781743 August 7, 2026 06:25
@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from 2ae625f to 5ef467e Compare August 7, 2026 08:07

Q_EMIT NotificationStateChanged(entity.id(), entity.processedType());

bool critical = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个逻辑去掉了,那定时器没拿这个参数?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

服务端定时器逻辑整体移除后,超时计算完全由前端 ExpireTimer 负责,ExpireTimer 内部通过 NotifyEntity::timeout()/urgency() 自行推导有效超时时间(-1 默认 5000ms,0/Critical 永不过期),因此无需再把 expireTimeout 参数单独传给定时器。


bool contains(qint64 key) const;
// Milliseconds left for key, or 0 when it is not tracked.
int remaining(qint64 key) const;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按操作来定义接口吧,不用搞这么通用的,然后让调用者去组合,
这里只有start,stop,clear吧,resume类似传递需要停止的entity,逻辑在内部封装,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改。

* that key. All bookkeeping lives in hash maps, so no QTimer is allocated per
* key and nothing leaks when a key expires or is stopped.
*/
class ExpireTimer : public QObject

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

暂存区和通知横幅的是不是共用同一个定时器管理的呀,不然这里会不会一个通知有两个定时器在弄呀,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是共用的。ExpireTimer 是进程级单例,仅一个共享 QTimer,气泡与暂存区都通过它管理。同一通知 id 同时在气泡和暂存区显示时,ExpireTimer::push 发现 id 已在跟踪中会保持原截止时间而不重建倒计时,不会出现一个通知被两个定时器同时计时的现象。

@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from 5ef467e to aa5a30c Compare August 10, 2026 03:27
@Ivy233
Ivy233 requested a review from 18202781743 August 10, 2026 05:11
@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from aa5a30c to fadfda6 Compare August 11, 2026 09:25
return &expireTimer;
}

void ExpireTimer::start(qint64 key, const NotifyEntity &entity)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里里的key就是entity的id吧,直接从entity里获取就行了,这个key名称换成id吧,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改。

if (id == m_blockedId)
return;

if (m_blockedId != NotifyEntity::InvalidId)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个hover就blocked的逻辑可以放在ExpirreTimer里实现吧,不暴露resume和pause了,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经把block逻辑迁移到expiretimer里面,并替代掉resume和pause。

// A bubble that has been pushed off the display (overflow) is no longer
// in the model, but its countdown still finishes and the notification
// should still be closed, so fall back to the persisted bubble id.
Q_EMIT bubbleExpired(id, DataAccessorProxy::instance()->fetchEntity(id).bubbleId());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然bubbleExpired需要bubbleId,那在ExpireTimer::expired里就直接添加这个参数就行了,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已按建议处理。ExpireTimer::expired 的签名就是 expired(qint64 id, uint bubbleId),到期时从实体读取 bubbleId 随信号一并发出,BubbleModel::bubbleExpired(id, bubbleId) 直接转发,无需自行拼接。

if (it == m_deadlines.cend())
return;

m_paused.insert(key, static_cast<int>(qMax<qint64>(0, it.value() - QDateTime::currentMSecsSinceEpoch())));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pause的只会是一个吧,最起码目前可以只是一个,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已经替换成只有一个。

if (m_deadlines.contains(key) || m_paused.contains(key))
return;

m_deadlines.insert(key, QDateTime::currentMSecsSinceEpoch() + interval);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个还是解决不了在通知横幅定时器跑了一半的时候,再切到暂存区定时器又重新计时的问题呀,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已解决。ExpireTimer::remove 在气泡移出模型时挂起该 id 的倒计时(把绝对截止时间记入 m_retired),随后暂存区 NotifyStagingModel::push 调用 ExpireTimer::push 时从 m_retired 恢复原截止时间而不是重新计时;若暂停期间已过截止点,恢复时会按当前时间立即到期,不再从横幅跑了一半的地方重头开始。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

补充说明(方案有更新):最终实现没有引入 m_retired,改为更简单的幂等 push——ExpireTimer::push 检测到相同 id + cTime 已在跟踪时直接返回,保留首次(横幅显示时)写入的绝对截止时间。因此同一通知随后进入/切换到暂存区时复用原截止时间、不会重新计时;只有服务端关闭通知(NotificationStateChangedExpireTimer::remove)或替换通知(按 bubbleId 换槽)才会结束/重建计时。效果与之前描述的 m_retired 方案等价:横幅跑了一半切到暂存区,剩余时间照旧倒数,过期点不变。

return;

// Critical notifications must not disappear on their own.
if (reason == NotifyEntity::Expired && entity.urgency() == NotifyEntity::Critical)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expired的不会是Critical类型的吧,不需要这个判断吧,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修复。

@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from fadfda6 to e12655f Compare August 11, 2026 14:25
@Ivy233
Ivy233 requested a review from 18202781743 August 11, 2026 14:25
if (id == m_blockedId)
return;

m_blockedId = id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BubbleModel里不需要m_blockedId了吧,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除。悬停冻结逻辑整体下沉到 ExpireTimer::setBlockId(内部维护单一悬停 id),BubblePanel::setHoveredId 直接调用 ExpireTimer::instance()->setBlockId(id),BubbleModel 不再保存 m_blockedId。

// times out, close it and notify the server so it moves the notification
// from the in-memory store to the center database and emits the signals.
connect(m_bubbles, &BubbleModel::bubbleExpired, this, [this](qint64 id, uint bubbleId) {
closeBubble(id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要这里close吧,它会被server发送过来的吧,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已去掉。到期时 BubblePanel 只向服务端发送 notificationClosed(id, bubbleId, Expired),服务端处理后发出 NotificationStateChanged,气泡经由 onNotificationStateChanged 的正常流程关闭,不再本地 closeBubble。

const auto replaceIndex = replaceBubbleIndex(bubble);
const auto oldBubble = m_bubbles[replaceIndex];

ExpireTimer::instance()->stop(oldBubble->id());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个替换的逻辑,放在ExpireTimer里吧,只需要跟insert一样,push进去就行,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已迁移。replaceBubble 现在与 insertBubble 一样只调用 ExpireTimer::push;替换逻辑(取消同一气泡槽位旧通知的倒计时、悬停块转移给新通知)封装在 push 内部的 cancelReplacement 中统一处理。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已按建议修改:BubbleModel::replaceBubble 现在只调用 ExpireTimer::instance()->push(bubble->entity()),与 insertBubble 完全一致。替换语义全部下沉到 ExpireTimer::push 内部:isReplace() 的实体会按 bubbleId 取消旧槽位的倒计时(悬停 block id 同步转移到新 id)后,再按新实体的 timeout/urgency 启动新计时。

// Absolute deadlines of ids stopped by stop(); restored by start() so a
// context switch (bubble <-> staging) resumes the countdown.
QHash<qint64, qint64> m_retired;
QHash<qint64, uint> m_bubbleIds;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要m_bubbleIds和m_deadlines两个吧,放一个QHash<qint64, NotifyEntity>是不是就可以了,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已合并。m_bubbleIds 已删除,m_deadlines 改为 QHash<qint64, Deadline>,Deadline 同时保存实体与绝对截止时间(point),到期时所需的 bubbleId 直接从实体读取。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改:现在只保留一个容器 QMultiHash<qint64, NotifyEntity> m_pendingEntities,key 为该通知的绝对截止时间(共享 QTimer 按最近的截止时间调度),id/bubbleId/cTime 都由 value 中的 entity 携带,不再有 m_bubbleIds/m_deadlines 两个结构。

@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from e12655f to 0c427f4 Compare August 12, 2026 10:34
@Ivy233
Ivy233 requested a review from 18202781743 August 13, 2026 04:06
connect(NotifyAccessor::instance(), &NotifyAccessor::stagingEntityClosed, this, &NotifyStagingModel::onEntityClosed);
connect(NotifySetting::instance(), &NotifySetting::contentRowCountChanged, this, &NotifyStagingModel::updateContentRowCount);

connect(ExpireTimer::instance(), &ExpireTimer::expired, this, [this](qint64 id, uint bubbleId) {

@18202781743 18202781743 Aug 13, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个通知发送了expired后,会不会被通知横幅和暂存区都close一次呀,
是不是可以直接由ExpireTimer调用server的close呀,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理。

  1. 关于"会不会 close 两次":确实存在这个竞态,已修复。之前暂存区在 ExpireTimer::expired 时本地 remove + 补位,而服务端关闭是异步投递到 worker 线程的,导致刚过期的通知在补位时仍被当作 NotProcessed 重新插入并重新计时,一个倒计时周期后再次触发 notificationClosed(被幂等检查挡住,但会多打一条日志、多一次空跑)。现在暂存区不再响应 expired,只在服务端 stagingEntityClosed 回环时移除行,此时实体已标记 Processed,不会被补位重新选中。

  2. 关于"由 ExpireTimer 直接调用 server 的 close":采用等价且不破坏分层的做法——ExpireTimer 保持纯前端、不感知服务端,由 NotifyServerApplet 连接 ExpireTimer::expired,通过 Qt::QueuedConnection 统一转发给 NotificationManager::notificationClosed,作为过期关闭的唯一入口;notificationClosed 保留幂等检查兜底。横幅和暂存区各自只移除自己的视图,服务端每个通知只处理一次。

@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch 3 times, most recently from a5757b6 to c7b598c Compare August 14, 2026 07:28
@Ivy233
Ivy233 requested a review from 18202781743 August 14, 2026 07:30

const auto entity = notifyById(id);
if (entity.isValid())
ExpireTimer::instance()->remove(entity);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在服务端调用这个比较合适吧,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最新版本已按建议调整:NotifyStagingModel::remove 已不再直接调用 ExpireTimer::remove。通知生命周期统一由服务端管理——关闭/归档时服务端发出 NotificationStateChanged(Processed/Removed),由 NotifyServerApplet 统一调用 ExpireTimer::remove(id) 停止倒计时;前端各视图只负责移除自己的行,删除逻辑不再散落在前端模型里。

顺带说明一下新增的 m_opened 标志的用途:NotifyStagingModel 在通知中心面板隐藏时依然存活,stagingEntityReceived 随时会触发 doEntityReceived。若面板隐藏期间不拦截,通知会立即 push() 进入 ExpireTimer 启动倒计时,还没显示就过期,违背"实际显示后才开始计时"的原则。m_openedopen()/close()Panel.visibleChanged 驱动)维护,作为"暂存区是否可见"的标志:doEntityReceivedif (!m_opened) return; 确保只有面板可见、暂存区实际显示时才启动倒计时;open() 中的 if (m_opened) return; 作为重复进入保护。

@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch 2 times, most recently from 0ea9c62 to 24d7693 Compare August 17, 2026 05:33
@Ivy233
Ivy233 requested a review from 18202781743 August 17, 2026 05:39
qDebug(notifyLog) << "Receive entity" << id;

// The model exists while the panel is hidden. A hidden staging item has not
// been displayed and must not start the expiration countdown.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个场景不存在吧,

1. Move the pending-timeout machinery from NotificationManager to the frontend
   ExpireTimer singleton, preserving one shared QTimer and absolute deadlines in
   a QMultiHash while removing the server-side timer state
2. Start a countdown only when a notification is inserted into the bubble model
   or the visible staging model, so queued, hidden and overflow notifications do
   not expire before they are displayed
3. Make push idempotent for the same entity so the bubble and staging views share
   the first deadline, and replace the old bubble-slot countdown when a new
   entity replaces it
4. Keep hover blocking inside ExpireTimer and give an unblocked notification a
   short grace period without spinning the timer at a zero interval
5. Keep the server authoritative for notification lifecycle: forward expiration
   through NotifyServerApplet with a queued call, and stop countdowns from server
   state changes instead of frontend model removal
6. Derive effective timeouts from NotifyEntity (0/Critical never expires and -1
   uses the 5000 ms default) and carry bubbleId in the expiration signal
7. Remove the staging expiry race by waiting for the server close round trip
   before refilling rows; hidden-panel updates remain gated by NotifyAccessor
8. Remove the obsolete NotificationManager pending-timeout code and its applet
   tests
9. Match staging notification replacements by bubble id (the key preserved
   across a replace), exactly like BubbleModel::replaceBubbleIndex: a replacement
   may carry a fresh entity id (marked-processed then re-added) or the original
   entity id (replaceEntity), so matching on the entity id left the old staging
   row stale and inserted a duplicate instead of updating in place
10. Update the SPDX copyright year of notifystagingmodel.h to 2024-2026

Log: Start notification expiration after actual display and keep lifecycle cleanup
owned by the server

Influence:
1. Verify normal notifications disappear after their configured/default timeout
2. Verify queued and hidden notifications do not expire before being displayed
3. Verify opening the notification center starts only visible staging countdowns
4. Verify hovering prevents expiration and leaves a short grace period on exit
5. Verify replacement and shared bubble/staging notifications keep one countdown
6. Verify close, invoke and expiry paths stop countdowns through server state
7. Verify a replaced staging notification updates in place instead of leaving a
   stale row and inserting a duplicate

fix: 通知实际显示后才开始过期计时

1. 将待超时机制从 NotificationManager 迁移到前端 ExpireTimer 单例,保留一个
   共享 QTimer,并以 QMultiHash 保存绝对截止时间,同时移除服务端定时器状态
2. 仅在通知插入横幅模型或可见暂存区模型时启动倒计时,排队、隐藏及未展示的
   重叠通知不会在实际显示前过期
3. 同一实体重复 push 时保持首次截止时间,使横幅与暂存区共享一个倒计时;新实体
   替换通知时则移除同一气泡槽位的旧倒计时
4. 将悬停阻塞封装在 ExpireTimer 内,解除阻塞后保留短暂宽限期,并避免定时器以
   零间隔空转
5. 由服务端统一决定通知生命周期:NotifyServerApplet 通过队列调用转发过期关闭,
   并根据服务端状态变化停止倒计时,不再由前端模型移除操作清理
6. 由 NotifyEntity 推导有效超时(0/Critical 永不过期,-1 使用默认 5000ms),
   并在过期信号中携带 bubbleId
7. 暂存区等待服务端关闭回环后再补位,避免过期通知被重新插入;面板隐藏期间的
   更新继续由 NotifyAccessor 统一屏蔽
8. 移除 NotificationManager 中废弃的待超时代码及对应 applet 测试
9. 暂存区通知替换改为按 bubbleId 匹配(与 BubbleModel::replaceBubbleIndex
   一致):替换可能携带全新实体 id(旧实体标记已处理后再新增)或原实体 id
   (replaceEntity),按实体 id 匹配会导致旧行残留并插入重复行,无法原地更新
10. 更新 notifystagingmodel.h 的 SPDX 版权年份为 2024-2026

Log: 通知实际显示后开始过期计时,并由服务端统一管理生命周期清理

Influence:
1. 验证普通通知按配置或默认超时时间消失
2. 验证排队及隐藏通知不会在显示前过期
3. 验证打开通知中心时仅启动实际可见暂存通知的倒计时
4. 验证悬停阻止通知过期,移开后保留短暂宽限期
5. 验证替换通知以及横幅/暂存区共享通知只保留一个倒计时
6. 验证关闭、调用动作及过期路径均通过服务端状态停止倒计时
7. 验证被替换的暂存区通知原地更新,而非残留旧行并插入重复行

PMS: BUG-372279
@Ivy233
Ivy233 force-pushed the fix/notification-expire-after-display branch from 24d7693 to 8c366c9 Compare August 17, 2026 07:43
@Ivy233

Ivy233 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

回复 auto review 提出的多线程竞态问题:经逐一核实,ExpireTimer 的所有调用点均在主线程,不存在跨线程访问 m_pendingEntities 的路径,该问题为误报,无需加锁。

  1. BubbleModel::insertBubble/replaceBubbleBubblePanel::setHoveredIdNotifyStagingModel::push/replace 均为 UI 侧调用,运行在主线程;
  2. NotifyServerApplet::init() 中连接 NotificationStateChanged 的 lambda(内部调用 ExpireTimer::remove)使用 Qt::QueuedConnection,接收者为 applet 本身(主线程创建),因此该 lambda 也在主线程执行(panels/notification/server/notifyserverapplet.cpp:65);
  3. 唯一的跨线程调用是 expired → m_manager->notificationClosed,通过 Qt::QueuedConnection 投递到 worker 线程(notifyserverapplet.cpp:74-77),该路径不触碰 ExpireTimer 内部状态;
  4. 单例首次构造发生在 init()(主线程),QTimer 与哈希表同属主线程,符合 QTimer 的线程亲和性要求。

另外,示例中在持锁状态下调用 m_timer->start() 并不能解决跨线程问题——QTimer 只能在其所属线程启动,真正跨线程时需要 QMetaObject::invokeMethod 投递;本实现已确保全部访问在主线程,故无此问题。

@Ivy233

Ivy233 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

/test github-pr-review-ci

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码成功将通知过期定时器从服务端工作线程迁移至前端主线程单例,彻底解决了跨线程竞态与重复关闭问题
逻辑严密、注释详尽、架构设计优秀,无任何语法、质量、性能及安全问题

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

重构后的 ExpireTimer 单例通过 push 方法的幂等性设计(基于 idcTime 双重判定)与替换逻辑(基于 bubbleId 匹配并转移 blockId)精准规避了重复倒计时问题。notificationmanager.cpp 中通过校验 processedType() != NotifyEntity::NotProcessed 成功拦截了多源触发的重复关闭。notifyserverapplet.cpp 将跨线程调用由 DirectConnection 改为 QueuedConnection,彻底消除了线程安全隐患。notifystagingmodel.cpp 中将 replace 匹配键从 id 修正为 bubbleId,修复了携带新 id 的替换通知无法匹配的边界缺陷。整体逻辑闭环完整,无内存泄漏或空指针风险。

  • 2.代码质量(优秀)✓

代码注释极其详尽,准确解释了设计意图(如悬停宽限期机制、为何前端不直接关闭通知而交由服务端单一管理等)。命名规范清晰,如 effectiveTimeoutBlockItemTimeout 等见名知意。服务端果断移除了 setBlockClosedIdpushPendingEntityremovePendingEntity 等已废弃的冗余代码及对应的单元测试,降低了维护负担,符合高内聚低耦合的设计原则。

  • 3.代码性能(无性能问题)✓

通知数量通常受限于系统上限(一般几十条),ExpireTimer 中对 QMultiHash 的线性遍历及 onTimeout 中的排序操作在当前业务量级下不会产生性能瓶颈。定时器采用单例 single-shot 模式,避免了多定时器唤醒带来的不必要开销。
建议:在 onTimeout 中可通过在遍历时顺带记录最小时间戳来替代 keys()std::sort 的操作,将时间复杂度从 O(n log n) 降至 O(n),进一步提升极端场景下的执行效率。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次重构消除了原有的跨线程直接调用(Qt::DirectConnection)引发的竞态条件风险,未引入任何命令注入、缓冲区溢出、路径遍历等安全漏洞。跨线程通信均通过信号槽与 QMetaObject::invokeMethod 配合 Qt::QueuedConnection 安全投递,防重复关闭机制在服务端做了严格的状态机校验。
建议:保持当前安全的跨线程通信模式,后续新增涉及工作线程与主线程交互的代码时,务必延续此队列连接规范。

■ 【改进建议代码示例】

// expiretimer.cpp - 优化 onTimeout 性能,避免 keys() 拷贝与 sort 排序
void ExpireTimer::onTimeout()
{
    QList<NotifyEntity> timeoutEntities;
    qint64 minPoint = std::numeric_limits<qint64>::max();

    const auto current = QDateTime::currentMSecsSinceEpoch();
    for (auto iter = m_pendingEntities.begin(); iter != m_pendingEntities.end();) {
        if (iter.key() > current) {
            if (iter.key() < minPoint) {
                minPoint = iter.key();
            }
            ++iter;
            continue;
        }
        timeoutEntities << iter.value();
        iter = m_pendingEntities.erase(iter);
    }

    for (const auto &item : timeoutEntities) {
        if (!item.isValid()) {
            qWarning(notifyLog) << "Skipping timeout processing for invalid entity id:" << item.id()
                                << "appName:" << item.appName() << "cTime:" << item.cTime();
            continue;
        }

        if (item.id() == m_blockId) {
            const auto newPoint = current + BlockItemTimeout;
            m_pendingEntities.insert(newPoint, item);
            if (newPoint < minPoint) {
                minPoint = newPoint;
            }
            continue;
        }
        qDebug(notifyLog) << "Expired for the notification" << item.id() << item.appName();
        Q_EMIT expired(item.id(), item.bubbleId());
    }

    if (m_pendingEntities.isEmpty()) {
        m_timer->stop();
        m_lastPoint = std::numeric_limits<qint64>::max();
        return;
    }

    m_lastPoint = minPoint;
    m_timer->start(static_cast<int>(qMax<qint64>(0, m_lastPoint - QDateTime::currentMSecsSinceEpoch())));
}

@Ivy233
Ivy233 requested a review from 18202781743 August 17, 2026 09:43
@deepin-bot

deepin-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 2.0.53
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #1702

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants