SIGPIPE on client disconnect SSE handler

13 views
Skip to first unread message

leonard...@gmail.com

unread,
Oct 1, 2016, 2:10:54 AM10/1/16
to onion-dev
Perhaps I'm doing this wrong, but I'm looking to create an Server Side Event server running the handler in a loop. All works brilliantly unless the client terminates the connection, and then we get a segmentation SIGPIPE error. 

I've tried several types of signal handler options that work. Nothing seems to avoid the segmentation fail within the pthreadt; however replacing 'write' with 'send' in the pipe writer works. EPIPE can be checked for an error.

Test code follows:

onion_connection_status sse_handler(void *data, onion_request *req, onion_response *res) {

    char resp[40];
    int x = 0;    
    while (1) {

        snprintf(resp, 40,"data: foobar back to you %d\n\n", x++);
        onion_response_write0(res, resp);
        if (errno == EPIPE) {
            errno = 0;
            break;
        }
        onion_response_flush(res);
        sleep(5);
    }
    return OCS_PROCESSED;
}

ssize_t onion_http_write(onion_request *con, const char *data, size_t len) {
    return send(con->connection.fd, data, len, MSG_NOSIGNAL );                  // Works on client disconnect; sets errno
    //return write(con->connection.fd, data, len);                              // Segmentation fault on client disconnect
}

Should this go into base?

Cheers, 



David Moreno Montero

unread,
Oct 1, 2016, 6:09:30 AM10/1/16
to leonard...@gmail.com, onion-dev
Onion should ignore SIGPIPE by default, unless you set the O_NO_SIGPIPE flag at onion_new. (https://github.com/davidmoreno/onion/blob/master/src/onion/onion.c#L215)

Anyway your proposed solution looks even better for me. I'm not sure if then the flag should be removed (does it affect to https handler?) and maybe also use recv.

Can you prepare a pull request with the change?

Regards,
David.

--
You received this message because you are subscribed to the Google Groups "onion-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to onion-dev+unsubscribe@coralbits.com.
To post to this group, send email to onio...@coralbits.com.
Visit this group at https://groups.google.com/a/coralbits.com/group/onion-dev/.
For more options, visit https://groups.google.com/a/coralbits.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages