Starscream
Starscream copied to clipboard
Fix: connect() Does Not Proceed When Peer is Closed 🛠️
Issue Link 🔗
Fixes #1052
Goals ⚽
This PR aims to fix a bug where isConnecting remains true indefinitely, preventing connect() from proceeding after a WebSocket peer closes.
As discussed in #1052, this occurs when:
- The device enters sleep mode (causing WebSocket closure).
- The server goes down during sleep.
- The app tries to reconnect, but isConnecting remains true, blocking new connections forever.
📝 There are alternative steps to reproduce the issue, but this is the simplest method.
In other words, step 2 can be replaced by an extended sleep duration.
Additionally, this PR now includes a fix for another related scenario where isConnecting remains stuck when the WebSocket receives a waiting error.
Implementation Details 🚧
Fix for .peerClosed Issue
- Ensures that isConnecting is properly reset to false when the WebSocket receives a
.peerClosedevent (introduced in #946).
Fix for .waiting(Error?) Issue
- The
waitingstate now includes an optional error (case waiting(Error?)). -
isConnectingis now reset to false when awaitingerror is received. - This prevents the connection from getting stuck in an unusable state.
Testing ✅
- Reproduced both the peerClosed and waiting error issues using the SampleStarscream project.
- Verified that
connect()now proceeds correctly after.peerClosedand.waiting(error), allowing successful reconnections. - Checked logs with
NSLogto confirm thatisConnectingresets properly.