Skip to content

Conversation

@caixr23
Copy link
Contributor

@caixr23 caixr23 commented Jan 19, 2026

The connection selection logic was moved to the beginning of enableDevice function to ensure it runs before enabling networking. This prevents potential race conditions where network connections might change after enabling the device. The connection selection algorithm now correctly updates maxTs variable when finding the most recent autoconnect connection.

Log: Fixed connection selection timing issue during device activation

Influence:

  1. Test device activation with multiple available connections
  2. Verify autoconnect functionality works correctly
  3. Check that the most recent connection is properly selected
  4. Test network activation with disabled networking
  5. Verify connection selection when no autoconnect connections exist

fix: 将连接选择逻辑移到网络启用之前

连接选择逻辑被移到 enableDevice 函数的开头,确保在网络启用之前运行。这可
以防止在启用设备后网络连接可能发生变化的潜在竞争条件。连接选择算法现在在
找到最新的自动连接连接时正确更新 maxTs 变量。

Log: 修复设备激活期间连接选择时机问题

Influence:

  1. 测试具有多个可用连接时的设备激活
  2. 验证自动连接功能正常工作
  3. 检查是否正确选择了最新连接
  4. 测试网络禁用状态下的网络激活
  5. 验证没有自动连接连接时的连接选择

PMS: BUG-336935

Summary by Sourcery

Adjust device activation flow to select the appropriate autoconnect connection before enabling networking and correct the timestamp-based selection logic.

Bug Fixes:

  • Resolve a race condition by performing connection selection before enabling networking during device activation.
  • Fix autoconnect selection to update and use the latest connection timestamp when determining the preferred connection.

The connection selection logic was moved to the beginning of
enableDevice function to ensure it runs before enabling networking.
This prevents potential race conditions where network connections might
change after enabling the device. The connection selection algorithm
now correctly updates maxTs variable when finding the most recent
autoconnect connection.

Log: Fixed connection selection timing issue during device activation

Influence:
1. Test device activation with multiple available connections
2. Verify autoconnect functionality works correctly
3. Check that the most recent connection is properly selected
4. Test network activation with disabled networking
5. Verify connection selection when no autoconnect connections exist

fix: 将连接选择逻辑移到网络启用之前

连接选择逻辑被移到 enableDevice 函数的开头,确保在网络启用之前运行。这可
以防止在启用设备后网络连接可能发生变化的潜在竞争条件。连接选择算法现在在
找到最新的自动连接连接时正确更新 maxTs 变量。

Log: 修复设备激活期间连接选择时机问题

Influence:
1. 测试具有多个可用连接时的设备激活
2. 验证自动连接功能正常工作
3. 检查是否正确选择了最新连接
4. 测试网络禁用状态下的网络激活
5. 验证没有自动连接连接时的连接选择

PMS: BUG-336935
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: caixr23

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
Copy link

sourcery-ai bot commented Jan 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Moves connection selection earlier in NetworkThread::enableDevice to avoid races with networking enablement and fixes the autoconnect timestamp comparison so the most recent connection is selected correctly.

Sequence diagram for updated enableDevice connection selection timing

sequenceDiagram
    actor System
    participant NetworkThread
    participant Device
    participant Connection
    participant NetworkManager

    System->>NetworkThread: enableDevice(device)
    activate NetworkThread

    Note over NetworkThread,Device: 1) Select autoconnect connection before enabling networking
    NetworkThread->>Device: availableConnections()
    Device-->>NetworkThread: connPaths

    loop for each connPath in connPaths
        NetworkThread->>Connection: connPath.settings()
        Connection-->>NetworkThread: settings
        alt settings.autoconnect is true
            NetworkThread->>Connection: settings.timestamp()
            Connection-->>NetworkThread: ts
            alt ts is newer than maxTs or no selection yet
                NetworkThread-->>NetworkThread: update maxTs and connPath0
            end
        else settings.autoconnect is false
            NetworkThread-->>NetworkThread: skip connection
        end
    end

    NetworkThread->>NetworkManager: isNetworkingEnabled()
    NetworkManager-->>NetworkThread: enabled
    alt enabled is false
        NetworkThread->>NetworkManager: setNetworkingEnabled(true)
    end

    alt connPath0 is empty
        NetworkThread-->>System: "/"
    else
        NetworkThread-->>System: connPath0
    end
    deactivate NetworkThread
Loading

Flow diagram for autoconnect connection selection in enableDevice

flowchart TD
    A[Start enableDevice] --> B[Get connPaths from device.availableConnections]
    B --> C[Initialize connPath0 as empty]
    C --> D[Initialize maxTs as default QDateTime]
    D --> E{More connPaths?}
    E -->|No| F{Is connPath0 empty?}
    F -->|Yes| G[Return /]
    F -->|No| H[Return connPath0]

    E -->|Yes| I[Take next connPath]
    I --> J[Load settings from connPath.settings]
    J --> K{settings.autoconnect?}
    K -->|No| E
    K -->|Yes| L[ts = settings.timestamp]
    L --> M{ts > maxTs or connPath0 is empty?}
    M -->|No| E
    M -->|Yes| N[Set maxTs = ts and connPath0 = connPath.path]
    N --> E
