Thread: SIGINT not interrupting accept()

  1. #1
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79

    SIGINT not interrupting accept()

    Hi.

    I'm writing a FCGI-app and have come to a problem. I want to handle SIGINT to take down the server under control. But when handling SIGINT the accept-call doesn't get interrupted, so I have to refresh the browser to get use of the socket and then it closes.
    I'm not directly familiar with signals. I obviously got it all wrong. Maybe it doesn't work as I think it does?

    It's multithreaded and the main process set the signal handler

    Code:
    static void _signal_handler(int signum) {
    
    	switch (signum) {
    	case SIGINT:
    		srv_logmsg(_srv, LOG_INFO, "Server shutdown received");
    		FCGX_ShutdownPending();
    		break;
    	}
    
    }
    with
    Code:
    struct sigaction a;
    
    a.sa_handler = _signal_handler;
    a.sa_flags = 0;
    sigemptyset(&a.sa_mask);
    sigaction(SIGINT,  &a, NULL);
    I'm using libfcgi as you can see. All threads are created into:
    Code:
    static void* _thread_start(void* p) {
    
    	struct req req;
    	struct srv* srv = (struct srv*) p;
    
    	req_init(&req, srv);
    
    	while (!srv->shutdown) {
    		pthread_mutex_lock(&srv->mux_accept);
    		if (!(srv->shutdown = FCGX_Accept_r(&req.fcgi))) {
    			pthread_mutex_unlock(&srv->mux_accept);
    			req_setup(&req, srv);
    			FCGX_PutS("Status: 200 Ok\r\nContent-type: text/html\r\n\r\n"
    					"Ok!", req.fcgi.out);
    			req_reset(&req);
    		} else {
    			pthread_mutex_unlock(&srv->mux_accept);
    			break;
    		}
    	}
    
    	req_cleanup(&req);
    
    }
    And the main thread also enter that, after all threads are created.

    FCGX_Requests are created with...
    Code:
    	FCGX_InitRequest(&req->fcgi, srv->fcgi_socket, FCGI_FAIL_ACCEPT_ON_INTR);
    The interrupt on accept() works if I don't handle the interrupt my self, (but then we've don't get a clean shutdown either).
    Last edited by Fader_Berg; 09-13-2016 at 11:58 AM. Reason: main "thread", not "process"

  2. #2
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    I went with calling shutdown() on socket from the signal handler. Don't really like it since feels a bit crude, but the fd is left valid and it does what it takes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGINT and cleanup not working
    By Adam Niazi in forum C Programming
    Replies: 3
    Last Post: 12-03-2014, 06:22 PM
  2. Replies: 4
    Last Post: 05-14-2013, 12:11 PM
  3. accept()
    By ~Kyo~ in forum Networking/Device Communication
    Replies: 11
    Last Post: 11-28-2009, 08:57 PM
  4. Error with accept();
    By xddxogm3 in forum Networking/Device Communication
    Replies: 35
    Last Post: 08-19-2005, 08:32 PM
  5. accept()
    By char in forum C Programming
    Replies: 0
    Last Post: 05-30-2002, 02:22 PM

Tags for this Thread