fix: fix async access causing invalid properties in SNI tray#434
Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom Mar 5, 2026
Merged
fix: fix async access causing invalid properties in SNI tray#434deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Conversation
There was a problem hiding this comment.
Hey - I've found 3 issues, and left some high level feedback:
- The new
menuImporter()implementation is declared non-const in the header but defined asconstin the cpp and then usesconst_cast; consider making it non-const everywhere (and calling it only from non-const contexts) to avoid the mismatch and the need forconst_cast. - In the constructor you assign
m_path = pair.firstbut then passm_pathas the first argument toDBusMenu(m_path, ...)while the description says you are storing the service path; double-check whetherpair.first/pair.secondsemantics are correct here and whetherm_pathshould actually hold the service name or the object path, and rename the member accordingly for clarity.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `menuImporter()` implementation is declared non-const in the header but defined as `const` in the cpp and then uses `const_cast`; consider making it non-const everywhere (and calling it only from non-const contexts) to avoid the mismatch and the need for `const_cast`.
- In the constructor you assign `m_path = pair.first` but then pass `m_path` as the first argument to `DBusMenu(m_path, ...)` while the description says you are storing the service path; double-check whether `pair.first`/`pair.second` semantics are correct here and whether `m_path` should actually hold the service name or the object path, and rename the member accordingly for clarity.
## Individual Comments
### Comment 1
<location path="plugins/application-tray/sniprotocolhandler.h" line_range="77" />
<code_context>
private:
void generateId();
+ DBusMenuImporter *menuImporter();
private:
</code_context>
<issue_to_address>
**issue (bug_risk):** Method signature for menuImporter differs between header and implementation (missing const in header).
In the .cpp file this method is defined as `DBusMenuImporter *SniTrayProtocolHandler::menuImporter() const`, but here it’s declared without `const`, which will cause a compile error. Please make the signatures match—ideally `DBusMenuImporter *menuImporter() const;`, given it’s used from `const` contexts (and adjust call sites if required).
</issue_to_address>
### Comment 2
<location path="plugins/application-tray/sniprotocolhandler.cpp" line_range="148-152" />
<code_context>
m_id = UTIL->generateUniqueId(id);
}
+DBusMenuImporter *SniTrayProtocolHandler::menuImporter() const
+{
+ if (!m_dbusMenuImporter) {
+ auto that = const_cast<SniTrayProtocolHandler *>(this);
+ that->m_dbusMenuImporter = new DBusMenu(m_path, m_sniInter->menu().path(), that);
+ }
+ return m_dbusMenuImporter;
</code_context>
<issue_to_address>
**suggestion:** The use of const_cast for lazy initialization in menuImporter suggests the constness model might be off.
Initializing `m_dbusMenuImporter` via `const_cast` in a `const` method obscures object state and thread-safety guarantees. Prefer either (1) making `menuImporter` non-const and using it only from non-const contexts, or (2) marking `m_dbusMenuImporter` as `mutable` to allow lazy init from const call sites. This preserves const-correctness without `const_cast`.
Suggested implementation:
```cpp
DBusMenuImporter *SniTrayProtocolHandler::menuImporter()
{
if (!m_dbusMenuImporter) {
m_dbusMenuImporter = new DBusMenu(m_path, m_sniInter->menu().path(), this);
}
return m_dbusMenuImporter;
}
```
1. In the corresponding header file (likely `plugins/application-tray/sniprotocolhandler.h`), change the declaration from:
`DBusMenuImporter *menuImporter() const;`
to:
`DBusMenuImporter *menuImporter();`
2. Ensure all call sites are from non-const contexts (the shown lambda using `this` is fine). If there are any calls on a `const SniTrayProtocolHandler`, those usages should be revisited or cast to non-const only where logically safe.
</issue_to_address>
### Comment 3
<location path="plugins/application-tray/sniprotocolhandler.cpp" line_range="162-165" />
<code_context>
QString SniTrayProtocolHandler::id() const
{
+ if (m_id.isEmpty()) {
+ const_cast<SniTrayProtocolHandler *>(this)->generateId();
+ }
return m_id;
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Lazy initialization of m_id via const_cast in id() can have subtle concurrency and constness implications.
If `id()` is called from multiple threads, this lazy write to `m_id` is racy. Even in single-threaded use it weakens const semantics. Prefer restoring eager initialization (e.g. call `generateId()` in the constructor) or make `m_id` `mutable` and perform lazy init without `const_cast`, using proper synchronization if concurrent access is possible.
Suggested implementation:
```cpp
uint32_t SniTrayProtocolHandler::windowId() const
{
return m_sniInter->windowId();
}
QString SniTrayProtocolHandler::id() const
{
return m_id;
}
```
To fully satisfy your review comment, you should also:
1. Ensure `m_id` is eagerly initialized, typically by calling `generateId();` in the `SniTrayProtocolHandler` constructor (or wherever the instance is first fully set up).
2. If you still want lazy initialization for some reason, mark `m_id` as `mutable` in the header and implement a thread-safe lazy initialization strategy there (e.g. using `std::call_once` or equivalent), avoiding `const_cast` entirely.
3. Verify that no callers rely on `id()` having side effects; it should now be a pure accessor.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
1. Fixed asynchronous access issue where properties like ID and menu path were accessed before being fully initialized 2. Delayed DBusMenuImporter creation until first actual use to avoid accessing uninitialized menu path 3. Added lazy initialization pattern for menu importer to handle async property updates 4. Moved initialization logic to separate init() method to ensure proper property setup 5. Stored service name and menu path separately for later use in menu importer creation Log: Fixed system tray icon properties becoming invalid due to asynchronous initialization Influence: 1. Test system tray icons appear correctly with proper IDs 2. Verify right-click context menus work for all tray icons 3. Test tray icon tooltips display correctly 4. Verify theme changes properly update tray icon menus 5. Test tray icon activation (left-click) and context menu (right-click) functionality 6. Ensure no crashes occur during tray icon initialization fix: 修复异步访问导致SNI托盘属性无效的问题 1. 修复异步访问问题,避免在属性完全初始化前访问ID和菜单路径 2. 延迟DBusMenuImporter的创建,直到首次实际使用时才创建,避免访问未初始 化的菜单路径 3. 为菜单导入器添加懒加载模式,以处理异步属性更新 4. 将初始化逻辑移至单独的init()方法,确保属性正确设置 5. 单独存储服务名称和菜单路径,供后续创建菜单导入器时使用 Log: 修复系统托盘图标属性因异步初始化而变为无效的问题 Influence: 1. 测试系统托盘图标是否正确显示并具有正确的ID 2. 验证所有托盘图标的右键上下文菜单是否正常工作 3. 测试托盘图标工具提示是否正确显示 4. 验证主题更改是否正常更新托盘图标菜单 5. 测试托盘图标激活(左键点击)和上下文菜单(右键点击)功能 6. 确保托盘图标初始化过程中不会发生崩溃 PMS: BUG-351643
deepin pr auto review这段代码主要对 1. 语法逻辑
2. 代码质量
3. 代码性能
4. 代码安全
总结建议这段代码的改动是积极的,主要实现了菜单导入器的延迟加载以优化性能。为了进一步提高代码质量,建议进行以下修改:
修改后的代码片段建议(头文件): private:
mutable DBusMenuImporter *m_dbusMenuImporter; // 添加 mutable
// ...修改后的代码片段建议(源文件): DBusMenuImporter *SniTrayProtocolHandler::menuImporter() const
{
if (!m_dbusMenuImporter) {
// 使用 mutable 成员,不再需要 const_cast<SniTrayProtocolHandler*>(this)
// 但如果 DBusMenu 构造函数严格要求非 const QObject*,则保留 const_cast 仅用于传参
m_dbusMenuImporter = new DBusMenu(m_service, m_menuPath, const_cast<SniTrayProtocolHandler*>(this));
}
return m_dbusMenuImporter;
} |
BLumia
approved these changes
Mar 5, 2026
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 18202781743, BLumia The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Contributor
Author
|
/forcemerge |
|
This pr force merged! (status: blocked) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
properties are available when accessed
accessing uninitialized menu
before initialization
creation
potentially null pointer
Log: Fixed system tray icons sometimes showing no menu or incorrect
behavior
Influence:
using SNI protocol)
fix: 修复异步访问导致SNI托盘属性无效的问题
Log: 修复系统托盘图标有时不显示菜单或行为异常的问题
Influence:
PMS: BUG-351643
Summary by Sourcery
Ensure SNI tray properties and menus are available when accessed by making notifier interaction synchronous and deferring menu/importer initialization until needed.
Bug Fixes: