Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: kill process silently #12790

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/vscode-extension/src/debug/depsChecker/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ async function checkPort(
let portsInUse = await localEnvManager.getPortsInUse(ports);
LocalDebugPorts.conflictPorts = portsInUse;
if (portsInUse.length > 0) {
const killRes = await selectPortsToKill(portsInUse);
if (killRes.isOk()) {
// recheck
portsInUse = await localEnvManager.getPortsInUse(ports);
}
await selectPortsToKill(portsInUse);
// wait some time
await new Promise((resolve) => setTimeout(resolve, 2000));
// recheck
portsInUse = await localEnvManager.getPortsInUse(ports);
}
const formatPortStr = (ports: number[]) =>
ports.length > 1 ? ports.join(", ") : `${ports[0]}`;
Expand Down
9 changes: 5 additions & 4 deletions packages/vscode-extension/src/utils/processUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const killModule = {

class ProcessUtil {
// kill process and its child processes
async killProcess(pid: number, timeout = 5000): Promise<void> {
async killProcess(pid: number, timeout = 5000, silent = true): Promise<void> {
const tPromise = timeoutPromise(timeout);
const killPromise = new Promise<void>((resolve, reject) => {
killModule.killTree(pid, "SIGTERM", (err) => {
if (err) {
if (err && !silent) {
reject(err);
} else {
resolve();
Expand All @@ -23,10 +23,11 @@ class ProcessUtil {
}
}

export function timeoutPromise(timeout: number): Promise<void> {
export function timeoutPromise(timeout: number, silent = true): Promise<void> {
return new Promise<void>((resolve, reject) => {
setTimeout(() => {
reject(new Error("Operation timeout"));
if (silent) resolve();
else reject(new Error("Operation timeout"));
}, timeout);
});
}
Expand Down
14 changes: 12 additions & 2 deletions packages/vscode-extension/test/utils/processUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("ProcessUtil", () => {
const killStub = sandbox.stub(killModule, "killTree");
killStub.yields(new Error());
try {
await processUtil.killProcess(-1);
await processUtil.killProcess(-1, 5000, false);
chai.assert.fail("Expected promise to reject, but it resolved.");
} catch (error) {
chai.assert.isTrue(error instanceof Error);
Expand Down Expand Up @@ -42,7 +42,7 @@ describe("timeoutPromise", () => {
it("timeoutPromise", async () => {
try {
const timeout = 1000;
const promise = timeoutPromise(timeout);
const promise = timeoutPromise(timeout, false);
clock.tick(timeout);
await promise;
chai.assert.fail("Expected promise to reject, but it resolved.");
Expand All @@ -51,4 +51,14 @@ describe("timeoutPromise", () => {
chai.assert.equal(error.message, "Operation timeout");
}
});
it("timeoutPromise - silent", async () => {
try {
const timeout = 1000;
const promise = timeoutPromise(timeout, true);
clock.tick(timeout);
await promise;
} catch (error) {
chai.assert.fail("Expected promise to resolve, but it rejected.");
}
});
});
Loading