Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Fix the bug that tuner init error does not cause ERROR state (#4953)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzhe-lz authored Jun 22, 2022
1 parent 45af1d6 commit 8b40044
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions ts/nni_manager/core/tuner_command_channel/websocket_channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function serveWebSocket(ws: WebSocket): void {
}

class WebSocketChannelImpl implements WebSocketChannel {
private deferredInit: Deferred<void> = new Deferred<void>();
private deferredInit: Deferred<void> | null = new Deferred<void>();
private emitter: EventEmitter = new EventEmitter();
private heartbeatTimer!: NodeJS.Timer;
private serving: boolean = false;
Expand All @@ -56,8 +56,13 @@ class WebSocketChannelImpl implements WebSocketChannel {

public setWebSocket(ws: WebSocket): void {
if (this.ws !== undefined) {
logger.error('A second client is trying to connect');
ws.close(4030, 'Already serving a tuner.');
logger.error('A second client is trying to connect.');
ws.close(4030, 'Already serving a tuner');
return;
}
if (this.deferredInit === null) {
logger.error('Connection timed out.');
ws.close(4080, 'Timeout');
return;
}

Expand All @@ -72,11 +77,26 @@ class WebSocketChannelImpl implements WebSocketChannel {

this.heartbeatTimer = setInterval(this.heartbeat.bind(this), heartbeatInterval);
this.deferredInit.resolve();
this.deferredInit = null;
}

public init(): Promise<void> {
logger.debug(this.ws === undefined ? 'Waiting connection...' : 'Initialized.');
return this.deferredInit.promise;
if (this.ws === undefined) {
logger.debug('Waiting connection...');
// TODO: This is a quick fix. It should check tuner's process status instead.
setTimeout(() => {
if (this.deferredInit !== null) {
const msg = 'Tuner did not connect in 10 seconds. Please check tuner (dispatcher) log.';
this.deferredInit.reject(new Error('tuner_command_channel: ' + msg));
this.deferredInit = null;
}
}, 10000);
return this.deferredInit!.promise;

} else {
logger.debug('Initialized.');
return Promise.resolve();
}
}

public async shutdown(): Promise<void> {
Expand Down

0 comments on commit 8b40044

Please sign in to comment.