()
| 63 | } |
| 64 | |
| 65 | function isStaleProxy() { |
| 66 | const settings = readSettings(); |
| 67 | const pid = settings.proxy?.pid; |
| 68 | if (!pid) return false; |
| 69 | try { |
| 70 | // process.kill(pid, 0) probes whether the process exists without sending a |
| 71 | // signal. On POSIX it throws ESRCH when the PID is gone. On Windows the |
| 72 | // Node.js runtime maps this to the same behavior (ESRCH via uv_kill), so |
| 73 | // the cross-platform semantics are consistent. If the current process does |
| 74 | // not have permission to query the target PID, EPERM is thrown -- that |
| 75 | // means the PID exists and is owned by another user, so we treat it as |
| 76 | // live (not stale) rather than crashing. |
| 77 | process.kill(pid, 0); |
| 78 | return false; |
| 79 | } catch (err) { |
| 80 | // ESRCH: process does not exist -> stale. |
| 81 | // EPERM: process exists but is not ours -> not stale (leave settings alone). |
| 82 | if (err.code === 'EPERM') return false; |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function clearIfStale() { |
| 88 | if (isStaleProxy()) { |
no test coverage detected