Skip to content

Conversation

@shaynevanasperen
Copy link
Contributor

@shaynevanasperen shaynevanasperen commented Sep 25, 2025

Fixes #156

This PR fixes a few problems raised in the above issue:

  • Wrong ReconnectionType after receiving Close message from server
  • Duplicate events on DisconnectionHappened stream
  • Sometimes hanging forever when trying to connect

I have split the changes into separate commits, for easier reviewing. I've also taken the opportunity to improve the tests, by making them faster and more debuggable. I've also added some public members for better diagnotics/debugging support.

* Expose new properties on IWebsocketClient (TextSenderRunning, BinarySenderRunning, IsInsideLock)
* Use Debug log level when receiving a close message and creating new send threads
* Write a Debug log message when the sending threads are shutting down
… events

* Use ReconnectionType.ByServer when reconnecting in response to receiving a close message from the server
* Actually start the client a 2nd time in the test for that
* Fixes an issue where the connection factory occasionally just hangs forever
* Expose a new property for configuring how long to wait before timing out (defaulted to 2 seconds)
* Ignore duplicate calls to Dispose
* Use try methods for closing channel writers in Dispose
* Update IsRunning flag immediately within exception handler in StartClient
* Skip reconnecting on error after waiting if the client is already reconnecting by user request
Copy link
Owner

@Marfusios Marfusios left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks, I will test this locally before deploying

StartBackgroundThreadForSendingBinary();
}

BinarySenderRunning = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably better put it into finally block

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but you already merged it now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, np, I will update it

_client?.Dispose();

if (!IsReconnectionEnabled || disInfo.CancelReconnection)
if (type != ReconnectionType.Error && (!IsReconnectionEnabled || disInfo.CancelReconnection))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this change, it could affect existing implementations that rely on auto reconnection. I will test it locally

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, the unit test Starting_WithServerDelay_RetriesAfterConnectionTimeout fails. With it, all the unit test pass.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a little worried it could stop the reconnection, but actually, it is the opposite; in case of error, it will always reconnect.
Looks good to me

}
catch (Exception e)
{
IsRunning = _client?.State == WebSocketState.Open;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this possible to happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the exception could get thrown unexpectedly (perhaps even in future after other changes happen to this code). This is just here to be extra safe and make sure that we set the IsRunning property to false in case it is true at this point. I was just being pessimistic with this line of code. It can be removed if you want.

@Marfusios Marfusios merged commit c1ec006 into Marfusios:master Sep 26, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Occasionally hangs when trying to reconnect

2 participants