From b91499b2fc0f52ec6e9b8d03f4e707bb27499c6f Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Mon, 19 Mar 2018 22:01:52 -0700 Subject: [PATCH] Cleanup rogue timeout in TS client (#1653) --- client-ts/signalr/spec/HubConnection.spec.ts | 27 ++++++++++++++++++++ client-ts/signalr/src/HubConnection.ts | 10 ++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/client-ts/signalr/spec/HubConnection.spec.ts b/client-ts/signalr/spec/HubConnection.spec.ts index c897c89557..d7d5ff5fbb 100644 --- a/client-ts/signalr/spec/HubConnection.spec.ts +++ b/client-ts/signalr/spec/HubConnection.spec.ts @@ -683,6 +683,33 @@ describe("HubConnection", () => { expect(error).toBeUndefined(); }); + it("does not timeout if message was received before HubConnection.start", async () => { + const connection = new TestConnection(); + const hubConnection = new HubConnection(connection, { ...commonOptions, timeoutInMilliseconds: 100 }); + + const p = new PromiseSource(); + hubConnection.onclose((e) => p.resolve(e)); + + // send message before start to trigger timeout handler + // testing for regression where we didn't cleanup timer if request received before start created a timer + await connection.receive({ type: MessageType.Ping }); + + await hubConnection.start(); + + await connection.receive({ type: MessageType.Ping }); + await delay(50); + await connection.receive({ type: MessageType.Ping }); + await delay(50); + await connection.receive({ type: MessageType.Ping }); + await delay(50); + + connection.stop(); + + const error = await p.promise; + + expect(error).toBeUndefined(); + }); + it("terminates if no messages received within timeout interval", async () => { const connection = new TestConnection(); const hubConnection = new HubConnection(connection, { ...commonOptions, timeoutInMilliseconds: 100 }); diff --git a/client-ts/signalr/src/HubConnection.ts b/client-ts/signalr/src/HubConnection.ts index 93e467f7fa..f6aa33f333 100644 --- a/client-ts/signalr/src/HubConnection.ts +++ b/client-ts/signalr/src/HubConnection.ts @@ -60,9 +60,7 @@ export class HubConnection { } private processIncomingData(data: any) { - if (this.timeoutHandle !== undefined) { - clearTimeout(this.timeoutHandle); - } + this.cleanupTimeout(); if (!this.receivedHandshakeResponse) { data = this.processHandshakeResponse(data); @@ -196,9 +194,9 @@ export class HubConnection { }); this.callbacks.clear(); - this.closedCallbacks.forEach((c) => c.apply(this, [error])); - this.cleanupTimeout(); + + this.closedCallbacks.forEach((c) => c.apply(this, [error])); } public async start(): Promise { @@ -213,6 +211,8 @@ export class HubConnection { this.logger.log(LogLevel.Information, `Using HubProtocol '${this.protocol.name}'.`); + // defensively cleanup timeout in case we receive a message from the server before we finish start + this.cleanupTimeout(); this.configureTimeout(); }