swisspol/GCDWebServer

Protocol wrong type for socket (41)

Closed this issue · 1 comments

(void)_writeData:(NSData*)data withCompletionBlock:(WriteDataCompletionBlock)block
occasionally throws an error "Error while writing to socket 5: Protocol wrong type for socket (41)"

It seems to be related to the issue described here:
http://erickt.github.io/blog/2014/11/19/adventures-in-debugging-a-potential-osx-kernel-bug/

I tried to solve the problem following the solution found in:
https://stackoverflow.com/questions/17948903/dispatch-write-and-dispatch-read-usage
It seems to work. Not 100% sure yet. But it may...

Here is the beginning of the new code for the method:

(void)_writeData:(NSData*)data withCompletionBlock:(WriteDataCompletionBlock)block {

   **dispatch_semaphore_t sem = dispatch_semaphore_create(0);**
    
   dispatch_data_t buffer = dispatch_data_create(
    data.bytes,
    data.length,
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
    **^{ dispatch_semaphore_signal(sem); }**
    );

   dispatch_write(
    _socket,
    buffer,
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
    ^(dispatch_data_t remainingData, int error)
     {
         **if (dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW,100000))!=0)**
         {...}
         ...

Which OSes and devices have you seen this on? How reproducible is this bug? What triggers it?