Skip to content

fix: prevent printer query UT from hanging on sudo - #984

Open
dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:fix/ut-drivermanager-printer-hang
Open

dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:fix/ut-drivermanager-printer-hang

Conversation

@dengzhongyuan365-dev

@dengzhongyuan365-dev dengzhongyuan365-dev commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

问题

DriverManagerExtra_UT.QueryHelpers_NonexistentInputs_ReturnEmptyOrFalse 会真实执行 sudo dpkg -s。流水线 Worker 没有交互终端,sudo 会等待认证,而 QProcess::waitForFinished(-1) 无限等待,导致 UT 卡死。

修改

  • printerHasInstalled() 改用无特权的 dpkg-query -s,避免触发 sudo 认证。
  • 查询增加 5 秒超时;超时后终止进程并返回 false。
  • UT 对 QProcess 打桩,不再访问真实系统命令。
  • 新增包不存在和查询超时两个回归用例,并断言调用目标为 dpkg-query。

验证

在 70 节点使用现有 Qt6 构建环境增量编译 deepin-devicemanager-server-test,执行:

DriverManagerExtra_UT.QueryHelpers_NonexistentInputs_ReturnEmptyOrFalse
DriverManagerExtra_UT.PrinterQuery_NonexistentPackage_ReturnsFalse
DriverManagerExtra_UT.PrinterQuery_Timeout_ReturnsFalse

结果: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:

  • Prevent printer package queries from hanging in non-interactive environments by avoiding sudo authentication and enforcing a timeout.

Enhancements:

  • Validate successful package queries using normal process termination, exit status, and installed-package output.

Tests:

  • Stub QProcess interactions in printer-query tests and add regression coverage for nonexistent packages and query timeouts.

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
@deepin-ci-robot

Copy link
Copy Markdown

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

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 commented Nov 20, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Replace the sudo-based printer package check with a bounded dpkg-query call that returns false on timeout or unsuccessful execution.

  • deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/drivermanager.cpp L69-70, L695-706

Tests

Stub QProcess and add regression coverage for the dpkg-query target, missing-package result, and timeout path.

  • test/deepin-devicemanager-server/src/DriverControl/ut_DriverManager.cpp L231-236, L244-248, L602-627

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

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

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

if (!p.waitForFinished(PRINTER_QUERY_TIMEOUT_MS)) {
p.kill();
p.waitForFinished(1000);
return false;

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

Comment on lines +698 to +699
p.kill();
p.waitForFinished(1000);

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 · 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"));

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 · 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)

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

Comment on lines +244 to +246
bool ut_stubQProcessWaitTimedOut(QProcess *, int)
{
return false;

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

@deepin-bot

deepin-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.77
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #990

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.

2 participants