Skip to content

Commit

Permalink
fix(core): daemon termination should be ok when client does not need …
Browse files Browse the repository at this point in the history
…anything (#12519)

(cherry picked from commit 8c393b5)
  • Loading branch information
vsavkin authored and FrozenPandaz committed Oct 11, 2022
1 parent 26fce5b commit 7865e6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/nx/src/utils/promised-based-queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export class PromisedBasedQueue {
private counter = 0;
private promise = Promise.resolve(null);

sendToQueue(fn: () => Promise<any>): Promise<any> {
this.counter++;
let res, rej;
const r = new Promise((_res, _rej) => {
res = _res;
Expand All @@ -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;
}
}

0 comments on commit 7865e6a

Please sign in to comment.