Skip to content

Commit 6dab385

Browse files
lpcoxCopilotCopilot
authored
refactor: deduplicate trackPidForPort variants (#2494)
* refactor: deduplicate trackPidForPort variants Extract shared try/catch + file-read pattern from trackPidForPort (async) and trackPidForPortSync into a common trackPidForPortCore helper. Closes #2480 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix(pid-tracker): fix unused err and remove trackPidForPortCore * refactor(pid-tracker): extract makeTcpReadError to eliminate duplication Agent-Logs-Url: https://github.com/github/gh-aw-firewall/sessions/3fdd89e7-919a-4d73-9459-4fde40cb3499 --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 8229e3c commit 6dab385

1 file changed

Lines changed: 29 additions & 44 deletions

File tree

src/pid-tracker.ts

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ export function getProcessInfo(
279279
};
280280
}
281281

282+
/**
283+
* Builds the PidTrackResult returned when /proc/net/tcp cannot be read.
284+
*
285+
* Shared by trackPidForPort and trackPidForPortSync so the error shape
286+
* lives in exactly one place.
287+
*/
288+
function makeTcpReadError(tcpPath: string, err: unknown): PidTrackResult {
289+
return {
290+
pid: -1,
291+
cmdline: 'unknown',
292+
comm: 'unknown',
293+
error: `Failed to read ${tcpPath}: ${err}`,
294+
};
295+
}
296+
282297
/**
283298
* Resolves a PidTrackResult from already-read /proc/net/tcp content.
284299
*
@@ -349,31 +364,16 @@ export async function trackPidForPort(
349364
srcPort: number,
350365
procPath = '/proc'
351366
): Promise<PidTrackResult> {
352-
try {
353-
// Read /proc/net/tcp using async operations
354-
const tcpPath = path.join(procPath, 'net', 'tcp');
355-
let tcpContent: string;
356-
357-
try {
358-
tcpContent = await fsPromises.readFile(tcpPath, 'utf-8');
359-
} catch (err) {
360-
return {
361-
pid: -1,
362-
cmdline: 'unknown',
363-
comm: 'unknown',
364-
error: `Failed to read ${tcpPath}: ${err}`,
365-
};
366-
}
367+
const tcpPath = path.join(procPath, 'net', 'tcp');
368+
let tcpContent: string;
367369

368-
return resolvePidFromTcpContent(tcpContent, srcPort, procPath);
370+
try {
371+
tcpContent = await fsPromises.readFile(tcpPath, 'utf-8');
369372
} catch (err) {
370-
return {
371-
pid: -1,
372-
cmdline: 'unknown',
373-
comm: 'unknown',
374-
error: `Unexpected error: ${err}`,
375-
};
373+
return makeTcpReadError(tcpPath, err);
376374
}
375+
376+
return resolvePidFromTcpContent(tcpContent, srcPort, procPath);
377377
}
378378

379379
/**
@@ -384,31 +384,16 @@ export async function trackPidForPort(
384384
* @returns PidTrackResult with process information
385385
*/
386386
export function trackPidForPortSync(srcPort: number, procPath = '/proc'): PidTrackResult {
387-
try {
388-
// Read /proc/net/tcp
389-
const tcpPath = path.join(procPath, 'net', 'tcp');
390-
let tcpContent: string;
391-
392-
try {
393-
tcpContent = fs.readFileSync(tcpPath, 'utf-8');
394-
} catch (err) {
395-
return {
396-
pid: -1,
397-
cmdline: 'unknown',
398-
comm: 'unknown',
399-
error: `Failed to read ${tcpPath}: ${err}`,
400-
};
401-
}
387+
const tcpPath = path.join(procPath, 'net', 'tcp');
388+
let tcpContent: string;
402389

403-
return resolvePidFromTcpContent(tcpContent, srcPort, procPath);
390+
try {
391+
tcpContent = fs.readFileSync(tcpPath, 'utf-8');
404392
} catch (err) {
405-
return {
406-
pid: -1,
407-
cmdline: 'unknown',
408-
comm: 'unknown',
409-
error: `Unexpected error: ${err}`,
410-
};
393+
return makeTcpReadError(tcpPath, err);
411394
}
395+
396+
return resolvePidFromTcpContent(tcpContent, srcPort, procPath);
412397
}
413398

414399
/**

0 commit comments

Comments
 (0)