Repository navigation
fix: auto-exit index service after idle timeout #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| #include "idleexitmanager.h" | ||
| #include "serviceentry.h" | ||
|
|
||
| #include <QCoreApplication> | ||
| #include <QDebug> | ||
| #include <QDBusMessage> | ||
| #include <QTimer> | ||
| #include <DConfig> | ||
|
|
||
| // qDBusAddSpyHook is a Qt DBus internal function exported from libQt6DBus. | ||
| // It registers a callback invoked for every D-Bus message that flows | ||
| // through the connection (both incoming and outgoing). The callback | ||
| // runs on the main thread, so touching the QTimer directly is safe. | ||
| // | ||
| // The symbol is not declared in any public Qt6 header, so we declare | ||
| // it here. It is a C++ function (not extern "C"), so the linker must | ||
| // resolve the mangled symbol _Z15qDBusAddSpyHookPFvRK12QDBusMessageE | ||
| // that libQt6DBus exports. Using extern "C" would search for the | ||
| // unmangled name and fail at link time. | ||
| Q_DECL_IMPORT void qDBusAddSpyHook(void (*)(const QDBusMessage &)); | ||
|
|
||
| namespace anything_index { | ||
|
|
||
| static constexpr char kAnythingAppId[] = "org.deepin.anything"; | ||
| static constexpr char kAnythingSchema[] = "org.deepin.anything"; | ||
| static constexpr char kIdleExitTimeoutKey[] = "idle_exit_timeout_minutes"; | ||
| static constexpr int kDefaultIdleExitTimeoutMinutes = 10; | ||
|
|
||
| static QTimer *g_idleTimer = nullptr; | ||
|
|
||
| static void idleSpyHook(const QDBusMessage &) | ||
| { | ||
| if (g_idleTimer) | ||
| g_idleTimer->start(); | ||
| } | ||
|
|
||
| void startIdleExitManager() | ||
| { | ||
| int timeoutMinutes = kDefaultIdleExitTimeoutMinutes; | ||
|
|
||
| const std::unique_ptr<Dtk::Core::DConfig> dconfig( | ||
| Dtk::Core::DConfig::create(QString::fromLatin1(kAnythingAppId), | ||
| QString::fromLatin1(kAnythingSchema))); | ||
| if (dconfig && dconfig->isValid()) { | ||
| timeoutMinutes = dconfig->value(QString::fromLatin1(kIdleExitTimeoutKey), | ||
| kDefaultIdleExitTimeoutMinutes).toInt(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Malformed setting disables auto-exit When the dconfig value is present but cannot be converted to an integer, a non-numeric configured value makes Check conversion success and use the default timeout with a warning when the value is invalid. Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at line 50:
**Malformed setting disables auto-exit**
When the dconfig value is present but cannot be converted to an integer, a non-numeric configured value makes `toInt()` return 0, which the following check treats as the explicit “disable auto-exit” setting. The service then stays running indefinitely instead of using the default timeout, with no warning about the invalid value.
Check conversion success and use the default timeout with a warning when the value is invalid. |
||
| if (timeoutMinutes < 0) { | ||
| qWarning() << "deepin-anything-index: invalid idle_exit_timeout_minutes" | ||
| << timeoutMinutes << ", using default" | ||
| << kDefaultIdleExitTimeoutMinutes; | ||
| timeoutMinutes = kDefaultIdleExitTimeoutMinutes; | ||
| } | ||
| } else { | ||
| qWarning() << "deepin-anything-index: failed to load dconfig for idle exit" | ||
| "timeout, using default" << kDefaultIdleExitTimeoutMinutes | ||
| << "minutes"; | ||
| } | ||
|
|
||
| if (timeoutMinutes == 0) { | ||
| qInfo() << "deepin-anything-index: idle exit disabled (timeout = 0)"; | ||
| return; | ||
| } | ||
|
|
||
| g_idleTimer = new QTimer; | ||
| g_idleTimer->setSingleShot(true); | ||
| g_idleTimer->setInterval(timeoutMinutes * 60 * 1000); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Configured idle timeout is not honored When Calculate the interval without signed-integer overflow and handle values outside the timer's supported range. Also at Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at line 70:
**Configured idle timeout is not honored**
When `idle_exit_timeout_minutes` is configured above 35,791, the signed `timeoutMinutes * 60 * 1000` calculation overflows before `QTimer::setInterval()` receives it, so the timer uses an unintended interval and the service exits at the wrong time or fails to time out as configured.
Calculate the interval without signed-integer overflow and handle values outside the timer's supported range.
Also at `src/index/idleexitmanager.cpp:71`. |
||
|
|
||
| QObject::connect(g_idleTimer, &QTimer::timeout, []() { | ||
| qInfo() << "deepin-anything-index: idle timeout reached," | ||
| "exiting to release resources (D-Bus activation will" | ||
| "restart on next request)"; | ||
| unregisterIndexServices(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Active indexing work is interrupted When an indexing task runs longer than the configured timeout without D-Bus activity, Defer the idle exit while indexing work is active. Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at line 76:
**Active indexing work is interrupted**
When an indexing task runs longer than the configured timeout without D-Bus activity, `unregisterIndexServices()` runs cleanup, which stops the task and marks unfinished work dirty, so indexing is aborted and needs recovery on the next startup.
Defer the idle exit while indexing work is active.There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · OCR and filename requests fail after exit When the process has exited and the next request targets only the OCR or filename service, the installed D-Bus activation entry names only TextIndex, so a request to the OCR or filename service cannot activate the process and fails with a missing-service error. Add D-Bus activation entries for the OCR and filename service names, or otherwise ensure requests to those names activate the process. Also at Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at line 76:
**OCR and filename requests fail after exit**
When the process has exited and the next request targets only the OCR or filename service, the installed D-Bus activation entry names only TextIndex, so a request to the OCR or filename service cannot activate the process and fails with a missing-service error.
Add D-Bus activation entries for the OCR and filename service names, or otherwise ensure requests to those names activate the process.
Also at `src/index/idleexitmanager.cpp:77`, `src/index/idleexitmanager.h:20`, `src/index/main.cpp:139`.There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Idle shutdown appears as backend failure When the idle timer expires while dde-file-manager is watching the index service, the service unregisters its names on a normal idle exit, so dde-file-manager’s service watcher treats the shutdown as an index-backend failure and switches the UI to an error state instead of treating the service as available for on-demand activation. Make the client recover cleanly from intentional idle unregistration, or change the shutdown/availability signaling so idle exit is not reported as a backend failure. Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at line 76:
**Idle shutdown appears as backend failure**
When the idle timer expires while dde-file-manager is watching the index service, the service unregisters its names on a normal idle exit, so dde-file-manager’s service watcher treats the shutdown as an index-backend failure and switches the UI to an error state instead of treating the service as available for on-demand activation.
Make the client recover cleanly from intentional idle unregistration, or change the shutdown/availability signaling so idle exit is not reported as a backend failure. |
||
| QCoreApplication::quit(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · TextIndex activation cannot relaunch binary When the process has auto-exited and a caller next requests the TextIndex service, after this code quits the process, a TextIndex request asks D-Bus to activate Change the D-Bus activation executable path to the installed libexec path, or install the executable at the path named by Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at line 77:
**TextIndex activation cannot relaunch binary**
When the process has auto-exited and a caller next requests the TextIndex service, after this code quits the process, a TextIndex request asks D-Bus to activate `/usr/bin/deepin-anything-index`, but the package installs the executable under `/usr/libexec/deepin/deepin-anything-index`. Activation fails, leaving the index service unavailable.
Change the D-Bus activation executable path to the installed libexec path, or install the executable at the path named by `Exec`. |
||
| }); | ||
|
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Medium · Missed changes leave index stale When filesystem changes occur while monitoring is enabled but no D-Bus activity reaches the service for the configured timeout, Before an idle exit, persist a dirty/recovery state whenever monitoring was enabled so the next activation reconciles changes missed during downtime. Prompt for AI agentsIn `src/index/idleexitmanager.cpp` at lines 76-78:
**Missed changes leave index stale**
When filesystem changes occur while monitoring is enabled but no D-Bus activity reaches the service for the configured timeout, `unregisterIndexServices()` stops filesystem monitoring, and a normal cleanup with no unfinished task leaves the index marked clean. On restart, `handleSilentStart()` skips the global update for a clean index, so files changed while the service was stopped are absent or stale in search results.
Before an idle exit, persist a dirty/recovery state whenever monitoring was enabled so the next activation reconciles changes missed during downtime. |
||
|
|
||
| qDBusAddSpyHook(idleSpyHook); | ||
| g_idleTimer->start(); | ||
| } | ||
|
|
||
| } // namespace anything_index | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| #ifndef IDLEEXITMANAGER_H | ||
| #define IDLEEXITMANAGER_H | ||
|
|
||
| namespace anything_index { | ||
|
|
||
| // Registers a D-Bus spy hook that restarts an idle timer on every D-Bus | ||
| // message. When the timer expires (no D-Bus activity for the configured | ||
| // number of minutes), unregisters index services and quits the application. | ||
| // | ||
| // The idle timeout is read from the dconfig key "idle_exit_timeout_minutes" | ||
| // in the org.deepin.anything schema (default: 10 minutes). A value of 0 | ||
| // disables the auto-exit feature entirely. | ||
| // | ||
| // The service is D-Bus-activated (its .service file points at | ||
| // deepin-anything-index), so exiting when idle is safe: the next D-Bus | ||
| // call to any index service name will automatically restart the process. | ||
| // | ||
| // dde-file-manager's AbstractIndexController::keepBackendAlive polls | ||
| // IsEnabled every 5 minutes while indexes are enabled, which resets the | ||
| // timer and keeps the service alive. When all indexes are disabled the | ||
| // keepalive stops, and the service exits after the idle timeout. | ||
| void startIdleExitManager(); | ||
|
|
||
| } // namespace anything_index | ||
|
|
||
| #endif // IDLEEXITMANAGER_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Activity from worker threads is missed
When a D-Bus message is sent from a thread other than the one that owns
g_idleTimer,idleSpyHookcallsQTimer::start()directly even though the hook runs synchronously for D-Bus messages and can be invoked by a thread sending a message; starting a main-thread timer from that thread is unsafe and does not reliably reset the timeout, so the service can exit despite D-Bus activity.Marshal the timer reset to the timer's owning thread, for example by queuing a slot or invocation on the timer.
Prompt for AI agents