fix: Sometimes failed to attach the debugee (#1272)

This commit is contained in:
Shi Chen 2022-11-28 13:02:30 +08:00 коммит произвёл GitHub
Родитель 7eac84b7d7
Коммит 9d9a5cc455
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 22 добавлений и 10 удалений

Просмотреть файл

@ -57,9 +57,8 @@ export class GradleRunnerTerminal implements vscode.Pseudoterminal {
reject(new Error("The task completed without the debugger being attached"));
});
});
const logError = (err: Error): void => logger.error("Unable to start Java debugging:", err.message);
Promise.race([waitOnTcp("localhost", javaDebugPort), closePromise])
.then(async () => {
Promise.race([waitOnTcp("localhost", javaDebugPort), closePromise]).then(
async () => {
const definition = this.task?.definition as GradleTaskDefinition;
const projectName = definition ? definition.project : undefined;
const debugConfig = {
@ -77,11 +76,14 @@ export class GradleRunnerTerminal implements vscode.Pseudoterminal {
if (!startedDebugging) {
throw new Error("The debugger was not started");
}
}, logError)
.catch((err) => {
logError(err);
return this.close();
});
},
(err) => {
const errorMessage = "Unable to start Java debugging:" + err.message;
logger.error(errorMessage);
vscode.window.showErrorMessage(errorMessage);
this.close();
}
);
}
private async runBuild(): Promise<void> {

Просмотреть файл

@ -8,7 +8,8 @@ export const isTest = (): boolean => process.env.VSCODE_TEST?.toLowerCase() ===
export const isDebuggingServer = (): boolean => process.env.VSCODE_DEBUG_SERVER?.toLowerCase() === "true";
const maximumTimeout = 10000; // ms
// some run application tasks require a lot of time to start. So we should set a loose timeout.
const maximumTimeout = 60000; // ms
const tcpTimeout = 300; // ms
function tcpExists(host: string, port: number): Promise<boolean> {
@ -32,10 +33,19 @@ function tcpExists(host: string, port: number): Promise<boolean> {
});
}
function sleep(time: number) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function tryConnect(host: string, port: number, startTime: number): Promise<void> {
const connected = await tcpExists(host, port);
if (connected) {
return;
// workaround: experimental re-checking after waiting for 1s
await sleep(1000);
const connectedRetry = await tcpExists(host, port);
if (connectedRetry) {
return;
}
}
if (Date.now() - startTime >= maximumTimeout) {
throw new Error("Unable to wait on tcp due to maxmium timeout reached");