From 38cecad6542fc834a024828c5048f1948a8dbe2d Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Mon, 25 Mar 2024 12:31:31 +0100 Subject: [PATCH] hotfix port discovery (#709) --- executor/package.json | 2 +- packages/chopsticks/package.json | 2 +- packages/chopsticks/src/server.ts | 44 +++++++++++++++++------ packages/core/package.json | 2 +- packages/db/package.json | 2 +- packages/e2e/src/genesis-provider.test.ts | 2 +- packages/testing/package.json | 2 +- packages/utils/package.json | 2 +- 8 files changed, 40 insertions(+), 18 deletions(-) diff --git a/executor/package.json b/executor/package.json index c17a1ae6..1e92c7c8 100644 --- a/executor/package.json +++ b/executor/package.json @@ -1,7 +1,7 @@ { "name": "@acala-network/chopsticks-executor", "description": "Chopsticks executor", - "version": "0.9.11", + "version": "0.9.12", "license": "Apache-2.0", "type": "module", "repository": { diff --git a/packages/chopsticks/package.json b/packages/chopsticks/package.json index 65a087d7..08b5d374 100644 --- a/packages/chopsticks/package.json +++ b/packages/chopsticks/package.json @@ -1,6 +1,6 @@ { "name": "@acala-network/chopsticks", - "version": "0.9.11", + "version": "0.9.12", "author": "Acala Developers ", "license": "Apache-2.0", "bin": "./chopsticks.cjs", diff --git a/packages/chopsticks/src/server.ts b/packages/chopsticks/src/server.ts index 9dbe1b12..17159118 100644 --- a/packages/chopsticks/src/server.ts +++ b/packages/chopsticks/src/server.ts @@ -57,6 +57,27 @@ const respond = (res: http.ServerResponse, data?: any) => { res.end() } +const portInUse = async (port: number) => { + const server = http.createServer() + const inUse = await new Promise((resolve) => { + server.once('error', (e: any) => { + if (e.code === 'EADDRINUSE') { + resolve(true) + } else { + resolve(false) + } + }) + server.once('listening', () => { + server.close() + resolve(false) + }) + server.listen(port) + }) + server.removeAllListeners() + server.unref() + return inUse +} + export const createServer = async (handler: Handler, port: number) => { let wss: WebSocketServer | undefined let listenPort: number | undefined @@ -118,24 +139,25 @@ export const createServer = async (handler: Handler, port: number) => { }) for (let i = 0; i < 10; i++) { + if (port && (await portInUse(port + i))) { + continue + } const preferPort = port ? port + i : undefined wsLogger.debug('Try starting on port %d', preferPort) - const success = await new Promise((resolve) => { - server.once('error', (e: any) => { - if (e.code === 'EADDRINUSE') { - server.close() - resolve(false) - } - }) + await new Promise((resolve, reject) => { + const onError = (e: Error) => { + server.close() + reject(e) + } + server.once('error', onError) server.listen(preferPort, () => { wss = new WebSocketServer({ server, maxPayload: 1024 * 1024 * 100 }) listenPort = (server.address() as AddressInfo).port - resolve(true) + server.removeListener('error', onError) + resolve() }) }) - if (success) { - break - } + break } if (!wss || !listenPort) { diff --git a/packages/core/package.json b/packages/core/package.json index d0780bc0..d5cb72dd 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@acala-network/chopsticks-core", - "version": "0.9.11", + "version": "0.9.12", "author": "Acala Developers ", "license": "Apache-2.0", "type": "module", diff --git a/packages/db/package.json b/packages/db/package.json index 0ba2aad8..a4982282 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -1,6 +1,6 @@ { "name": "@acala-network/chopsticks-db", - "version": "0.9.11", + "version": "0.9.12", "author": "Acala Developers ", "license": "Apache-2.0", "type": "module", diff --git a/packages/e2e/src/genesis-provider.test.ts b/packages/e2e/src/genesis-provider.test.ts index 3a1259c6..05656b5f 100644 --- a/packages/e2e/src/genesis-provider.test.ts +++ b/packages/e2e/src/genesis-provider.test.ts @@ -13,7 +13,7 @@ describe.each([ ['Asset Hub Kusama', new URL('../blobs/asset-hub-kusama.json', import.meta.url).pathname], ])(`genesis provider works %s`, async (name, genesis) => { const { chain, dev, api, teardown } = await setupContextWithConfig({ - port: 0, + port: 1234, genesis, 'build-block-mode': BuildBlockMode.Manual, }) diff --git a/packages/testing/package.json b/packages/testing/package.json index 4b8ff42a..663fdaac 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -1,6 +1,6 @@ { "name": "@acala-network/chopsticks-testing", - "version": "0.9.11", + "version": "0.9.12", "author": "Acala Developers ", "license": "Apache-2.0", "type": "module", diff --git a/packages/utils/package.json b/packages/utils/package.json index fddffeae..f9cb6091 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@acala-network/chopsticks-utils", - "version": "0.9.11", + "version": "0.9.12", "author": "Acala Developers ", "license": "Apache-2.0", "type": "module",