From 4e9c4149cb4551637cb775665d7e8ef604b062b2 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 25 Jun 2024 16:51:28 -0400 Subject: [PATCH] fix(core): unref socket and avoid needlessly disposing + reconnecting --- .../affected/npm-packages.spec.ts | 2 +- .../project-graph/plugins/isolation/index.ts | 20 +--------------- .../plugins/isolation/plugin-pool.ts | 24 +++++++------------ 3 files changed, 11 insertions(+), 35 deletions(-) diff --git a/packages/nx/src/plugins/js/project-graph/affected/npm-packages.spec.ts b/packages/nx/src/plugins/js/project-graph/affected/npm-packages.spec.ts index b5c7992a4ed85..a5ea8032ff64d 100644 --- a/packages/nx/src/plugins/js/project-graph/affected/npm-packages.spec.ts +++ b/packages/nx/src/plugins/js/project-graph/affected/npm-packages.spec.ts @@ -296,7 +296,7 @@ describe('getTouchedNpmPackages', () => { }); it('should handle and log workspace package.json changes when the changes are not in `npmPackages` (projectGraph.externalNodes)', () => { - jest.spyOn(logger, 'warn'); + jest.spyOn(logger, 'warn').mockImplementation(() => {}); expect(() => { getTouchedNpmPackages( [ diff --git a/packages/nx/src/project-graph/plugins/isolation/index.ts b/packages/nx/src/project-graph/plugins/isolation/index.ts index 7b6e9af11460f..4bab409265189 100644 --- a/packages/nx/src/project-graph/plugins/isolation/index.ts +++ b/packages/nx/src/project-graph/plugins/isolation/index.ts @@ -3,33 +3,15 @@ import { PluginConfiguration } from '../../../config/nx-json'; import { LoadedNxPlugin } from '../internal-api'; import { loadRemoteNxPlugin } from './plugin-pool'; -/** - * Used to ensure 1 plugin : 1 worker - */ -const remotePluginCache = new Map< - string, - readonly [Promise, () => void] ->(); - export async function loadNxPluginInIsolation( plugin: PluginConfiguration, root = workspaceRoot ): Promise, () => void]> { - const cacheKey = JSON.stringify(plugin); - - if (remotePluginCache.has(cacheKey)) { - return remotePluginCache.get(cacheKey); - } - const [loadingPlugin, cleanup] = await loadRemoteNxPlugin(plugin, root); - // We clean up plugin workers when Nx process completes. - const val = [ + return [ loadingPlugin, () => { cleanup(); - remotePluginCache.delete(cacheKey); }, ] as const; - remotePluginCache.set(cacheKey, val); - return val; } diff --git a/packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts b/packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts index 232fe0b63d10a..df76b0309a193 100644 --- a/packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts +++ b/packages/nx/src/project-graph/plugins/isolation/plugin-pool.ts @@ -37,19 +37,12 @@ export async function loadRemoteNxPlugin( plugin: PluginConfiguration, root: string ): Promise<[Promise, () => void]> { - const cacheKey = JSON.stringify(plugin); + const cacheKey = JSON.stringify({ plugin, root }); if (nxPluginWorkerCache.has(cacheKey)) { return [nxPluginWorkerCache.get(cacheKey), () => {}]; } - const { ipcPath, worker } = await startPluginWorker(); - - const socket = await new Promise((res, rej) => { - const socket = connect(ipcPath, () => { - res(socket); - }); - socket.on('error', rej); - }); + const { worker, socket } = await startPluginWorker(); const pendingPromises = new Map(); @@ -317,14 +310,16 @@ async function startPluginWorker() { let attempts = 0; return new Promise<{ worker: ChildProcess; - ipcPath: string; + socket: Socket; }>((resolve, reject) => { const id = setInterval(async () => { - if (await isServerAvailable(ipcPath)) { + const socket = await isServerAvailable(ipcPath); + if (socket) { + // socket.unref(); clearInterval(id); resolve({ worker, - ipcPath, + socket, }); } else if (attempts > 1000) { // daemon fails to start, the process probably exited @@ -337,12 +332,11 @@ async function startPluginWorker() { }); } -function isServerAvailable(ipcPath: string): Promise { +function isServerAvailable(ipcPath: string): Promise { return new Promise((resolve) => { try { const socket = connect(ipcPath, () => { - socket.destroy(); - resolve(true); + resolve(socket); }); socket.once('error', () => { resolve(false);