Repository navigation
feat: release single instance lock on aboutToQuit #410
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) | ||
|
|
||
| set(BIN single-instance-demo) | ||
| set(TARGET ${BIN}${DTK_VERSION_MAJOR}) | ||
|
|
||
| add_executable(${TARGET} | ||
| main.cpp | ||
| ) | ||
|
|
||
| target_link_libraries(${TARGET} PRIVATE | ||
| Qt${QT_VERSION_MAJOR}::Widgets | ||
| ${LIB_NAME} | ||
| ) | ||
|
|
||
| set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME ${BIN}) | ||
|
|
||
| install(TARGETS ${TARGET} DESTINATION "${TOOL_INSTALL_DIR}") |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| #include <QApplication> | ||
| #include <QThread> | ||
| #include <QTimer> | ||
| #include <QDebug> | ||
| #include <DGuiApplicationHelper> | ||
|
|
||
| DGUI_USE_NAMESPACE | ||
|
|
||
| // 演示场景: | ||
| // 1. 实例 A 启动,成为单实例 primary | ||
| // 2. 3 秒后 A 触发退出:aboutToQuit 发出(dtkgui 释放单实例锁和 server), | ||
| // 但 main() 中模拟"有线程卡住",进程继续存活 10 秒才真正退出 | ||
| // 3. 在这 10 秒内启动实例 B: | ||
| // - 若在 aboutToQuit 之前启动 → B 检测到 A 存活,正常退出(单实例保护) | ||
| // - 若在 aboutToQuit 之后启动 → B 应能成功成为新的 primary(本次修复的目标) | ||
|
|
||
| int main(int argc, char *argv[]) | ||
| { | ||
| QApplication app(argc, argv); | ||
|
|
||
| if (!DGuiApplicationHelper::setSingleInstance("single-instance-demo")) { | ||
| qInfo() << "[demo] another instance is running, exit now. pid =" << app.applicationPid(); | ||
| return 0; | ||
| } | ||
|
|
||
| qInfo() << "[demo] primary started. pid =" << app.applicationPid(); | ||
|
|
||
| // 可通过参数控制退出前的存活时间,默认 3 秒后自动退出 | ||
| int lifetime = 3000; | ||
| if (app.arguments().contains("--lifetime")) { | ||
| int pos = app.arguments().indexOf("--lifetime"); | ||
| if (pos + 1 < app.arguments().size()) | ||
| lifetime = app.arguments().at(pos + 1).toInt(); | ||
| } | ||
|
|
||
| // 模拟退出阶段有线程卡住:事件循环退出后进程并不会立即结束 | ||
| const int stuckSeconds = 10; | ||
| QObject::connect(&app, &QCoreApplication::aboutToQuit, &app, [stuckSeconds] { | ||
| qInfo() << "[demo] aboutToQuit: single instance released," | ||
| << "process will stay alive for" << stuckSeconds << "s (simulating stuck thread)"; | ||
| }); | ||
|
|
||
| QTimer::singleShot(lifetime, &app, &QCoreApplication::quit); | ||
| const int ret = app.exec(); | ||
|
|
||
| // 模拟卡住的清理逻辑阻塞进程完全退出 | ||
| QThread::sleep(stuckSeconds); | ||
| qInfo() << "[demo] stuck cleanup finished, real exit now."; | ||
|
|
||
| return ret; | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.