Quick calls to connect() results in thread leaks
Discovered inadvertently during the investigation of https://github.com/ably/ably-java/issues/917.
If you use the autoconnect option in ClientOptions and also call connect on the ably instance, a race condition exists whereby threads are leaked from the underlying websocket handler.
This is because starting connections discards the current transport, calling transport.close(). The underlying websocket library only cleans up threads properly if the write thread (which is started by the read thread) isn't null.
With quick calls to connect, you can end up in a case where the write thread isn't yet started. This means that when the transport is closed, the read thread is left running, the write thread gets created - and neither can be cleaned up.
The issue is complicated by the fact that connecting is an asynchronous state change.
➤ Automation for Jira commented:
The link to the corresponding Jira issue is https://ably.atlassian.net/browse/SDK-3329