Loading

File-Level Changes

Change Details Files
Reordered connection selection to occur before enabling networking and corrected the autoconnect selection logic to track the latest timestamp.
  • Invoke device->availableConnections() and iterate over them at the start of enableDevice instead of after networking is enabled.
  • Filter connections by settings()->autoconnect() and compute the most recent connection using a maxTs QDateTime accumulator.
  • Ensure both maxTs and connPath0 are updated when a newer autoconnect connection is found, and return either the selected connection path or "/" if none is available.
network-service-plugin/src/system/networkthread.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

@deepin-ci-robot
Copy link

deepin pr auto review

代码审查报告

1. 语法逻辑

  • 代码逻辑正确:代码的修改将查找自动连接配置的逻辑移动到了 enableDevice 函数的前部。这在逻辑上是合理的,因为:
    • 查找连接路径(connPath0)是一个纯粹的读取操作,不依赖于网络是否已启用。
    • 原代码中,这部分逻辑位于 NetworkManager::setNetworkingEnabled(true) 之后,虽然功能上没问题,但将其提前可以使得后续的启用操作与连接路径的选择逻辑解耦。
  • 空指针风险auto settings = connPath->settings(); 调用返回的是指针。代码中直接调用了 settings->autoconnect()settings->timestamp(),如果 settings 为空指针,这将导致程序崩溃。建议增加空指针检查。

2. 代码质量

  • 代码可读性:变量命名 connPath0maxTs 略显晦涩。建议改为更具描述性的名称,例如 selectedConnPathlatestTimestamp
  • 代码重复:虽然本次修改只是移动了代码块,但该逻辑(查找最新的自动连接配置)具有一定的复用性。如果未来需要在其他地方使用,建议将其提取为一个独立的辅助函数。
  • 调试日志qCDebug(DSM()) << "available connections:" << connPaths; 是一个很好的调试日志,但在生产环境中可能会产生大量输出。建议根据日志级别控制输出。

3. 代码性能

  • 性能影响:代码修改本身对性能的影响较小。查找连接路径的逻辑是必要的,且时间复杂度为 O(n),其中 n 是 availableConnections() 返回的连接数量。对于大多数场景,n 通常较小,因此性能影响可以忽略。
  • 潜在优化:如果 device->availableConnections() 返回的列表非常大,可以考虑提前终止循环(例如,如果只需要最新的连接,可以在找到后立即退出循环)。但目前的实现已经通过 maxTs 保证了只选择最新的连接,因此无需额外优化。

4. 代码安全

  • 空指针检查:如前所述,settings 可能为空指针,建议增加检查:
    auto settings = connPath->settings();
    if (!settings) {
        continue;
    }
  • 线程安全NetworkManager 的操作通常涉及异步调用和信号槽机制,但 enableDevice 函数本身并未显式涉及多线程。如果 device->availableConnections()settings->autoconnect() 不是线程安全的,可能需要额外的同步机制。建议确认 NetworkManager 的线程安全保证。

5. 改进建议

以下是改进后的代码片段:

QString NetworkThread::enableDevice(NetworkManager::Device::Ptr device)
{
    auto connPaths = device->availableConnections();
    qCDebug(DSM()) << "available connections:" << connPaths;
    QString selectedConnPath;
    QDateTime latestTimestamp;

    for (auto &&connPath : connPaths) {
        auto settings = connPath->settings();
        if (!settings || !settings->autoconnect()) {
            continue;
        }
        QDateTime ts = settings->timestamp();
        if (ts > latestTimestamp || selectedConnPath.isEmpty()) {
            latestTimestamp = ts;
            selectedConnPath = connPath->path();
        }
    }

    bool enabled = NetworkManager::isNetworkingEnabled();
    if (!enabled) {
        NetworkManager::setNetworkingEnabled(true);
    }

    if (!device->isEnabled()) {
        device->setEnabled(true);
    }

    return selectedConnPath.isEmpty() ? "/" : selectedConnPath;
}

6. 总结

本次修改主要是代码块的移动,逻辑上没有问题,但可以通过以下改进提升代码质量:

  1. 增加空指针检查,避免潜在崩溃。
  2. 优化变量命名,提升可读性。
  3. 考虑将查找连接路径的逻辑提取为独立函数,便于复用。
  4. 确认 NetworkManager 的线程安全保证。

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:

  • When picking the most recent autoconnect connection, consider basing the initialization/guard purely on maxTs.isValid() (or !maxTs.isNull()) instead of connPath0.isEmpty() so the selection logic is clearly tied to timestamp validity rather than the side effect of having chosen a path.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When picking the most recent autoconnect connection, consider basing the initialization/guard purely on `maxTs.isValid()` (or `!maxTs.isNull()`) instead of `connPath0.isEmpty()` so the selection logic is clearly tied to timestamp validity rather than the side effect of having chosen a path.

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.

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