Skip to content

fix: bind index lifecycle to daemon via D-Bus name watcher - #294

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:develop/snipe
Nov 10, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:develop/snipe

Conversation

@Johnson-zs

@Johnson-zs Johnson-zs commented Nov 10, 2026 •

Copy link
Copy Markdown

Root Cause Analysis

When deepin-anything-daemon stops or crashes, deepin-anything-index continues running with its session-bus D-Bus names still registered. dde-file-manager uses QDBusServiceWatcher to monitor index names on the session bus; since the index process never exits, serviceUnregistered never fires and the UI stays stuck in "updating" instead of switching to an error state. The root cause is that index does not monitor the daemon's system-bus name (org.deepin.Anything), so it cannot detect daemon disappearance and exit accordingly.

Fix Approach

Add a QDBusServiceWatcher in src/index/main.cpp watching org.deepin.Anything on the system bus for serviceUnregistered. When the daemon disappears, the watcher callback calls unregisterIndexServices() (releases session-bus names) and then QCoreApplication::exit(0), causing the index process to exit. dde-file-manager's existing index name watcher then detects serviceUnregistered and switches the UI to error state. On daemon recovery, the index is D-Bus-activated again and reconnects.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Pure additive change in main() — no existing function signatures modified, no public functions removed, no existing call paths altered. The watcher callback calls unregisterIndexServices() (existing, idempotent) and QCoreApplication::exit(0), which integrates with the existing atexit handler and sentinel cleanup.
  • Target code region is not a historical bug fix product; the new watcher complements rather than conflicts with the existing atexit handler and sentinel mechanism.

Business Impact Scope

Affected module: deepin-anything-index process lifecycle. User scenario: when the indexing daemon stops unexpectedly (crash, OOM, manual stop), the index process now exits cleanly instead of lingering with stale D-Bus names. dde-file-manager's UI correctly transitions to an error state instead of showing perpetual "updating". Normal shutdown/restart is unaffected (SIGTERM handler and atexit already call unregisterIndexServices).

Verification Suggestion

  1. Start daemon + index, stop daemon process, verify index exits and dde-file-manager UI shows error state.
  2. Restart daemon, verify index is D-Bus-activated and reconnects.
  3. Normal restart/shutdown: verify index exits cleanly without errors.

根因分析

当 deepin-anything-daemon 停止或崩溃时,deepin-anything-index 仍继续运行,其会话总线 D-Bus 名称仍保持注册。dde-file-manager 通过 QDBusServiceWatcher 监视会话总线上的 index 名称;由于 index 进程不退出,serviceUnregistered 不触发,UI 一直显示"更新中"而不切换到错误状态。根因是 index 未监视 daemon 的系统总线名称(org.deepin.Anything),无法感知 daemon 消失并退出。

修复方案

在 src/index/main.cpp 中新增 QDBusServiceWatcher,监听系统总线上 org.deepin.Anything 的 serviceUnregistered 信号。daemon 消失时,watcher 回调调用 unregisterIndexServices()(释放会话总线名称)后执行 QCoreApplication::exit(0),index 进程退出。文管现有的 index 名称 watcher 随即检测到 serviceUnregistered,切换 UI 为错误状态。daemon 恢复后 index 通过 D-Bus 激活重启并重连。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 纯增量改动,仅在 main() 中新增 watcher 代码块,不修改已有函数签名、不删除公开函数、不影响已有调用路径。回调调用的 unregisterIndexServices() 为已有幂等函数,QCoreApplication::exit(0) 经 Qt 事件循环正常退出,与现有 atexit handler 和 sentinel 清理机制互补不冲突。
  • 目标代码区域非历史修复产物,新增 watcher 不撤销任何已有修复。

业务影响范围

受影响模块:deepin-anything-index 进程生命周期。用户场景:索引 daemon 异常停止(崩溃、OOM、手动停止)时,index 进程现在会干净退出,而非带着过时的 D-Bus 名称继续运行。文管 UI 正确切换到错误状态,而非一直显示"更新中"。正常关机重启不受影响。

验证建议

  1. 启动 daemon 和 index,停止 daemon 进程,验证 index 退出且文管 UI 显示错误状态。
  2. 重启 daemon,验证 index 通过 D-Bus 激活重启并重连。
  3. 正常重启关机:验证 index 干净退出无报错。

Summary by Sourcery

Make the index process follow the daemon’s D-Bus lifecycle so daemon failures are reflected promptly in the file manager.

Bug Fixes:

  • Ensure the index process exits and releases its D-Bus names when the indexing daemon disappears, allowing file-manager error handling to activate instead of remaining stuck in an updating state.

Enhancements:

  • Bind the index lifecycle to the daemon by watching the daemon’s system-bus D-Bus name and supporting clean reactivation after daemon recovery.

1. Root cause: index did not monitor the daemon's D-Bus name on
   the system bus; when daemon stopped, index kept running with
   its session-bus names registered, so dde-file-manager's
   QDBusServiceWatcher never fired serviceUnregistered and UI
   stayed "updating" instead of showing an error state
