Skip to content

fix: prevent dtkcore from starting session bus in backend service#202

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
wangrong1069:pr0422
Apr 22, 2026
Merged

fix: prevent dtkcore from starting session bus in backend service#202
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
wangrong1069:pr0422

Conversation

@wangrong1069
Copy link
Copy Markdown
Contributor

@wangrong1069 wangrong1069 commented Apr 22, 2026

Unset DISPLAY environment variable before DLogManager initialization to avoid dtkcore auto-starting a D-Bus session bus, which is unnecessary for the backend service.

Summary by Sourcery

Bug Fixes:

  • Unset the DISPLAY environment variable before initializing DLogManager to avoid auto-starting a D-Bus session bus in the backend service process.

Unset DISPLAY environment variable before DLogManager initialization
to avoid dtkcore auto-starting a D-Bus session bus, which is
unnecessary for the backend service.
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 22, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Unsets the DISPLAY environment variable in the backend service’s main entrypoint before initializing Dtk::Core::DLogManager to prevent dtkcore from starting an unnecessary D-Bus session bus.

Sequence diagram for backend service startup with DISPLAY unset

sequenceDiagram
    participant BackendServiceMain as BackendService_main
    participant QEnv as QtEnvironment
    participant DLogManager as DtkCore_DLogManager
    participant SessionBus as DBusSessionBus

    BackendServiceMain->>QEnv: qputenv(PATH, updatedPath)
    BackendServiceMain->>QEnv: qunsetenv(DISPLAY)
    note right of QEnv: DISPLAY removed from environment

    BackendServiceMain->>DLogManager: registerConsoleAppender()
    alt DISPLAY is unset
        DLogManager--x SessionBus: do not auto start session bus
    else DISPLAY is set (previous behavior)
        DLogManager->>SessionBus: auto start session bus
    end
Loading

File-Level Changes

Change Details Files
Ensure backend service does not trigger a D-Bus session bus via Dtk::Core::DLogManager initialization.
  • Insert a comment explaining that DISPLAY is unset to avoid dtkcore starting a session bus when registering the console appender.
  • Call qunsetenv("DISPLAY") immediately after configuring PATH and before any Dtk::Core::DLogManager usage to clear the DISPLAY environment variable for the process.
service/main.cpp

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

@github-actions
Copy link
Copy Markdown

  • 检测到敏感词unset变动
详情
    {
    "unset": {
        "service/main.cpp": {
            "b": [
                "    qunsetenv(\"DISPLAY\");"
            ]
        }
    }
}

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码通过 git diff 展示了对 service/main.cpp 的修改。主要改动是在设置环境变量 PATH 之后,添加了一行 qunsetenv("DISPLAY");,并附带注释说明了原因。

以下是对这段代码的审查意见,分为语法逻辑、代码质量、代码性能和代码安全四个方面:

1. 语法逻辑

  • 逻辑合理性:代码逻辑在特定场景下是合理的。根据注释,目的是为了避免 Dtk::Core::DLogManager::registerConsoleAppender() 在初始化时触发 D-Bus 会话总线(Session Bus)的启动。在无图形界面(Headless)或后台服务中,通常不希望依赖 DISPLAY 环境变量或会话总线。
  • 潜在风险:如果在后续代码中,有其他逻辑(例如调用 GUI 相关的库或依赖 X11 的功能)依赖于 DISPLAY 环境变量,这行代码会导致后续功能异常(如无法打开窗口或调用图形库失败)。需要确保该服务确实是纯后台逻辑,不依赖图形环境。

2. 代码质量

  • 注释质量:添加的注释非常清晰,解释了"为什么"(Why)要这样做,而不仅仅是"做了什么"(What)。这有助于后续维护者理解这个看似奇怪的修改。
  • 上下文位置:将此操作放在 PATH 设置之后、主业务逻辑(如参数解析 argc < 2)之前是合适的,属于环境初始化阶段。

3. 代码性能

  • 性能影响qunsetenv 是一个非常轻量级的系统调用,对程序启动性能几乎没有负面影响。相反,通过避免不必要的 D-Bus 会话总线连接,可能会略微提升服务启动速度,并减少资源占用。

4. 代码安全

  • 安全性:从安全角度看,移除 DISPLAY 环境变量对于后台服务来说是一个好习惯。它可以防止某些试图利用 X11 认证机制或通过 X11 服务器进行交互的潜在攻击面(例如如果服务被攻陷,攻击者无法轻易利用 X11 转发进行截屏或注入输入)。
  • 副作用:需注意,如果该服务后续通过 system()popen() 启动子进程,子进程也会继承这个没有 DISPLAY 的环境。如果子进程恰好需要图形界面,将会失败。

改进建议

  1. 确认依赖范围
    请务必确认整个 service 模块及其依赖的所有动态库(不仅仅是 DTK)都不依赖 DISPLAY。如果后续引入了依赖 DISPLAY 的第三方库,这里会成为隐患。

  2. 条件判断(可选)
    如果这个服务既可能作为纯后台服务运行,也可能在开发阶段作为调试进程运行(需要日志输出到图形界面),可以考虑添加判断逻辑。例如:

    // 仅在非调试模式下或明确不需要图形界面的情况下取消 DISPLAY
    #ifndef QT_DEBUG
    // 避免在调用 Dtk::Core::DLogManager::registerConsoleAppender() 时 dtkcore 启动会话总线
    qunsetenv("DISPLAY");
    #endif

    注意:这取决于你的具体部署策略,如果确定永远是后台服务,则无需添加此判断。

  3. 防御性编程
    如果 DTK 的行为在未来版本发生变化,或者 qunsetenv 失败(虽然极少见),确保程序有相应的错误处理机制。不过对于 unsetenv 来说,通常不需要检查返回值,因为即使失败,最坏的情况也就是 D-Bus 依然被启动,属于"降级运行"。

总结

这是一个针对特定问题(防止 D-Bus 会话总线自动启动)的有效且合理的补丁。它提高了服务在无图形环境下的兼容性和安全性。只要确保该服务后续不依赖 X11 图形功能,这个修改是可以接受的。

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider saving and restoring the original DISPLAY value after DLogManager initialization so that later code in this process can still use DISPLAY if needed.
  • If the intent is to affect only DLogManager initialization, wrapping that call in a small helper that temporarily unsets DISPLAY may make the scope and side effects of this workaround clearer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider saving and restoring the original DISPLAY value after DLogManager initialization so that later code in this process can still use DISPLAY if needed.
- If the intent is to affect only DLogManager initialization, wrapping that call in a small helper that temporarily unsets DISPLAY may make the scope and side effects of this workaround clearer.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: liujianqiang-niu, lzwind, wangrong1069

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

@wangrong1069
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented Apr 22, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit ba47f7c into linuxdeepin:master Apr 22, 2026
21 of 22 checks passed
@wangrong1069 wangrong1069 deleted the pr0422 branch April 22, 2026 07:18
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.

4 participants