diff --git a/packages/teraslice-messaging/src/messenger/server.ts b/packages/teraslice-messaging/src/messenger/server.ts index 27b663bb64d..06115653ff4 100644 --- a/packages/teraslice-messaging/src/messenger/server.ts +++ b/packages/teraslice-messaging/src/messenger/server.ts @@ -93,34 +93,16 @@ export class Server extends Core { } async listen(): Promise { - // Check if port is available before using it - const testPort = async function (port: number) { - return new Promise((resolve) => { - const portTestServer = http.createServer(); - - portTestServer.unref(); - - portTestServer.once('error', () => { - portTestServer.close(() => { - return resolve(false); - }); - }); - - portTestServer.once('listening', () => { - portTestServer.close(() => { - return resolve(true); - }); + await pRetry(async () => { + await new Promise((resolve, reject) => { + this.httpServer.listen(this.port, (err?: Error) => { + if (err) { + reject(err); + } else { + resolve(); + } }); - - portTestServer.listen(port); }); - }; - - await pRetry(async () => { - const portAvailable = await testPort(this.port); - if (!portAvailable) { - throw new Error(`Port ${this.port} is already in-use`); - } }, { retries: isTest ? 1 : 5, endWithFatal: true, @@ -129,16 +111,6 @@ export class Server extends Core { } }); - await new Promise((resolve, reject) => { - this.httpServer.listen(this.port, (err?: Error) => { - if (err) { - reject(err); - } else { - resolve(); - } - }); - }); - this.server.attach(this.httpServer); this.server.use((socket, next) => {