From 5fdb25aa87d1bc8406a5d6f9e6e811f442c20abe 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 d21e829e022a14..04ac00ec0963aa 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 d33b0445004bae..63a6be74bc0371 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; + } }