2. Fix: add QDBusServiceWatcher in main() watching org.deepin.Anything
   on the system bus for serviceUnregistered; on trigger, unregister
   index session-bus names and exit so dde-file-manager detects the
   name unregistration and switches UI to error state
3. Impact: index now exits when daemon disappears; on daemon recovery
   index is D-Bus-activated again and reconnects; normal shutdown
   unaffected (SIGTERM handler and atexit already call
   unregisterIndexServices)

Log: index exits when daemon stops, UI shows error state

Influence:
1. Test: start daemon + index, stop daemon, verify index exits and
   UI shows error state
2. Test: restart daemon, verify index restarts and reconnects
3. Test: normal restart/shutdown unaffected

fix: 通过D-Bus名称监视绑定index与daemon生命周期

1. 根因:index未监视daemon在系统总线的D-Bus名称,daemon停止时
   index仍保持会话总线名称注册,文管的QDBusServiceWatcher不触发
   serviceUnregistered,UI仍显示"更新中"而非错误状态
2. 方案:在main()中添加QDBusServiceWatcher监听系统总线上
   org.deepin.Anything的serviceUnregistered信号,触发时注销index
   会话总线名称并退出,文管检测到名称注销后切换UI为错误状态
3. 影响:daemon消失时index退出;daemon恢复时index通过D-Bus激活
   重启并重连;正常关机重启不受影响

Log: daemon停止时index退出,UI显示错误状态

Influence:
1. 测试:启动daemon和index,停止daemon,验证index退出且UI显示错误
2. 测试:重启daemon,验证index重启并重连
3. 测试:正常重启关机不受影响

PMS: BUG-377687
@Johnson-zs
Johnson-zs marked this pull request as draft November 10, 2026 00:58
@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Add a system-bus watcher that unregisters the index services and exits when the daemon disappears.

  • src/index/main.cpp L20-22, L135-155

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

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

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 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.

Hey - I've found 4 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/index/main.cpp" line_range="142" />
<code_context>
+    // existing QDBusServiceWatcher detects the index name unregistration and
+    // switches the UI to an error state.  When the daemon restarts the index
+    // is D-Bus-activated again and re-establishes the VFS monitor connection.
+    auto *daemonWatcher = new QDBusServiceWatcher(
+        QStringLiteral("org.deepin.Anything"),
+        QDBusConnection::systemBus(),
</code_context>
<issue_to_address>
**Index stays available without daemon**

When the index is activated while the daemon is absent, or the daemon disappears after service registration but before watcher installation, `daemonWatcher` only reports later unregistrations, so `main()` leaves the session-bus names registered. After an initial `initDispatcher()` failure, its watcher is also deleted and the index cannot reconnect, leaving the index presented as available and the file manager stuck updating.

Check whether the daemon currently owns the name when setting up the watcher, and handle an unregistration that races with watcher installation.

Also at `src/index/main.cpp:143-146`.
</issue_to_address>

### Comment 2
<location path="src/index/main.cpp" line_range="152" />
<code_context>
+        qWarning() << "deepin-anything-index: daemon (org.deepin.Anything) "
+                      "disappeared on system bus, exiting to maintain "
+                      "lifecycle binding";
+        anything_index::unregisterIndexServices();
+        QCoreApplication::exit(0);
+    });
</code_context>
<issue_to_address>
**Outage changes stay unindexed**

When the daemon disappears while no indexing work is pending and files change before the index starts again, `unregisterIndexServices()` follows normal cleanup, leaving the index clean and removing the sentinel. On reactivation, `handleSilentStart()` skips the compensating update, so outage changes remain absent or stale in search results.

Preserve a dirty state or sentinel on this exit so reactivation runs the compensating update.
</issue_to_address>

### Comment 3
<location path="src/index/main.cpp" line_range="154" />
<code_context>
+                      "lifecycle binding";
+        anything_index::unregisterIndexServices();
+        QCoreApplication::exit(0);
+    });
+
     // Safety net: ensure cleanup runs even when the process exits via C exit()
</code_context>
<issue_to_address>
**Sibling services cannot reactivate index**

When the daemon recovers and a client requests OCR or filename indexing before making a request to TextIndex, the index exits successfully after daemon unregistration, but only `org.deepin.Filemanager.TextIndex` has a D-Bus activation file. If a client next requests only the OCR or filename service, D-Bus cannot reactivate the index, so that request fails even after the daemon has recovered.

Provide D-Bus activation entries for the OCR and filename service names, or otherwise ensure requests to those names start the index process.
</issue_to_address>

