forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.ts
More file actions
73 lines (65 loc) · 2.16 KB
/
Copy pathprocess.ts
File metadata and controls
73 lines (65 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { isWindows, runSubprocessCapture } from '../../../lib';
import * as fs from 'node:fs';
import * as fsp from 'node:fs/promises';
import * as path from 'node:path';
/**
* Check if a process with the given PID is currently running.
* Uses async subprocess calls to avoid blocking the TUI event loop.
*/
export async function isProcessRunning(pid: number): Promise<boolean> {
if (isWindows) {
return isProcessRunningWindows(pid);
}
return isProcessRunningUnix(pid);
}
/**
* Check if a process is running on Unix-like systems (macOS, Linux).
* This uses process.kill(pid, 0) which is synchronous but near-instant
* (it's a syscall, not a subprocess), so it won't block the event loop.
*/
function isProcessRunningUnix(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
/**
* Check if a process is running on Windows.
* Uses async subprocess to avoid blocking the TUI event loop.
*/
async function isProcessRunningWindows(pid: number): Promise<boolean> {
try {
const result = await runSubprocessCapture('tasklist', ['/FI', `PID eq ${pid}`, '/NH']);
return !result.stdout.includes('No tasks');
} catch {
return false;
}
}
/**
* Clean up stale CDK read-lock files from cdk.out directory.
* Only removes read.<PID>.<number>.lock files where the owning process is no longer running.
* Does NOT touch synth.lock or other coordination files to avoid corrupting concurrent runs.
*
* This function is async to avoid blocking the TUI event loop on Windows.
*/
export async function cleanupStaleLockFiles(cdkOutDir: string): Promise<void> {
if (!fs.existsSync(cdkOutDir)) return;
const files = await fsp.readdir(cdkOutDir);
const readLockPattern = /^read\.(\d+)\.\d+\.lock$/;
for (const file of files) {
const match = readLockPattern.exec(file);
if (!match?.[1]) continue;
const pid = parseInt(match[1], 10);
if (isNaN(pid)) continue;
if (!(await isProcessRunning(pid))) {
const lockFilePath = path.join(cdkOutDir, file);
try {
await fsp.unlink(lockFilePath);
} catch {
// Ignore - file may have been removed by another process
}
}
}
}