Skip to content

fix: call init() synchronously in DccNetwork constructor#559

Open
caixr23 wants to merge 1 commit into
linuxdeepin:masterfrom
caixr23:bug-390967
Open

fix: call init() synchronously in DccNetwork constructor#559
caixr23 wants to merge 1 commit into
linuxdeepin:masterfrom
caixr23:bug-390967

Conversation

@caixr23

@caixr23 caixr23 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor
  1. Replace QMetaObject::invokeMethod with direct init() call to prevent potential crash caused by deferred initialization

PMS: TASK-390967

fix: 在 DccNetwork 构造函数中同步调用 init()

  1. 将 QMetaObject::invokeMethod 替换为直接调用 init(), 避免延迟初始化导致的潜在崩溃

PMS: TASK-390967

Summary by Sourcery

Bug Fixes:

  • Prevent potential crashes caused by deferred initialization of DccNetwork by removing the queued invokeMethod call and invoking init() directly in the constructor.

1. Replace QMetaObject::invokeMethod with direct init() call
   to prevent potential crash caused by deferred initialization

PMS: TASK-390967

fix: 在 DccNetwork 构造函数中同步调用 init()

1. 将 QMetaObject::invokeMethod 替换为直接调用 init(),
   避免延迟初始化导致的潜在崩溃

PMS: TASK-390967
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

我来分析这段代码的变更和潜在问题:

变更分析

  1. 代码将原来的 QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection) 改为了直接调用 init() 方法。

潜在问题

  1. 线程安全问题

    • 原代码使用 Qt::QueuedConnection 确保了 init() 方法会在事件循环中执行,这有助于避免潜在的线程同步问题。
    • 直接调用 init() 可能会导致在对象构造期间执行耗时操作,这可能影响构造函数的性能,并可能导致对象在完全初始化前就被使用。
  2. 执行顺序问题

    • 使用 QueuedConnection 可以确保 init() 在构造函数完成之后执行,这是更安全的方式。
    • 直接调用可能导致初始化代码在对象完全构造前执行,可能访问到未完全初始化的成员变量。
  3. 错误处理

    • 直接调用 init() 如果抛出异常,可能会导致构造函数异常,这可能导致对象构造失败但异常未被正确处理。

改进建议

  1. 保持异步初始化

    QMetaObject::invokeMethod(this, "init", Qt::QueuedConnection);

    这种方式可以确保 init() 在对象完全构造后执行,更加安全。

  2. 如果确实需要同步初始化

    try {
        init();
    } catch (const std::exception& e) {
        qWarning() << "Initialization failed:" << e.what();
        // 可以选择抛出异常或进行其他错误处理
    }
  3. 最佳实践

    • 对于可能耗时的初始化操作,最好在构造函数中只做必要的设置,将耗时操作推迟到对象完全构造后。
    • 如果确实需要在构造函数中执行初始化,确保不会抛出异常,或者妥善处理所有可能的异常。

总结

这个改动看似简化了代码,但实际上引入了潜在的风险。建议恢复使用 QMetaObject::invokeMethod 的方式,或者至少添加适当的错误处理机制。这样可以确保代码的健壮性和线程安全性。

@sourcery-ai

sourcery-ai Bot commented Jun 10, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR changes DccNetwork to call its init() method synchronously in the constructor instead of using a queued QMetaObject::invokeMethod, ensuring immediate initialization and avoiding crashes due to deferred setup.

Sequence diagram for synchronous init call in DccNetwork constructor

sequenceDiagram
    participant Caller
    participant DccNetwork

    Caller->>DccNetwork: DccNetwork constructor
    activate DccNetwork
    alt Previous_behavior
        DccNetwork->>DccNetwork: QMetaObject::invokeMethod(init, Qt::QueuedConnection)
        Note over Caller: init() executed later via event loop
    else Current_behavior
        DccNetwork->>DccNetwork: init()
        DccNetwork-->>Caller: constructed and initialized
    end
    deactivate DccNetwork
Loading

File-Level Changes

Change Details Files
Initialize DccNetwork synchronously in its constructor instead of via queued meta-object invocation to avoid deferred-init crashes.
  • Replace QMetaObject::invokeMethod(..., "init", Qt::QueuedConnection) with a direct init() call inside the DccNetwork constructor
  • Maintain existing QML type registrations and destructor behavior while only adjusting the initialization strategy
dcc-network/operation/dccnetwork.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

@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 left some high level feedback:

  • Since init() is now called synchronously from the constructor, double-check that it does not rely on the event loop or queued signals/slots that previously depended on Qt::QueuedConnection, and that any signals emitted during init() are safe to fire before the object is fully integrated with the rest of the system.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Since `init()` is now called synchronously from the constructor, double-check that it does not rely on the event loop or queued signals/slots that previously depended on `Qt::QueuedConnection`, and that any signals emitted during `init()` are safe to fire before the object is fully integrated with the rest of the system.

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: caixr23, robertkill

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

@deepin-bot

deepin-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 2.0.91
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #560

@deepin-bot

deepin-bot Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 2.0.92
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #563

@deepin-bot

deepin-bot Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 2.0.93
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #565

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.

3 participants