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.
I was testing the case when my websocket server is down, and noticed that Starscream hangs when it receives the
NSStreamEvent.ErrorOccurredcode after initialization ininitStreamsWithData.Specifically, it hangs on the following line in
disconnectStream():I tracked it down, and it seems that it's due to the following operation:
Essentially, there's an infinite loop in the while loop waiting for the stream to be available, as
outStream.hasSpaceAvailableis never true. This causes a deadlock in the disconnect waiting forwriteQueueto be empty. I was able to resolve it as follows:Is this the right fix? If so, I can open a pull request with the fix.