fuimaz / spserver

Automatically exported from code.google.com/p/spserver
Other
0 stars 0 forks source link

SP_LFServer shutdowns on any catched signal (including SIGUSRx) #28

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hello.

I'm writing simple http-server which processes some signals such as 
SIGUSRx, and using SP_LFServer for it (like in testhttp.cpp). But I've 
found that my server shuts down on the next request after it received 
SIGUSRx signal. After some investigation I've found that SP_LFServer uses 
pause() in SP_LFServer::runForever(), which "causes the calling process (or 
thread) to sleep until a signal is delivered that either terminates the 
process or causes the invocation of a signal-catching function", which is 
my case. Isn't is more convenient to use sigwait(), specifying SIGKILL and 
SIGTERM for it, than simple pause()? Below is the code I'm using and it 
seems it works for me.

void SP_LFServer :: runForever()
{
    run();
    sigset_t ss;
    sigemptyset(&ss);
    sigaddset(&ss, SIGKILL);
    sigaddset(&ss, SIGTERM);
    int signum;
    sigwait(&ss, &signum);
    struct sigaction sa;
    sigaction(signum, 0, &sa);
    sa.sa_handler(signum);
    //pause();
}

Original issue reported on code.google.com by artem.iglikov on 2 Feb 2010 at 3:12

GoogleCodeExporter commented 9 years ago
Thanks for your patch. The runForever is a workaround, just for those testcases.

The real applications maybe use more complex solution to shutdown the server 
according 
to their requirements, so I left the problem to the developer himself.

Original comment by liusi...@gmail.com on 12 Feb 2010 at 2:52

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
The applications maybe like the following:

static int gExitSignal = 0;

static void sigHandler(const int sig)
{
    gExitSignal = sig;
}

int main( int argc, char * argv[] )
{
    signal(SIGINT, sigHandler);
    signal(SIGTERM, sigHandler);
    signal( ...... );
    ......

    ForkAsDaemon();

    SP_LFServer server( "", port,
        new SP_HttpHandlerAdapterFactory( new SP_HttpEchoHandlerFactory() ) );

    ......

    server.run();

    for( ; ; ) {
        sleep( 1 );

        if( 0 != gExitSignal ) {
            sp_syslog( LOG_NOTICE, "exit signal %d", gExitSignal );
            break;
        }
    }    

    // do something, such as to close database connection,
    // to close the opening file
}

Original comment by liusi...@gmail.com on 12 Feb 2010 at 3:02