Skip to content

Commit

Permalink
fix: プロセスIDからプロセスを特定するのを wmic から tasklist に変更 (#2450)
Browse files Browse the repository at this point in the history
Co-authored-by: Nanashi. <[email protected]>
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
3 people authored Dec 31, 2024
1 parent 7cc32cc commit 9fd356b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/backend/electron/manager/engineProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class EngineProcessManager {
if (pid != undefined) {
const processName = await getProcessNameFromPid(engineHostInfo, pid);
log.warn(
`ENGINE ${engineId}: Port ${port} has already been assigned by ${processName} (pid=${pid})`,
`ENGINE ${engineId}: Port ${port} has already been assigned by ${processName ?? "(not found)"} (pid=${pid})`,
);
} else {
// ポートは使用不可能だがプロセスidは見つからなかった
Expand Down
23 changes: 15 additions & 8 deletions src/backend/electron/portHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ export async function getPidFromPort(
export async function getProcessNameFromPid(
hostInfo: HostInfo,
pid: number,
): Promise<string> {
): Promise<string | undefined> {
portLog(hostInfo.port, `Getting process name from pid=${pid}...`);
const exec = isWindows
? {
cmd: "wmic",
args: ["process", "where", `"ProcessID=${pid}"`, "get", "name"],
cmd: "tasklist",
args: ["/FI", `"PID eq ${pid}"`, "/NH"],
}
: {
cmd: "ps",
Expand All @@ -165,15 +165,22 @@ export async function getProcessNameFromPid(
/*
* ex) stdout:
* ```
* Name
* node.exe
*
* node.exe 25180 Console 1 86,544 K
* ```
* -> `node.exe`
*/
const processName = isWindows ? stdout.split("\n")[1] : stdout;
const processName = (
isWindows ? stdout.split("\r\n").at(1)?.split(/ +/)?.at(0) : stdout
)?.trim();

portLog(hostInfo.port, `Found process name: ${processName}`);
return processName.trim();
if (processName == undefined) {
portWarn(hostInfo.port, `Not found process name from pid=${pid}!`);
return undefined;
} else {
portLog(hostInfo.port, `Found process name: ${processName}`);
return processName;
}
}

/**
Expand Down

0 comments on commit 9fd356b

Please sign in to comment.