Skip to content

Commit 1bb96b7

Browse files
CammisuliFrozenPandaz
authored andcommitted
fix(core): check if socketMessenger is null before using (#18116)
(cherry picked from commit eecbde5)
1 parent 93a0200 commit 1bb96b7

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

packages/nx/src/daemon/client/client.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export type ChangedFile = {
3737
type: 'create' | 'update' | 'delete';
3838
};
3939

40+
enum DaemonStatus {
41+
CONNECTING,
42+
DISCONNECTED,
43+
CONNECTED,
44+
}
45+
4046
export class DaemonClient {
4147
constructor(private readonly nxJson: NxJsonConfiguration) {
4248
this.reset();
@@ -50,7 +56,9 @@ export class DaemonClient {
5056
private currentReject;
5157

5258
private _enabled: boolean | undefined;
53-
private _connected: boolean;
59+
private _daemonStatus: DaemonStatus = DaemonStatus.DISCONNECTED;
60+
private _waitForDaemonReady: Promise<void> | null = null;
61+
private _daemonReady: () => void | null = null;
5462
private _out: FileHandle = null;
5563
private _err: FileHandle = null;
5664

@@ -104,7 +112,10 @@ export class DaemonClient {
104112
this._out = null;
105113
this._err = null;
106114

107-
this._connected = false;
115+
this._daemonStatus = DaemonStatus.DISCONNECTED;
116+
this._waitForDaemonReady = new Promise<void>(
117+
(resolve) => (this._daemonReady = resolve)
118+
);
108119
}
109120

110121
async requestShutdown(): Promise<void> {
@@ -149,27 +160,28 @@ export class DaemonClient {
149160
) => void
150161
): Promise<UnregisterCallback> {
151162
await this.getProjectGraph();
152-
const messenger = new SocketMessenger(connect(FULL_OS_SOCKET_PATH)).listen(
153-
(message) => {
154-
try {
155-
const parsedMessage = JSON.parse(message);
156-
callback(null, parsedMessage);
157-
} catch (e) {
158-
callback(e, null);
159-
}
160-
},
161-
() => {
162-
callback('closed', null);
163-
},
164-
(err) => callback(err, null)
165-
);
166-
167-
await this.queue.sendToQueue(() =>
168-
messenger.sendMessage({ type: 'REGISTER_FILE_WATCHER', config })
169-
);
163+
let messenger: SocketMessenger | undefined;
164+
165+
await this.queue.sendToQueue(() => {
166+
messenger = new SocketMessenger(connect(FULL_OS_SOCKET_PATH)).listen(
167+
(message) => {
168+
try {
169+
const parsedMessage = JSON.parse(message);
170+
callback(null, parsedMessage);
171+
} catch (e) {
172+
callback(e, null);
173+
}
174+
},
175+
() => {
176+
callback('closed', null);
177+
},
178+
(err) => callback(err, null)
179+
);
180+
return messenger.sendMessage({ type: 'REGISTER_FILE_WATCHER', config });
181+
});
170182

171183
return () => {
172-
messenger.close();
184+
messenger?.close();
173185
};
174186
}
175187

@@ -232,7 +244,7 @@ export class DaemonClient {
232244
// it's ok for the daemon to terminate if the client doesn't wait on
233245
// any messages from the daemon
234246
if (this.queue.isEmpty()) {
235-
this._connected = false;
247+
this.reset();
236248
} else {
237249
output.error({
238250
title: 'Daemon process terminated and closed the connection',
@@ -280,12 +292,17 @@ export class DaemonClient {
280292
}
281293

282294
private async sendMessageToDaemon(message: Message): Promise<any> {
283-
if (!this._connected) {
284-
this._connected = true;
295+
if (this._daemonStatus == DaemonStatus.DISCONNECTED) {
296+
this._daemonStatus = DaemonStatus.CONNECTING;
297+
285298
if (!(await this.isServerAvailable())) {
286299
await this.startInBackground();
287300
}
288301
this.setUpConnection();
302+
this._daemonStatus = DaemonStatus.CONNECTED;
303+
this._daemonReady();
304+
} else if (this._daemonStatus == DaemonStatus.CONNECTING) {
305+
await this._waitForDaemonReady;
289306
}
290307

291308
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy