Repository navigation
fix: prevent printer query UT from hanging on sudo - #984
dengzhongyuan365-dev wants to merge 1 commit into
Conversation
1. Replace the privileged dpkg query with dpkg-query to avoid sudo prompts. 2. Bound printer package queries with a five-second timeout and kill stalled processes. 3. Stub QProcess in unit tests and cover both missing-package and timeout paths. Log: Prevent printer package queries from blocking in non-interactive sessions. Influence: 1. Run the DriverManager UT and verify no sudo prompt appears. 2. Verify missing printer packages return false without blocking. 3. Verify a stalled query is terminated and returns false after the timeout. fix: 修复打印机查询单元测试因 sudo 卡死 1. 使用无特权的 dpkg-query 查询打印机包,避免 sudo 等待认证。 2. 为打印机包查询增加五秒超时,超时后终止进程并返回失败。 3. 单元测试中打桩 QProcess,并覆盖包不存在和查询超时场景。 Log: 修复无交互环境下打印机包查询阻塞的问题。 Influence: 1. 运行 DriverManager 单元测试,确认不再出现 sudo 提示。 2. 验证查询不存在的打印机包会立即返回 false。 3. 验证查询超时后会终止进程并返回 false。 Log: Prevent printer package queries from blocking in non-interactive sessions. Issue: V-9036 multica: 01a1f470-4ef2-7be7-9d66-da17990fdc68
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: dengzhongyuan365-dev 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 |
Reviewer's GuideImplementationReplace the sudo-based printer package check with a bounded dpkg-query call that returns false on timeout or unsuccessful execution.
TestsStub QProcess and add regression coverage for the dpkg-query target, missing-package result, and timeout path.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 5 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp" line_range="700" />
<code_context>
+ if (!p.waitForFinished(PRINTER_QUERY_TIMEOUT_MS)) {
+ p.kill();
+ p.waitForFinished(1000);
+ return false;
+ }
</code_context>
<issue_to_address>
**Uninstall reports false success**
When `dpkg-query` fails or times out while the package remains installed, `printerHasInstalled()` returns `false` for both an absent package and a failed query, so `unInstallPrinter()` treats the package as absent and reports successful removal; a timeout can also leave the requested operation incomplete, and `installPrinter()` reports failure after apt completes.
Distinguish a failed status query from a confirmed absent package, and do not report the requested operation as complete when the query fails.
</issue_to_address>
### Comment 2
<location path="deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp" line_range="698-699" />
<code_context>
+ // dpkg-query 只读查询,不需要 root 权限,避免 sudo 在无终端环境中等待认证。
+ p.start("dpkg-query", QStringList() << "-s" << packageName);
+ if (!p.waitForFinished(PRINTER_QUERY_TIMEOUT_MS)) {
+ p.kill();
+ p.waitForFinished(1000);
+ return false;
+ }
</code_context>
<issue_to_address>
**Unkillable query defeats timeout**
When `dpkg-query` remains in uninterruptible kernel I/O after the timeout and SIGKILL, the bounded wait can expire while the child remains alive; if it is stuck in uninterruptible kernel I/O, `kill()` cannot stop it. The stack `QProcess` is then destroyed while the child is still running, and its cleanup waits for the process, so the query can still block beyond the timeout.
Avoid synchronously destroying the `QProcess` while its child may still be running; manage termination without an unbounded wait in the caller.
</issue_to_address>
### Comment 3
<location path="test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp" line_range="626" />
<code_context>
+
+ g_stubQProcessProgram.clear();
+ DriverManager mgr;
+ EXPECT_FALSE(mgr.printerHasInstalled("ut-timeout-pkg-t24xyz"));
+ EXPECT_EQ(g_stubQProcessProgram, QStringLiteral("dpkg-query"));
}
</code_context>
<issue_to_address>
**Timeout test misses failed termination**
When the timeout test is used to verify stalled-process cleanup, the start stub launches no child, and the timeout test checks only the return value and program name, not whether `kill()` was called; the test passes even if termination is removed.
Make the timeout test verify that the process termination call occurs.
Also at `test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp:606-614`.
</issue_to_address>
### Comment 4
<location path="test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp" line_range="604" />
<code_context>
- // sudo dpkg -s 查询不存在的包:stdout 无内容 → false(无系统副作用)
+}
+
+TEST_F(DriverManagerExtra_UT, PrinterQuery_NonexistentPackage_ReturnsFalse)
+{
+ Stub stub;
</code_context>
<issue_to_address>
**Missing-package test does not model query result**
When the missing-package test runs with its current process stubs, the test's start stub launches no process and the test supplies no output or exit status, so the empty output makes the assertion false regardless of how a real missing-package response is handled.
Have the stub provide a representative `dpkg-query` missing-package result and exit status, then assert the method handles that response correctly.
</issue_to_address>
### Comment 5
<location path="test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp" line_range="244-246" />
<code_context>
return true;
}
+bool ut_stubQProcessWaitTimedOut(QProcess *, int)
+{
+ return false;
+}
+
</code_context>
<issue_to_address>
**Timeout test accepts unbounded waits**
When `printerHasInstalled` is changed to wait indefinitely, `ut_stubQProcessWaitTimedOut` ignores its timeout argument and returns `false` immediately, so `PrinterQuery_Timeout_ReturnsFalse` at line 626 still passes if `printerHasInstalled` regresses to an unbounded wait that can hang real callers.
Record the timeout passed to the stub and assert that the test receives the expected finite timeout.
</issue_to_address>| if (!p.waitForFinished(PRINTER_QUERY_TIMEOUT_MS)) { | ||
| p.kill(); | ||
| p.waitForFinished(1000); | ||
| return false; |
There was a problem hiding this comment.
🟡 Medium · Uninstall reports false success
When dpkg-query fails or times out while the package remains installed, printerHasInstalled() returns false for both an absent package and a failed query, so unInstallPrinter() treats the package as absent and reports successful removal; a timeout can also leave the requested operation incomplete, and installPrinter() reports failure after apt completes.
Distinguish a failed status query from a confirmed absent package, and do not report the requested operation as complete when the query fails.
Prompt for AI agents
In `deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp` at line 700:
**Uninstall reports false success**
When `dpkg-query` fails or times out while the package remains installed, `printerHasInstalled()` returns `false` for both an absent package and a failed query, so `unInstallPrinter()` treats the package as absent and reports successful removal; a timeout can also leave the requested operation incomplete, and `installPrinter()` reports failure after apt completes.
Distinguish a failed status query from a confirmed absent package, and do not report the requested operation as complete when the query fails.| p.kill(); | ||
| p.waitForFinished(1000); |
There was a problem hiding this comment.
🟡 Medium · Unkillable query defeats timeout
When dpkg-query remains in uninterruptible kernel I/O after the timeout and SIGKILL, the bounded wait can expire while the child remains alive; if it is stuck in uninterruptible kernel I/O, kill() cannot stop it. The stack QProcess is then destroyed while the child is still running, and its cleanup waits for the process, so the query can still block beyond the timeout.
Avoid synchronously destroying the QProcess while its child may still be running; manage termination without an unbounded wait in the caller.
Prompt for AI agents
In `deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp` at lines 698-699:
**Unkillable query defeats timeout**
When `dpkg-query` remains in uninterruptible kernel I/O after the timeout and SIGKILL, the bounded wait can expire while the child remains alive; if it is stuck in uninterruptible kernel I/O, `kill()` cannot stop it. The stack `QProcess` is then destroyed while the child is still running, and its cleanup waits for the process, so the query can still block beyond the timeout.
Avoid synchronously destroying the `QProcess` while its child may still be running; manage termination without an unbounded wait in the caller.
|
|
||
| g_stubQProcessProgram.clear(); | ||
| DriverManager mgr; | ||
| EXPECT_FALSE(mgr.printerHasInstalled("ut-timeout-pkg-t24xyz")); |
There was a problem hiding this comment.
🟡 Medium · Timeout test misses failed termination
When the timeout test is used to verify stalled-process cleanup, the start stub launches no child, and the timeout test checks only the return value and program name, not whether kill() was called; the test passes even if termination is removed.
Make the timeout test verify that the process termination call occurs.
Also at test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp:606-614.
Prompt for AI agents
In `test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp` at line 626:
**Timeout test misses failed termination**
When the timeout test is used to verify stalled-process cleanup, the start stub launches no child, and the timeout test checks only the return value and program name, not whether `kill()` was called; the test passes even if termination is removed.
Make the timeout test verify that the process termination call occurs.
Also at `test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp:606-614`.| // sudo dpkg -s 查询不存在的包:stdout 无内容 → false(无系统副作用) | ||
| } | ||
|
|
||
| TEST_F(DriverManagerExtra_UT, PrinterQuery_NonexistentPackage_ReturnsFalse) |
There was a problem hiding this comment.
🟡 Medium · Missing-package test does not model query result
When the missing-package test runs with its current process stubs, the test's start stub launches no process and the test supplies no output or exit status, so the empty output makes the assertion false regardless of how a real missing-package response is handled.
Have the stub provide a representative dpkg-query missing-package result and exit status, then assert the method handles that response correctly.
Prompt for AI agents
In `test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp` at line 604:
**Missing-package test does not model query result**
When the missing-package test runs with its current process stubs, the test's start stub launches no process and the test supplies no output or exit status, so the empty output makes the assertion false regardless of how a real missing-package response is handled.
Have the stub provide a representative `dpkg-query` missing-package result and exit status, then assert the method handles that response correctly.| bool ut_stubQProcessWaitTimedOut(QProcess *, int) | ||
| { | ||
| return false; |
There was a problem hiding this comment.
🟡 Medium · Timeout test accepts unbounded waits
When printerHasInstalled is changed to wait indefinitely, ut_stubQProcessWaitTimedOut ignores its timeout argument and returns false immediately, so PrinterQuery_Timeout_ReturnsFalse at line 626 still passes if printerHasInstalled regresses to an unbounded wait that can hang real callers.
Record the timeout passed to the stub and assert that the test receives the expected finite timeout.
Prompt for AI agents
In `test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp` at lines 244-246:
**Timeout test accepts unbounded waits**
When `printerHasInstalled` is changed to wait indefinitely, `ut_stubQProcessWaitTimedOut` ignores its timeout argument and returns `false` immediately, so `PrinterQuery_Timeout_ReturnsFalse` at line 626 still passes if `printerHasInstalled` regresses to an unbounded wait that can hang real callers.
Record the timeout passed to the stub and assert that the test receives the expected finite timeout.|
TAG Bot New tag: 6.0.77 |
问题
DriverManagerExtra_UT.QueryHelpers_NonexistentInputs_ReturnEmptyOrFalse会真实执行sudo dpkg -s。流水线 Worker 没有交互终端,sudo会等待认证,而QProcess::waitForFinished(-1)无限等待,导致 UT 卡死。修改
printerHasInstalled()改用无特权的dpkg-query -s,避免触发 sudo 认证。false。QProcess打桩,不再访问真实系统命令。dpkg-query。验证
在 70 节点使用现有 Qt6 构建环境增量编译
deepin-devicemanager-server-test,执行:结果:3/3 通过,未触发 sudo。
Summary by Sourcery
Prevent printer installation queries from hanging by using a non-privileged package query with bounded execution and regression coverage.
Bug Fixes:
Enhancements:
Tests: