From 7865e6a91c87fce16461042cde3bbd87e88a00aa Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Tue, 11 Oct 2022 11:08:02 -0400 Subject: [PATCH] fix(core): daemon termination should be ok when client does not need anything (#12519) (cherry picked from commit 8c393b5660eba4d8404b8194ff139497aa1636fa) --- packages/nx/src/daemon/client/client.ts | 18 +++++++++++++----- packages/nx/src/utils/promised-based-queue.ts | 10 ++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index d21e829e022a1..04ac00ec0963a 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -161,11 +161,19 @@ export class DaemonClient { }); this.socket.on('close', () => { - output.error({ - title: 'Daemon process terminated and closed the connection', - bodyLines: ['Please rerun the command, which will restart the daemon.'], - }); - process.exit(1); + // it's ok for the daemon to terminate if the client doesn't wait on + // any messages from the daemon + if (this.queue.isEmpty()) { + this._connected = false; + } else { + output.error({ + title: 'Daemon process terminated and closed the connection', + bodyLines: [ + 'Please rerun the command, which will restart the daemon.', + ], + }); + process.exit(1); + } }); this.socket.on('error', (err) => { diff --git a/packages/nx/src/utils/promised-based-queue.ts b/packages/nx/src/utils/promised-based-queue.ts index d33b0445004ba..63a6be74bc037 100644 --- a/packages/nx/src/utils/promised-based-queue.ts +++ b/packages/nx/src/utils/promised-based-queue.ts @@ -1,7 +1,9 @@ export class PromisedBasedQueue { + private counter = 0; private promise = Promise.resolve(null); sendToQueue(fn: () => Promise): Promise { + this.counter++; let res, rej; const r = new Promise((_res, _rej) => { res = _res; @@ -12,17 +14,25 @@ export class PromisedBasedQueue { .then(async () => { try { res(await fn()); + this.counter--; } catch (e) { rej(e); + this.counter--; } }) .catch(async () => { try { res(await fn()); + this.counter--; } catch (e) { rej(e); + this.counter--; } }); return r; } + + isEmpty() { + return this.counter === 0; + } }