Skip to content

fix: Fix failure to update network details after a newly inserted dev…#502

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
ut003640:tmp1
Feb 9, 2026
Merged

fix: Fix failure to update network details after a newly inserted dev…#502
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
ut003640:tmp1

Conversation

@ut003640
Copy link
Contributor

@ut003640 ut003640 commented Feb 9, 2026

…ice connects to the network

When a new device is inserted, its 'managed' property is false. After it becomes true, the device object is created, but the signals of the newly created device are not connected. As a result, when the device status changes, the network status is not updated.

Log: Fix failure to update network details after a newly inserted device connects to the network
PMS: BUG-286921

fix: 修复新插入设备连接网络后无法更新网络详情

新插入设备的时候,该设备的managed为false,等它变成true后再创建设备对象,并未连接新创建的设备的信号,所以当设备状态变化的时候,没有更新网络状态

Log: 修复新插入设备连接网络后无法更新网络详情
PMS: BUG-286921

Summary by Sourcery

Bug Fixes:

  • Connect status and IP change signals for newly created network devices so network details stay in sync after a device becomes managed.

…ice connects to the network

When a new device is inserted, its 'managed' property is false. After it becomes true, the device object is created, but the signals of the newly created device are not connected. As a result, when the device status changes, the network status is not updated.

Log: Fix failure to update network details after a newly inserted device connects to the network
PMS: BUG-286921

fix: 修复新插入设备连接网络后无法更新网络详情

新插入设备的时候,该设备的managed为false,等它变成true后再创建设备对象,并未连接新创建的设备的信号,所以当设备状态变化的时候,没有更新网络状态

Log: 修复新插入设备连接网络后无法更新网络详情
PMS: BUG-286921
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 9, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Connects status-related signals for newly created network devices so that network details are updated when a hot‑plugged device becomes managed and its state changes.

Sequence diagram for updating network details on newly created device status changes

sequenceDiagram
    actor User
    participant Kernel as Kernel/OS
    participant NetworkManager as NetworkManager
    participant NetworkManagerProcesser as NetworkManagerProcesser
    participant NetworkDeviceBase as NetworkDeviceBase

    User->>Kernel: Insert network device
    Kernel-->>NetworkManager: Notify new device path
    NetworkManager-->>NetworkManagerProcesser: createOrRemoveDevice(path)

    activate NetworkManagerProcesser
    NetworkManagerProcesser->>NetworkManagerProcesser: Check managed flag (false)
    NetworkManagerProcesser-->>NetworkManager: Defer device creation
    deactivate NetworkManagerProcesser

    NetworkDeviceBase-->>NetworkManager: Device becomes managed (true)
    NetworkManager-->>NetworkManagerProcesser: createOrRemoveDevice(path)
    activate NetworkManagerProcesser
    NetworkManagerProcesser->>NetworkManager: Get device info
    NetworkManager-->>NetworkManagerProcesser: Device object

    NetworkManagerProcesser->>NetworkManagerProcesser: deviceExist(uni) == false
    NetworkManagerProcesser->>NetworkManagerProcesser: createDevice(device)
    NetworkManagerProcesser-->>NetworkDeviceBase: newDevice

    NetworkManagerProcesser->>NetworkDeviceBase: connect(deviceStatusChanged, onUpdateNetworkDetail)
    NetworkManagerProcesser->>NetworkDeviceBase: connect(activeConnectionChanged, onUpdateNetworkDetail)
    NetworkManagerProcesser->>NetworkDeviceBase: connect(ipV4Changed, onUpdateNetworkDetail)
    NetworkManagerProcesser->>NetworkManagerProcesser: m_devices << newDevice
    NetworkManagerProcesser->>NetworkManagerProcesser: sortDevice()
    NetworkManagerProcesser->>NetworkManagerProcesser: updateDeviceName()
    deactivate NetworkManagerProcesser

    NetworkDeviceBase-->>NetworkDeviceBase: Status or connection or IPv4 changes
    NetworkDeviceBase-->>NetworkManagerProcesser: deviceStatusChanged/activeConnectionChanged/ipV4Changed
    activate NetworkManagerProcesser
    NetworkManagerProcesser->>NetworkManagerProcesser: onUpdateNetworkDetail()
    NetworkManagerProcesser-->>NetworkManager: Updated network details
    deactivate NetworkManagerProcesser
Loading

Updated class diagram for NetworkManagerProcesser and NetworkDeviceBase signal connections

