Skip to content

Commit 1ff2547

Browse files
deepin-mozartdeepin-ci-robot
authored andcommitted
fix: python debugpy cannot connect
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
1 parent 71c3a56 commit 1ff2547

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

src/tools/debugadapter/debugger/python/pythondebugger.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,30 @@ void PythonDebugger::initialize(const QString &pythonExecute,
144144
d->process.setProcessEnvironment(env);
145145
d->process.start("/bin/bash", options);
146146
d->process.waitForStarted();
147-
QThread::msleep(500); // The port may not start listening immediately when Python starts, resulting in the IDE being unable to connect. Wait for 500ms.
147+
148+
// debugpy may not ready when launched,so we should delay when it start listen port.
149+
const int maxRetries = 50;
150+
const int retryInterval = 100;
151+
bool portReady = false;
152+
153+
for (int i = 0; i < maxRetries && !portReady; i++) {
154+
QProcess checkPort;
155+
QString cmd = QString("netstat -an | grep LISTEN | grep %1").arg(d->port);
156+
checkPort.start("bash", {"-c", cmd});
157+
checkPort.waitForFinished();
158+
159+
if (!checkPort.readAll().isEmpty()) {
160+
portReady = true;
161+
qInfo() << "Debugpy port" << d->port << "is ready after" << (i + 1) * retryInterval << "ms";
162+
break;
163+
}
164+
165+
QThread::msleep(retryInterval);
166+
}
167+
168+
if (!portReady) {
169+
qWarning() << "Debugpy port" << d->port << "failed to start after" << maxRetries * retryInterval << "ms";
170+
}
148171
}
149172

150173
void PythonDebugger::slotReceiveClientInfo(const QString &ppid,

0 commit comments

Comments
 (0)