daltoniam / Starscream

Websockets in swift for iOS and OSX
Apache License 2.0
8.35k stars 1.21k forks source link

Crash when connecting the socket #1033

Open gouravkmar opened 5 months ago

gouravkmar commented 5 months ago

Since we upgraded to 4.0.8 version, we have noticed a crash in didReceive(event: WebSocketEvent) method after calling the connect() method Below is the screenshot of the crash , it occurs shortly after calling socket's connect() method.

Screenshot 2024-06-14 at 2 36 49 PM

socket initialisation is as below:- ` var request = URLRequest(url: URL(string: "wss://(sSocketServerHostName)")!) request.timeoutInterval = 30

    let cookies = HTTPCookie.requestHeaderFields(with: HTTPCookieStorage.shared.cookies(for: URL(string: sApiServerHostName)!) ?? [])
    let keys = cookies.keys
    for key in keys{
        request.setValue(cookies[key], forHTTPHeaderField: key)
    }
    request.setValue("websocket", forHTTPHeaderField: "Upgrade")
    request.setValue("\(sSocketServerHostName)", forHTTPHeaderField: "Host")
    request.setValue("\(sApiServerHostName)", forHTTPHeaderField: "Origin")
    request.setValue("13", forHTTPHeaderField: "Sec-WebSocket-Version")
    request.setValue("Upgrade", forHTTPHeaderField: "connection")
    socket = WebSocket(request: request)
    socket?.delegate = self
    socket?.connect()`

Environment:

Additional context

Add any other context about the problem here.

CodeKunal commented 5 months ago

@daltoniam please help here.

Charlotte3240 commented 5 months ago

update to xcode 15.3 or xcode 15.4 try it again

DeepBhupatkar commented 2 months ago

Hello, did you manage to resolve the issue? I am encountering the same difficulty. Do you have any suggestions for a solution?

make1a commented 2 months ago

I am still facing the same issue. Do you have any suggestions for a solution?

Charlotte3240 commented 2 months ago

I am still facing the same issue. Do you have any suggestions for a solution?

on Websocket.swift file change this code

    public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true) {
        if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *), !useCustomEngine {
            self.init(request: request, engine: NativeEngine())
        } 
//        else if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) {
//            self.init(request: request, engine: WSEngine(transport: TCPTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
//        } 
        else {
            self.init(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        }
    }
DeepBhupatkar commented 2 months ago

I am still facing the same issue. Do you have any suggestions for a solution?

on Websocket.swift file change this code

    public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true) {
        if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *), !useCustomEngine {
            self.init(request: request, engine: NativeEngine())
        } 
//        else if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) {
//            self.init(request: request, engine: WSEngine(transport: TCPTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
//        } 
        else {
            self.init(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        }
    }

can you describe what will this code do ? i am also facing the crash. and i am only dependent on starScream i am not using native socket by apple.

Charlotte3240 commented 2 months ago

I am still facing the same issue. Do you have any suggestions for a solution?

on Websocket.swift file change this code

    public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true) {
        if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *), !useCustomEngine {
            self.init(request: request, engine: NativeEngine())
        } 
//        else if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) {
//            self.init(request: request, engine: WSEngine(transport: TCPTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
//        } 
        else {
            self.init(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        }
    }

can you describe what will this code do ? i am also facing the crash.

this code for don't use network.framework to transport , iOS 13.0 - use foundation transport

Charlotte3240 commented 2 months ago

I am still facing the same issue. Do you have any suggestions for a solution?

on Websocket.swift file change this code

    public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true) {
        if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *), !useCustomEngine {
            self.init(request: request, engine: NativeEngine())
        } 
//        else if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) {
//            self.init(request: request, engine: WSEngine(transport: TCPTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
//        } 
        else {
            self.init(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        }
    }

can you describe what will this code do ? i am also facing the crash. and i am only dependent on starScream i am not using native socket by apple.

I understand , your crash for the reconnect scene?

you can connect after disconnect 0.5 second

DeepBhupatkar commented 2 months ago

I am still facing the same issue. Do you have any suggestions for a solution?

on Websocket.swift file change this code

    public convenience init(request: URLRequest, certPinner: CertificatePinning? = FoundationSecurity(), compressionHandler: CompressionHandler? = nil, useCustomEngine: Bool = true) {
        if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *), !useCustomEngine {
            self.init(request: request, engine: NativeEngine())
        } 
//        else if #available(macOS 10.14, iOS 12.0, watchOS 5.0, tvOS 12.0, *) {
//            self.init(request: request, engine: WSEngine(transport: TCPTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
//        } 
        else {
            self.init(request: request, engine: WSEngine(transport: FoundationTransport(), certPinner: certPinner, compressionHandler: compressionHandler))
        }
    }

can you describe what will this code do ? i am also facing the crash. and i am only dependent on starScream i am not using native socket by apple.

I understand , your crash for the reconnect scene? you can connect after disconnect 0.5 second

The app is crashing randomly, even though it worked fine when I first ran it on two devices. I've been repeatedly closing and joining the meeting (it's an audio-video calling app), and within milliseconds, it keeps crashing. Sometimes, this happens after 4-5 attempts. However, after a crash, if I try again within 3-4 seconds, it works fine. I have no idea how to resolve this issue or what exactly is happening

Charlotte3240 commented 2 months ago

there has two problem

  1. you should use show your code (fountion transport)
  2. delay some time(e.g. 0.5 or 1 second) reconnect websocket when disconnect and you must be sure disconnected
DeepBhupatkar commented 2 months ago

there has two problem

  1. you should use show your code (fountion transport)
  2. delay some time(e.g. 0.5 or 1 second) reconnect websocket when disconnect and you must be sure disconnected

Sure i will try to do that.