Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-boxes-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/utils": patch
---

fix(utils): detect linux ports via /proc
126 changes: 122 additions & 4 deletions packages/utils/src/get-port.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,118 @@
import { readdir, readFile, readlink } from 'node:fs/promises';
import { execa } from 'execa';

/**
* Parses a port string and returns it if valid (0-65535), otherwise undefined.
*/
function parsePort(value: string, radix = 10): number | undefined {
const port = parseInt(value, radix);
if (!Number.isNaN(port) && port >= 0 && port <= 65535) {
return port;
}
return undefined;
}

/**
* Gets listening ports for the current process on Linux by reading /proc filesystem.
* This approach requires no external commands and works on all Linux systems.
*/
async function getLinuxPort(pid: number): Promise<number | undefined> {
const listenState = '0A'; // TCP LISTEN state in /proc/net/tcp
const tcpFiles = ['/proc/net/tcp', '/proc/net/tcp6'] as const;

// Step 1: Get socket inodes from /proc/<pid>/fd/ in order
// We preserve order to maintain deterministic behavior (return first port)
// Use both array (for order) and Set (for O(1) lookup)
const socketInodes: string[] = [];
const socketInodesSet = new Set<string>();
const fdPath = `/proc/${pid}/fd`;

try {
const fds = await readdir(fdPath);
// Sort FDs numerically to ensure deterministic order (FDs are always numeric strings)
const sortedFds = fds.sort((a, b) => {
const numA = Number.parseInt(a, 10);
const numB = Number.parseInt(b, 10);
return numA - numB;
});

const results = await Promise.allSettled(
sortedFds.map(async (fd) => {
const link = await readlink(`${fdPath}/${fd}`);
// Socket links look like: socket:[12345]
const match = link.match(/^socket:\[(\d+)\]$/);
return match?.[1] ?? null;
})
);

for (const result of results) {
if (result.status === 'fulfilled' && result.value) {
socketInodes.push(result.value);
socketInodesSet.add(result.value);
}
}
} catch {
// Process might not exist or no permission
return undefined;
}

if (socketInodes.length === 0) {
return undefined;
}

// Step 2: Read /proc/net/tcp and /proc/net/tcp6 to find listening sockets
// Format: sl local_address rem_address st ... inode
// local_address is hex IP:port, st=0A means LISTEN
// We iterate through socket inodes in order to maintain deterministic behavior
for (const tcpFile of tcpFiles) {
try {
const content = await readFile(tcpFile, 'utf8');
const lines = content.split('\n').slice(1); // Skip header

// Build a map of inode -> port for quick lookup
const inodeToPort = new Map<string, number>();
for (const line of lines) {
if (!line.trim()) continue; // Skip empty lines

const parts = line.trim().split(/\s+/);
if (parts.length < 10) continue;

const localAddr = parts[1]; // e.g., "00000000:0BB8" (0.0.0.0:3000)
const state = parts[3]; // "0A" = LISTEN
const inode = parts[9];

if (!localAddr || state !== listenState || !inode) continue;
if (!socketInodesSet.has(inode)) continue;

// Extract port from hex format (e.g., "0BB8" -> 3000)
const colonIndex = localAddr.indexOf(':');
if (colonIndex === -1) continue;

const portHex = localAddr.slice(colonIndex + 1);
if (!portHex) continue;

const port = parsePort(portHex, 16);
if (port !== undefined) {
inodeToPort.set(inode, port);
}
}

// Return the first port matching our socket inodes in order
for (const inode of socketInodes) {
const port = inodeToPort.get(inode);
if (port !== undefined) {
return port;
}
}
} catch {
// File might not exist (e.g., no IPv6 support) - continue to next file
continue;
}
}

return undefined;
}

/**
* Gets the port number that the process is listening on.
* @returns The port number that the process is listening on, or undefined if the process is not listening on any port.
Expand All @@ -11,7 +124,10 @@ export async function getPort(): Promise<number | undefined> {

try {
switch (platform) {
case 'linux':
case 'linux': {
port = await getLinuxPort(pid);
break;
}
case 'darwin': {
const lsofResult = await execa('lsof', [
'-a',
Expand All @@ -28,7 +144,7 @@ export async function getPort(): Promise<number | undefined> {
input: lsofResult.stdout,
}
);
port = parseInt(awkResult.stdout.trim(), 10);
port = parsePort(awkResult.stdout.trim());
break;
}

Expand All @@ -50,8 +166,10 @@ export async function getPort(): Promise<number | undefined> {
.trim()
.match(/^\s*TCP\s+(?:\[[\da-f:]+\]|[\d.]+):(\d+)\s+/i);
if (match) {
port = parseInt(match[1], 10);
break;
port = parsePort(match[1]);
if (port !== undefined) {
break;
}
}
}
}
Expand Down
Loading