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): check if socketMessenger is null before using #18116

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export type ChangedFile = {
type: 'create' | 'update' | 'delete';
};

enum DaemonStatus {
CONNECTING,
DISCONNECTED,
CONNECTED,
}

export class DaemonClient {
constructor(private readonly nxJson: NxJsonConfiguration) {
this.reset();
Expand All @@ -50,7 +56,9 @@ export class DaemonClient {
private currentReject;

private _enabled: boolean | undefined;
private _connected: boolean;
private _daemonStatus: DaemonStatus = DaemonStatus.DISCONNECTED;
private _waitForDaemonReady: Promise<void> | null = null;
private _daemonReady: () => void | null = null;
private _out: FileHandle = null;
private _err: FileHandle = null;

Expand Down Expand Up @@ -104,7 +112,10 @@ export class DaemonClient {
this._out = null;
this._err = null;

this._connected = false;
this._daemonStatus = DaemonStatus.DISCONNECTED;
this._waitForDaemonReady = new Promise<void>(
(resolve) => (this._daemonReady = resolve)
);
}

async requestShutdown(): Promise<void> {
Expand Down Expand Up @@ -149,27 +160,28 @@ export class DaemonClient {
) => void
): Promise<UnregisterCallback> {
await this.getProjectGraph();
const messenger = new SocketMessenger(connect(FULL_OS_SOCKET_PATH)).listen(
(message) => {
try {
const parsedMessage = JSON.parse(message);
callback(null, parsedMessage);
} catch (e) {
callback(e, null);
}
},
() => {
callback('closed', null);
},
(err) => callback(err, null)
);

await this.queue.sendToQueue(() =>
messenger.sendMessage({ type: 'REGISTER_FILE_WATCHER', config })
);
let messenger: SocketMessenger | undefined;

await this.queue.sendToQueue(() => {
messenger = new SocketMessenger(connect(FULL_OS_SOCKET_PATH)).listen(
(message) => {
try {
const parsedMessage = JSON.parse(message);
callback(null, parsedMessage);
} catch (e) {
callback(e, null);
}
},
() => {
callback('closed', null);
},
(err) => callback(err, null)
);
return messenger.sendMessage({ type: 'REGISTER_FILE_WATCHER', config });
});

return () => {
messenger.close();
messenger?.close();
};
}

Expand Down Expand Up @@ -232,7 +244,7 @@ export class DaemonClient {
// 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;
this.reset();
} else {
output.error({
title: 'Daemon process terminated and closed the connection',
Expand Down Expand Up @@ -280,12 +292,17 @@ export class DaemonClient {
}

private async sendMessageToDaemon(message: Message): Promise<any> {
if (!this._connected) {
this._connected = true;
if (this._daemonStatus == DaemonStatus.DISCONNECTED) {
this._daemonStatus = DaemonStatus.CONNECTING;

if (!(await this.isServerAvailable())) {
await this.startInBackground();
}
this.setUpConnection();
this._daemonStatus = DaemonStatus.CONNECTED;
this._daemonReady();
} else if (this._daemonStatus == DaemonStatus.CONNECTING) {
await this._waitForDaemonReady;
}

return new Promise((resolve, reject) => {
Expand Down