fix(cli): handle EPERM signal errors in isProcessRunning helper

This commit is contained in:
yiling
2026-06-20 13:02:06 +08:00
parent c8b1d521d8
commit ca0c2ece75
+19 -18
View File
@@ -16,6 +16,18 @@ const SHUMAI_DIR = join(homedir(), '.shumai')
const PID_DIR = join(SHUMAI_DIR, 'pids') const PID_DIR = join(SHUMAI_DIR, 'pids')
const LOG_DIR = join(SHUMAI_DIR, 'logs') const LOG_DIR = join(SHUMAI_DIR, 'logs')
function isProcessRunning(pid: number): boolean {
try {
process.kill(pid, 0)
return true
} catch (err) {
if (err && typeof err === 'object' && 'code' in err && err.code === 'EPERM') {
return true
}
return false
}
}
async function stopProcess(appName: string, pidFile: string): Promise<boolean> { async function stopProcess(appName: string, pidFile: string): Promise<boolean> {
if (!existsSync(pidFile)) { if (!existsSync(pidFile)) {
console.log(`No running instance of ${appName} found.`) console.log(`No running instance of ${appName} found.`)
@@ -33,14 +45,7 @@ async function stopProcess(appName: string, pidFile: string): Promise<boolean> {
return false return false
} }
let isRunning = false let isRunning = isProcessRunning(pid)
try {
process.kill(pid, 0)
isRunning = true
} catch {
// Not running
}
if (!isRunning) { if (!isRunning) {
console.log(`Process ${pid} is not running. Cleaning up PID file.`) console.log(`Process ${pid} is not running. Cleaning up PID file.`)
try { try {
@@ -61,13 +66,11 @@ async function stopProcess(appName: string, pidFile: string): Promise<boolean> {
// Wait for it to stop // Wait for it to stop
for (let i = 0; i < 30; i++) { for (let i = 0; i < 30; i++) {
try { if (!isProcessRunning(pid)) {
process.kill(pid, 0)
await new Promise((resolve) => setTimeout(resolve, 100))
} catch {
isRunning = false isRunning = false
break break
} }
await new Promise((resolve) => setTimeout(resolve, 100))
} }
if (isRunning) { if (isRunning) {
@@ -94,13 +97,12 @@ function startDaemon(appName: string, pidFile: string, logFile: string): void {
if (existsSync(pidFile)) { if (existsSync(pidFile)) {
const pid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10) const pid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10)
if (!isNaN(pid) && pid !== process.pid) { if (!isNaN(pid) && pid !== process.pid) {
try { if (isProcessRunning(pid)) {
process.kill(pid, 0)
console.error( console.error(
`${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`, `${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`,
) )
process.exit(1) process.exit(1)
} catch { } else {
try { try {
unlinkSync(pidFile) unlinkSync(pidFile)
} catch { } catch {
@@ -218,13 +220,12 @@ export async function handleDaemonCommands(
if (existsSync(pidFile)) { if (existsSync(pidFile)) {
const pid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10) const pid = parseInt(readFileSync(pidFile, 'utf8').trim(), 10)
if (!isNaN(pid) && pid !== process.pid) { if (!isNaN(pid) && pid !== process.pid) {
try { if (isProcessRunning(pid)) {
process.kill(pid, 0)
console.error( console.error(
`${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`, `${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`,
) )
process.exit(1) process.exit(1)
} catch { } else {
// Process is dead, clean up PID file // Process is dead, clean up PID file
try { try {
unlinkSync(pidFile) unlinkSync(pidFile)