Starscream icon indicating copy to clipboard operation
Starscream copied to clipboard

Infinite loop on error in NativeEngine

Open jagdeep-manik opened this issue 4 years ago • 1 comments

Reproduction steps

  • Disconnect yourself from the internet
  • Establish a WebSocket using the native engine
  • Set up a delegate callback for .error(...)
  • The framework will infinitely loop through doRead() and keep calling .error. Disconnecting the socket doesn't fix the issue and neither does deinitializing the WebSocket, since there is a leak in NativeEngine.

Suggestion

  • Only recursively call doRead on success:
    private func doRead() {
        task?.receive { [weak self] (result) in
            switch result {
            case .success(let message):
                switch message {
                case .string(let string):
                    self?.broadcast(event: .text(string))
                case .data(let data):
                    self?.broadcast(event: .binary(data))
                @unknown default:
                    break
                }
                
                // Only recurse doRead on success
                self?.doRead()
                
            case .failure(let error):
                self?.broadcast(event: .error(error))
            }
        }
    }

jagdeep-manik avatar Jun 10 '20 03:06 jagdeep-manik

Hi, do you plan to merge it?

Arcikbtw avatar Mar 06 '21 06:03 Arcikbtw