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

[TS] HubConnection stuck in Disconnecting state #30839

Closed
M0ns1gn0r opened this issue Mar 11, 2021 · 3 comments · Fixed by #30948
Closed

[TS] HubConnection stuck in Disconnecting state #30839

M0ns1gn0r opened this issue Mar 11, 2021 · 3 comments · Fixed by #30948
Assignees
Labels
area-signalr Includes: SignalR clients and servers bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@M0ns1gn0r
Copy link

M0ns1gn0r commented Mar 11, 2021

Describe the bug

After I wake up my PC from sleep, the WebSocket connection gets terminated (which is OK). Sometimes it cannot be restarted and then HubConnection.state becomes Disconnecting forever:

  • .start() always throws Cannot start a HubConnection that is not in the 'Disconnected' state.
  • .stop() returns a promise which doesn't resolve ever.

Logs after waking up the PC from sleep:

image


Internals of the corrupted HubConnection:

image

Exceptions (if any)

Error: Cannot start a HubConnection that is not in the 'Disconnected' state.
    at e.<anonymous> (2.1fa56808.chunk.js:formatted:42032)
    at 2.1fa56808.chunk.js:formatted:41930
    at Object.next (2.1fa56808.chunk.js:formatted:41943)
    at 2.1fa56808.chunk.js:formatted:41850
    at new Promise (<anonymous>)
    at C (2.1fa56808.chunk.js:formatted:41829)
    at e.startWithStateTransitions (2.1fa56808.chunk.js:formatted:42026)
    at e.start (2.1fa56808.chunk.js:formatted:42021)
    at e.<anonymous> (SignalRService.ts:39)
    at s (2.1fa56808.chunk.js:formatted:60817)

Further technical details

  • Chrome 89.0.4389.72
  • @microsoft/signalr 5.0.3
  • Azure Functions V3
  • Azure SignalR service in "Serverless" mode.
@javiercn javiercn added the area-signalr Includes: SignalR clients and servers label Mar 11, 2021
@BrennanConroy
Copy link
Member

Can you share the debug logs from the client?

@BrennanConroy BrennanConroy added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Mar 11, 2021
@M0ns1gn0r
Copy link
Author

M0ns1gn0r commented Mar 12, 2021

Explanation

I opened 3 identical browser tabs and put the PC to sleep overnight. Today I woke it up and the connection was successfully restored in all tabs. Then I put the PC to sleep again, after waking up the issue reproduced in all 3 tabs.

1st sleep: reconnected normally

image

2nd sleep: stuck in "Disconnecting"

image

  • SignalRClient is the sink for your library's log messages.

  • SignalRService is our service that starts the connection when a user logs in and stops it when he logs out.

    export class SignalRService {
      private readonly _connection: HubConnection;
      private _accessToken: string | undefined;
    
      constructor() {
        this._connection = new HubConnectionBuilder()
          .withUrl(
            config.serviceUrl,
            {
              withCredentials: false,
              accessTokenFactory: () =>
                this._accessToken ?? Promise.reject("No user is logged in.")
            })
          .withAutomaticReconnect()
          .configureLogging(new TelemetryLoggerAdapter())
          .build();
    
        this._connection.on("jobCancelled", () => { /* irrelevant */});
        this._connection.on("walletBalanceUpdated", () => { /* irrelevant */ });
    
        eventHub.loggedIn.subscribe(this.start.bind(this));
        eventHub.loggedOut.subscribe(this.stop.bind(this));
      }
    
      private async start(userId: string, accessToken: string): Promise<void> {
        this._accessToken = accessToken;
        if (this._connection.connectionId) {
          telemetry.verbose(`SignalRService: connection is already established.`);
          return;
        }
        try {
          telemetry.verbose(`SignalRService: starting the connection.`);
          await this._connection.start();
          telemetry.info(`SignalRService: connected as user '${userId}'.`, { userId });
        } catch (err) {
          telemetry.error(
            `SignalRService: failed to connect as user '${userId}'. ${err}`, { userId, err });
        }
      }
    
      private async stop(): Promise<void> {
        this._accessToken = undefined;
        try {
          telemetry.verbose(`SignalRService: stopping the connection.`);
          await this._connection.stop();
          telemetry.info("SignalRService: disconnected, user logged out.");
        } catch (err) {
          telemetry.error(`SignalRService: failed to disconnect. ${err}`, { err });
        }
      }
    }

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Mar 12, 2021
@BrennanConroy BrennanConroy added bug This issue describes a behavior which is not expected - a bug. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Mar 12, 2021
@BrennanConroy BrennanConroy added this to the 6.0-preview3 milestone Mar 12, 2021
@BrennanConroy BrennanConroy self-assigned this Mar 12, 2021
@BrennanConroy
Copy link
Member

Awesome, thanks for the logs! Sort of figured out what is wrong in the library.

We don't properly handle closing the connection if connection.start errors during reconnect and hubConnection.stop was called.

I was able to write a test to reproduce the issue, so now we can look into fixing it and have a nice regression test 😃

@ghost ghost locked as resolved and limited conversation to collaborators Apr 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-signalr Includes: SignalR clients and servers bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants