Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): daemon termination should be ok when client does not need … #12519

Merged
merged 1 commit into from Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/nx/src/daemon/client/client.ts
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
@@ -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;
}
}