MCPcopy Create free account
hub / github.com/Marus/cortex-debug / isPortInUseEx

Method isPortInUseEx

src/tcpportscanner.ts:102–128  ·  view source on GitHub ↗
(port: number, host: string, avoid: Set<number>)

Source from the content-addressed store, hash-verified

100 */
101 private static SeqNumber = 0;
102 public static isPortInUseEx(port: number, host: string, avoid: Set<number>): Promise<boolean> {
103 const seq = TcpPortScanner.SeqNumber++;
104 const tries = TcpPortScanner.getLocalHostAliases();
105 // We could have launched all tests at once and waited on a Promise.all but that fails on Linux
106 // Have seen cases where it steps on itself. In the same try, it will give an EADDRINUSE and a listen
107 // will succeed. Doing one at a time avoids that but that Linux behavior is strange indeed
108 return new Promise<boolean> (async (resolve) => {
109 if (avoid && avoid.has(port)) {
110 resolve(true);
111 return;
112 }
113 for (const host of tries) {
114 try {
115 const inUse = await TcpPortScanner.isPortInUse(port, host, seq);
116 if (inUse) {
117 resolve(true);
118 return;
119 }
120 }
121 catch (e) {
122 resolve(true);
123 return;
124 }
125 }
126 resolve(false);
127 });
128 }
129
130 /**
131 * Scan for free ports (no one listening) on the specified host.

Callers 2

getAnyFreePortFunction · 0.45
nextMethod · 0.45

Calls 2

getLocalHostAliasesMethod · 0.45
isPortInUseMethod · 0.45

Tested by

no test coverage detected