Starscream icon indicating copy to clipboard operation
Starscream copied to clipboard

Fix: connect() Does Not Proceed When Peer is Closed 🛠️

Open yoheimuta opened this issue 1 year ago • 0 comments

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:

  1. The device enters sleep mode (causing WebSocket closure).
  2. The server goes down during sleep.
  3. 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 .peerClosed event (introduced in #946).

Fix for .waiting(Error?) Issue

  • The waiting state now includes an optional error (case waiting(Error?)).
  • isConnecting is now reset to false when a waiting error 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 .peerClosed and .waiting(error), allowing successful reconnections.
  • Checked logs with NSLog to confirm that isConnecting resets properly.

yoheimuta avatar Mar 03 '25 10:03 yoheimuta