Skip to content

Conversation

@deepin-mozart
Copy link
Contributor

@deepin-mozart deepin-mozart commented May 9, 2025

debugpy started but not listen on port, so delay some time to wait it initialized.

Log:
Bug: https://pms.uniontech.com/bug-view-315851.html Change-Id: I14cd10e5b1fcb63a91b38562f9898f71122fbb67

Summary by Sourcery

Improve debugpy port initialization by implementing a more robust port readiness check mechanism

Bug Fixes:

  • Replace static sleep with a dynamic port readiness check to ensure debugpy is fully initialized before connecting

Enhancements:

  • Add retry mechanism to verify debugpy port is listening
  • Implement logging for port initialization status

debugpy started but not listen on port, so delay some time to wait it initialized.

Log:
Bug: https://pms.uniontech.com/bug-view-315851.html
Change-Id: I14cd10e5b1fcb63a91b38562f9898f71122fbb67
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: deepin-mozart

The full list of commands accepted by this bot can be found here.

The pull request process is described 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 May 9, 2025

Reviewer's Guide

This pull request updates the PythonDebugger::initialize method to address an issue where debugpy might not be ready to accept connections immediately after startup. Instead of a fixed delay, the implementation now actively checks if the debug port is listening using netstat within a loop, with a set number of retries and an interval, before proceeding. Logging has been added to indicate when the port becomes ready or if it times out.

File-Level Changes

Change Details Files
Implemented a dynamic port readiness check for debugpy.
  • Replaced a fixed 500ms delay with a loop that checks for port availability.
  • Utilized the `netstat -an
grep LISTEN

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 @deepin-mozart - I've reviewed your changes - here's some feedback:

  • Consider using QTcpSocket::connectToHost in a loop for a more portable and efficient port check, instead of polling with netstat.
  • Consider making the timeout duration (currently 5 seconds) and retry interval for the port check configurable.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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.

QProcess checkPort;
QString cmd = QString("netstat -an | grep LISTEN | grep %1").arg(d->port);
checkPort.start("bash", {"-c", cmd});
checkPort.waitForFinished();
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Add a timeout to waitForFinished().

Specify a timeout for waitForFinished to avoid hanging indefinitely if the netstat command stalls.

Suggested change
checkPort.waitForFinished();
if (!checkPort.waitForFinished(500)) {
// If the netstat command stalls, kill the process and continue the retry loop
checkPort.kill();
qWarning() << "Netstat command timed out for port" << d->port;
QThread::msleep(retryInterval);
continue;
}

d->process.setProcessEnvironment(env);
d->process.start("/bin/bash", options);
d->process.waitForStarted();
QThread::msleep(500); // The port may not start listening immediately when Python starts, resulting in the IDE being unable to connect. Wait for 500ms.
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider refactoring the polling loop to an asynchronous approach using a QTimer and asynchronous QProcess calls to reduce complexity.

Consider refactoring the polling loop to an asynchronous approach using a centrally managed QTimer and asynchronous QProcess calls. This would simplify the manual retry logic and eliminate spawning a new process on every cycle. For example, you could extract the port check into a dedicated function and use QTimer to periodically trigger the check:

// In your header, add a signal/slot if not already present:
class PythonDebugger : public QObject {
    Q_OBJECT
    // ...
private slots:
    void checkPortReadiness();
private:
    QTimer *portCheckTimer = nullptr;
};
// In your implementation after starting debugpy:
void PythonDebugger::startDebugpy() {
    // Existing process start code ...

    // Initialize asynchronous port check
    portCheckTimer = new QTimer(this);
    portCheckTimer->setInterval(100);
    connect(portCheckTimer, &QTimer::timeout, this, &PythonDebugger::checkPortReadiness);
    portCheckTimer->start();
}

void PythonDebugger::checkPortReadiness() {
    QProcess *checkPort = new QProcess(this);
    QString cmd = QString("netstat -an | grep LISTEN | grep %1").arg(d->port);
    connect(checkPort, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            [this, checkPort](int, QProcess::ExitStatus) {
                if (!checkPort->readAll().isEmpty()) {
                    portCheckTimer->stop();
                    qInfo() << "Debugpy port" << d->port << "is ready";
                }
                checkPort->deleteLater();
            });
    checkPort->start("bash", {"-c", cmd});
}

This approach centralizes the retry logic via a timer and creates a new QProcess only when needed. It keeps functionality intact while reducing overall complexity and unnecessary repeated process creations.

@deepin-ci-robot deepin-ci-robot merged commit 1ff2547 into linuxdeepin:master May 9, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants