zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.74k stars 2.36k forks source link

problem: pthread_cond_wait doesn't get signaled by ctrl+c #3372

Open somdoron opened 5 years ago

somdoron commented 5 years ago

Issue description

pthread_cond_wait is not being in interrupted by ctrl+c and I cannot stop an app that waits on a thread safe socket.

Environment

Minimal test code / Steps to reproduce the issue


#include <zmq.h>
#include <stdio.h>

void my_handler(int s){
    printf("Caught signal %d\n",s);
}
int main() {
    struct sigaction sigIntHandler;

    sigIntHandler.sa_handler = my_handler;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;

    sigaction(SIGINT, &sigIntHandler, NULL);

    void *ctx = zmq_ctx_new ();
    void *client = zmq_socket (ctx, ZMQ_CLIENT);
    zmq_msg_t msg;
    zmq_msg_init (&msg);
    zmq_msg_recv (&msg, client, 0);

    return 0;
}

# What's the actual result? (include assertion message & call stack if applicable)

# What's the expected result?
the application should stop after pressing ctrl+c
sigiesec commented 5 years ago

Can you provide an example using libzmq (rather than czmq)?

somdoron commented 5 years ago

@sigiesec changed.

If using dealer instead of client the app do exit.

somdoron commented 5 years ago

A solution might be, to provide a context function that signal all thread safe sockets. So a user can call it when interrupted. CZMQ will probably do it by default.

bluca commented 5 years ago

could the timed version be used instead? so that it can check for interruption

somdoron commented 5 years ago

How can you check for interruption?

bluca commented 5 years ago

can't remember off the top of my head, but we are somehow doing it for the non-safe sockets right?

somdoron commented 5 years ago

Also, it might make a performance problem, exiting and entering a mutex...

somdoron commented 5 years ago

@bluca, no, the behavior of poll/select and pthread_cond_wait is different. poll get interrupted and return EINTR, pthread_cond_wait is not.

bluca commented 5 years ago

ah true, forgot about that

bluca commented 5 years ago

I guess then an API as you suggested is the easiest thing to get it working