classDiagram
    class NetworkDeviceBase {
        <<QObject>>
        +QString uni()
        +void deviceStatusChanged()
        +void activeConnectionChanged()
        +void ipV4Changed()
    }

    class NetworkManagerProcesser {
        <<QObject>>
        -QList~NetworkDeviceBase*~ m_devices
        +bool deviceExist(QString uni)
        +NetworkDeviceBase* createDevice(NetworkDeviceBase* device)
        +void createOrRemoveDevice(QString path)
        +void sortDevice()
        +void updateDeviceName()
        +void onUpdateNetworkDetail()
    }

    NetworkManagerProcesser "1" o-- "*" NetworkDeviceBase : manages

    NetworkDeviceBase ..> NetworkManagerProcesser : emits deviceStatusChanged
    NetworkDeviceBase ..> NetworkManagerProcesser : emits activeConnectionChanged
    NetworkDeviceBase ..> NetworkManagerProcesser : emits ipV4Changed

    NetworkManagerProcesser ..> NetworkDeviceBase : connect signals in createOrRemoveDevice
Loading

File-Level Changes

Change Details Files
Wire newly created network devices’ signals to the network detail update handler when devices are added.
  • After creating a new NetworkDeviceBase instance, connect its deviceStatusChanged signal to NetworkManagerProcesser::onUpdateNetworkDetail
  • Connect the new device’s activeConnectionChanged signal to NetworkManagerProcesser::onUpdateNetworkDetail
  • Connect the new device’s ipV4Changed signal to NetworkManagerProcesser::onUpdateNetworkDetail before adding it to the internal device list and running sorting/name update logic
src/impl/networkmanager/networkmanagerprocesser.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

Copy link

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

  • If existing devices are wired to onUpdateNetworkDetail in a central helper (e.g., when initially populating m_devices), consider reusing that helper here to keep the signal connections consistent and avoid duplication.
  • Double‑check that all relevant NetworkDeviceBase signals that should trigger a network detail update are connected here (for parity with already‑managed devices), so newly inserted devices don’t diverge in behavior over time.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- If existing devices are wired to `onUpdateNetworkDetail` in a central helper (e.g., when initially populating `m_devices`), consider reusing that helper here to keep the signal connections consistent and avoid duplication.
- Double‑check that all relevant `NetworkDeviceBase` signals that should trigger a network detail update are connected here (for parity with already‑managed devices), so newly inserted devices don’t diverge in behavior over time.

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

deepin pr auto review

这段代码主要是在创建新网络设备时,增加了三个信号与槽的连接,用于更新网络详情。以下是对这段代码的审查意见,分为语法逻辑、代码质量、代码性能和代码安全四个方面:

1. 语法逻辑

  • 基本正确:代码逻辑上是通顺的。在创建设备对象后,立即连接信号与槽,最后将其加入列表。这是 Qt 中标准的对象生命周期管理模式。
  • 信号与槽参数匹配:需要确认 deviceStatusChangedactiveConnectionChangedipV4Changed 这三个信号的参数列表与槽函数 onUpdateNetworkDetail 的参数列表是否兼容。
    • 如果 onUpdateNetworkDetail 不需要参数,或者参数类型可以隐式转换,则连接是安全的。
    • 建议检查 NetworkDeviceBase 类中这些信号的定义,确保它们发出的参数是 onUpdateNetworkDetail 所期望的。

2. 代码质量

  • 代码可读性:代码结构清晰,但连续三次调用 connect 略显冗余。
    • 改进建议:如果后续还有更多信号需要连接到同一个槽,可以考虑使用 Qt::UniqueConnection 防止重复连接(尽管这里对象刚创建,重复可能性小,但作为防御性编程是好的习惯),或者封装一个辅助函数来处理这些连接。
  • 魔法字符串/函数名onUpdateNetworkDetail 这个命名暗示它可能是一个私有槽函数。确保这个函数的命名规范符合项目标准(例如是否统一使用 on... 前缀)。

3. 代码性能

  • 潜在的性能瓶颈
    • NetworkDeviceBase::deviceStatusChangedactiveConnectionChangedipV4Changed 这三个信号可能会在短时间内频繁触发(例如网络连接建立过程中,状态和IP会快速变化)。
    • 如果 onUpdateNetworkDetail 槽函数内部执行了耗时操作(如大量 IO 操作、复杂的 UI 刷新或数据库查询),频繁触发会导致界面卡顿或 CPU 占用过高。
  • 改进建议
    • 防抖动:在 onUpdateNetworkDetail 内部使用 QTimer::singleShot 进行防抖动处理。即当信号触发时,不立即执行逻辑,而是启动一个短定时器(如 200ms),如果在定时器期间又有新信号到来,则重置定时器。只有当信号停止触发一段时间后,才真正执行更新逻辑。
    • 批量更新:检查 onUpdateNetworkDetail 的逻辑,看是否可以将多次更新合并为一次处理。