### Comment 4
<location path="src/index/main.cpp" line_range="142" />
<code_context>
+    // existing QDBusServiceWatcher detects the index name unregistration and
+    // switches the UI to an error state.  When the daemon restarts the index
+    // is D-Bus-activated again and re-establishes the VFS monitor connection.
+    auto *daemonWatcher = new QDBusServiceWatcher(
+        QStringLiteral("org.deepin.Anything"),
+        QDBusConnection::systemBus(),
</code_context>
<issue_to_address>
**Variable naming rule is broken**

The new `daemonWatcher` local uses camelCase, violating the required snake_case naming convention.

Rename the local to `daemon_watcher`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/index/main.cpp
// existing QDBusServiceWatcher detects the index name unregistration and
// switches the UI to an error state. When the daemon restarts the index
// is D-Bus-activated again and re-establishes the VFS monitor connection.
auto *daemonWatcher = new QDBusServiceWatcher(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Index stays available without daemon

When the index is activated while the daemon is absent, or the daemon disappears after service registration but before watcher installation, daemonWatcher only reports later unregistrations, so main() leaves the session-bus names registered. After an initial initDispatcher() failure, its watcher is also deleted and the index cannot reconnect, leaving the index presented as available and the file manager stuck updating.

Check whether the daemon currently owns the name when setting up the watcher, and handle an unregistration that races with watcher installation.

Also at src/index/main.cpp:143-146.

Prompt for AI agents
In `src/index/main.cpp` at line 142:

**Index stays available without daemon**

When the index is activated while the daemon is absent, or the daemon disappears after service registration but before watcher installation, `daemonWatcher` only reports later unregistrations, so `main()` leaves the session-bus names registered. After an initial `initDispatcher()` failure, its watcher is also deleted and the index cannot reconnect, leaving the index presented as available and the file manager stuck updating.

Check whether the daemon currently owns the name when setting up the watcher, and handle an unregistration that races with watcher installation.

Also at `src/index/main.cpp:143-146`.

Comment thread src/index/main.cpp
qWarning() << "deepin-anything-index: daemon (org.deepin.Anything) "
"disappeared on system bus, exiting to maintain "
"lifecycle binding";
anything_index::unregisterIndexServices();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Outage changes stay unindexed

When the daemon disappears while no indexing work is pending and files change before the index starts again, unregisterIndexServices() follows normal cleanup, leaving the index clean and removing the sentinel. On reactivation, handleSilentStart() skips the compensating update, so outage changes remain absent or stale in search results.

Preserve a dirty state or sentinel on this exit so reactivation runs the compensating update.

Prompt for AI agents
In `src/index/main.cpp` at line 152:

**Outage changes stay unindexed**

When the daemon disappears while no indexing work is pending and files change before the index starts again, `unregisterIndexServices()` follows normal cleanup, leaving the index clean and removing the sentinel. On reactivation, `handleSilentStart()` skips the compensating update, so outage changes remain absent or stale in search results.

Preserve a dirty state or sentinel on this exit so reactivation runs the compensating update.

Comment thread src/index/main.cpp
"lifecycle binding";
anything_index::unregisterIndexServices();
QCoreApplication::exit(0);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Sibling services cannot reactivate index

When the daemon recovers and a client requests OCR or filename indexing before making a request to TextIndex, the index exits successfully after daemon unregistration, but only org.deepin.Filemanager.TextIndex has a D-Bus activation file. If a client next requests only the OCR or filename service, D-Bus cannot reactivate the index, so that request fails even after the daemon has recovered.

Provide D-Bus activation entries for the OCR and filename service names, or otherwise ensure requests to those names start the index process.

Prompt for AI agents
In `src/index/main.cpp` at line 154:

**Sibling services cannot reactivate index**

When the daemon recovers and a client requests OCR or filename indexing before making a request to TextIndex, the index exits successfully after daemon unregistration, but only `org.deepin.Filemanager.TextIndex` has a D-Bus activation file. If a client next requests only the OCR or filename service, D-Bus cannot reactivate the index, so that request fails even after the daemon has recovered.

Provide D-Bus activation entries for the OCR and filename service names, or otherwise ensure requests to those names start the index process.

Comment thread src/index/main.cpp
// existing QDBusServiceWatcher detects the index name unregistration and
// switches the UI to an error state. When the daemon restarts the index
// is D-Bus-activated again and re-establishes the VFS monitor connection.
auto *daemonWatcher = new QDBusServiceWatcher(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚪ Low · Variable naming rule is broken

The new daemonWatcher local uses camelCase, violating the required snake_case naming convention.

Rename the local to daemon_watcher.

Prompt for AI agents
In `src/index/main.cpp` at line 142:

**Variable naming rule is broken**

The new `daemonWatcher` local uses camelCase, violating the required snake_case naming convention.

Rename the local to `daemon_watcher`.

From AGENTS.md L59:

- Variables: `snake_case` (e.g., `config_ptr`, `event_handler`)

@Johnson-zs
Johnson-zs marked this pull request as ready for review November 10, 2026 02:22
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交通过 QDBusServiceWatcher 将索引进程的生命周期绑定到守护进程的 D-Bus 名称,实现简洁、注释完善、逻辑正确。语法逻辑、代码质量、代码性能、代码安全四维度均未发现问题,无安全漏洞。OCR 因代码克隆环境问题未能完成,不影响 AI 四维度分析结论。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例


本报告由 AI 代码审查工具自动生成

@wangrong1069 wangrong1069 left a comment

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

@Johnson-zs

Copy link
Copy Markdown
Author

/merge

@deepin-bot
deepin-bot Bot merged commit a26108f into linuxdeepin:develop/snipe Nov 10, 2026
16 checks passed
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