Skip to content

Commit 0e2fb9f

Browse files
authored
refactor(pid-tracker): extract resolvePidFromTcpContent to deduplicate async/sync track logic (#2432)
* Initial plan * refactor: extract resolvePidFromTcpContent to eliminate duplicated logic Agent-Logs-Url: https://github.com/github/gh-aw-firewall/sessions/7cc2e5c5-52d8-4a2c-ba48-c11173aae264 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 1b3d570 commit 0e2fb9f

1 file changed

Lines changed: 50 additions & 65 deletions

File tree

src/pid-tracker.ts

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

282+
/**
283+
* Resolves a PidTrackResult from already-read /proc/net/tcp content.
284+
*
285+
* Shared helper used by both trackPidForPort (async) and trackPidForPortSync (sync)
286+
* to avoid duplicating the parse/lookup/return logic.
287+
*
288+
* @param tcpContent - Contents of /proc/net/tcp
289+
* @param srcPort - Source port number from the network connection
290+
* @param procPath - Base path to /proc
291+
* @returns PidTrackResult with process information
292+
*/
293+
function resolvePidFromTcpContent(
294+
tcpContent: string,
295+
srcPort: number,
296+
procPath: string
297+
): PidTrackResult {
298+
const entries = parseNetTcp(tcpContent);
299+
const inode = findInodeForPort(entries, srcPort);
300+
301+
if (!inode || inode === '0') {
302+
return {
303+
pid: -1,
304+
cmdline: 'unknown',
305+
comm: 'unknown',
306+
error: `No socket found for port ${srcPort}`,
307+
};
308+
}
309+
310+
const processInfo = findProcessByInode(inode, procPath);
311+
312+
if (!processInfo) {
313+
return {
314+
pid: -1,
315+
cmdline: 'unknown',
316+
comm: 'unknown',
317+
inode,
318+
error: `Socket inode ${inode} found but no process owns it`,
319+
};
320+
}
321+
322+
return {
323+
pid: processInfo.pid,
324+
cmdline: processInfo.cmdline,
325+
comm: processInfo.comm,
326+
inode,
327+
};
328+
}
329+
282330
/**
283331
* Main function to track a process by its source port
284332
*
@@ -317,39 +365,7 @@ export async function trackPidForPort(
317365
};
318366
}
319367

320-
// Parse TCP connections and find the inode for our port
321-
const entries = parseNetTcp(tcpContent);
322-
const inode = findInodeForPort(entries, srcPort);
323-
324-
if (!inode || inode === '0') {
325-
return {
326-
pid: -1,
327-
cmdline: 'unknown',
328-
comm: 'unknown',
329-
error: `No socket found for port ${srcPort}`,
330-
};
331-
}
332-
333-
// Find the process that owns this socket (uses sync operations for fd scanning)
334-
// This is intentional as the /proc filesystem is very fast and sync is simpler
335-
const processInfo = findProcessByInode(inode, procPath);
336-
337-
if (!processInfo) {
338-
return {
339-
pid: -1,
340-
cmdline: 'unknown',
341-
comm: 'unknown',
342-
inode,
343-
error: `Socket inode ${inode} found but no process owns it`,
344-
};
345-
}
346-
347-
return {
348-
pid: processInfo.pid,
349-
cmdline: processInfo.cmdline,
350-
comm: processInfo.comm,
351-
inode,
352-
};
368+
return resolvePidFromTcpContent(tcpContent, srcPort, procPath);
353369
} catch (err) {
354370
return {
355371
pid: -1,
@@ -384,38 +400,7 @@ export function trackPidForPortSync(srcPort: number, procPath = '/proc'): PidTra
384400
};
385401
}
386402

387-
// Parse TCP connections and find the inode for our port
388-
const entries = parseNetTcp(tcpContent);
389-
const inode = findInodeForPort(entries, srcPort);
390-
391-
if (!inode || inode === '0') {
392-
return {
393-
pid: -1,
394-
cmdline: 'unknown',
395-
comm: 'unknown',
396-
error: `No socket found for port ${srcPort}`,
397-
};
398-
}
399-
400-
// Find the process that owns this socket
401-
const processInfo = findProcessByInode(inode, procPath);
402-
403-
if (!processInfo) {
404-
return {
405-
pid: -1,
406-
cmdline: 'unknown',
407-
comm: 'unknown',
408-
inode,
409-
error: `Socket inode ${inode} found but no process owns it`,
410-
};
411-
}
412-
413-
return {
414-
pid: processInfo.pid,
415-
cmdline: processInfo.cmdline,
416-
comm: processInfo.comm,
417-
inode,
418-
};
403+
return resolvePidFromTcpContent(tcpContent, srcPort, procPath);
419404
} catch (err) {
420405
return {
421406
pid: -1,

0 commit comments

Comments
 (0)