4. 代码安全

  • 内存管理
    • m_devices << newDevice; 将新设备加入了一个列表。需要确认 m_devicesQObject 派生类的成员变量,并且在析构时是否正确删除了这些指针。
    • 如果 m_devices 是普通的 QListQVector,且 NetworkManagerProcesser 析构时没有遍历删除 newDevice,则会导致内存泄漏。
    • 改进建议:确保在 NetworkManagerProcesser 的析构函数中遍历 m_devicesdelete 所有对象,或者考虑使用 QVector<QSharedPointer<NetworkDeviceBase>> 等智能指针自动管理内存。
  • 空指针检查
    • 代码中已有 if (newDevice) 检查,这是很好的习惯。
  • 线程安全
    • 如果 createOrRemoveDevice 可能被多个线程调用,或者 device 对象来自其他线程,那么 connect 操作和 m_devices 的操作(<<sortDevice)需要加锁保护,或者确保这些操作都运行在同一个线程(通常通过 QObject::thread() 判断)。

总结与代码示例

建议修改后的代码片段(包含防抖动和 UniqueConnection):

// 假设在类定义中有一个成员变量: QTimer *m_updateDetailTimer; 
// 并在构造函数中初始化并连接:
// m_updateDetailTimer = new QTimer(this);
// m_updateDetailTimer->setSingleShot(true);
// connect(m_updateDetailTimer, &QTimer::timeout, this, &NetworkManagerProcesser::onUpdateNetworkDetail);

void NetworkManagerProcesser::createOrRemoveDevice(const QString &path)
{
    // ... 前面的代码 ...
    if (!deviceExist(device->uni())) {
        NetworkDeviceBase *newDevice = createDevice(device);
        if (newDevice) {
            // 使用 UniqueConnection 防止潜在的重连(防御性编程)
            connect(newDevice, &NetworkDeviceBase::deviceStatusChanged, this, [this]() {
                // 触发定时器,200ms 后执行,期间若再次触发则重置
                if (m_updateDetailTimer) {
                    m_updateDetailTimer->start(200);
                }
            }, Qt::UniqueConnection);

            connect(newDevice, &NetworkDeviceBase::activeConnectionChanged, this, [this]() {
                if (m_updateDetailTimer) {
                    m_updateDetailTimer->start(200);
                }
            }, Qt::UniqueConnection);

            connect(newDevice, &NetworkDeviceBase::ipV4Changed, this, [this]() {
                if (m_updateDetailTimer) {
                    m_updateDetailTimer->start(200);
                }
            }, Qt::UniqueConnection);

            m_devices << newDevice;
            sortDevice();
            updateDeviceName();
            // ... 后面的代码 ...
        }
    }
}

或者,如果不想引入 Lambda 和定时器,保持原样但确保内存安全:

void NetworkManagerProcesser::createOrRemoveDevice(const QString &path)
{
    // ... 前面的代码 ...
    if (!deviceExist(device->uni())) {
        NetworkDeviceBase *newDevice = createDevice(device);
        if (newDevice) {
            // 使用 UniqueConnection
            connect(newDevice, &NetworkDeviceBase::deviceStatusChanged, this, &NetworkManagerProcesser::onUpdateNetworkDetail, Qt::UniqueConnection);
            connect(newDevice, &NetworkDeviceBase::activeConnectionChanged, this, &NetworkManagerProcesser::onUpdateNetworkDetail, Qt::UniqueConnection);
            connect(newDevice, &NetworkDeviceBase::ipV4Changed, this, &NetworkManagerProcesser::onUpdateNetworkDetail, Qt::UniqueConnection);
            
            m_devices << newDevice;
            sortDevice();
            updateDeviceName();
            // ... 后面的代码 ...
        }
    }
}

并在析构函数中确保清理:

NetworkManagerProcesser::~NetworkManagerProcesser() {
    qDeleteAll(m_devices);
    m_devices.clear();
}

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy, ut003640

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

@ut003640
Copy link
Contributor Author

ut003640 commented Feb 9, 2026

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Feb 9, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 22c5f90 into linuxdeepin:master Feb 9, 2026
15 of 18 checks passed
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