Skip to content

Starscream hangs when NSStream closes due to connectivity issues #164

@dkharrat

Description

@dkharrat

I was testing the case when my websocket server is down, and noticed that Starscream hangs when it receives the NSStreamEvent.ErrorOccurred code after initialization in initStreamsWithData.

Specifically, it hangs on the following line in disconnectStream():

writeQueue.waitUntilAllOperationsAreFinished()

I tracked it down, and it seems that it's due to the following operation:

    writeQueue.addOperationWithBlock {
        while !outStream.hasSpaceAvailable {
            usleep(100) //wait until the socket is ready
        }
        outStream.write(bytes, maxLength: data.length)
    }

Essentially, there's an infinite loop in the while loop waiting for the stream to be available, as outStream.hasSpaceAvailable is never true. This causes a deadlock in the disconnect waiting for writeQueue to be empty. I was able to resolve it as follows:

    writeQueue.addOperationWithBlock {
        while outStream.streamError == nil && !outStream.hasSpaceAvailable {
            usleep(100) //wait until the socket is ready
        }
        outStream.write(bytes, maxLength: data.length)
    }

Is this the right fix? If so, I can open a pull request with the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions