Thread: [Cppcms-users] Can't catch signals after service::run()
Brought to you by:
artyom-beilis
|
From: CN <cn...@fa...> - 2013-12-09 15:35:54
|
I have read http://cppcms.com/wikipp/en/page/cppcms_1x_reload_application and the mailing list archive dated 2013-11-19 08:05:50 GMT before posting my question. I need to do some clean-up tasks after the program is killed (kill <pid>), but before it terminates. As shown below, version 1 can catch signals. Version 2 fails to catch signals. I wonder this is because cppcms::service::run() overwrites all the signal specifications set before it. How do I circumvent it? //------------------------------------------------ #include <iostream> #include <signal.h> #include <unistd.h> #include <cstring> #include <cppcms/service.h> #include <cppcms/thread_pool.h> void signal_handler(int signal_number); static bool got_signal=false; int main(int argc,char ** argv) { struct sigaction actions; memset(&actions,0,sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags=0; actions.sa_handler=signal_handler; int sig_result; if((sig_result=sigaction(SIGHUP,&actions,NULL)) != 0){ std::cerr << "sigaction(SIGHUP) failed: " << sig_result << std::endl; std::terminate(); } if((sig_result=sigaction(SIGTERM,&actions,NULL)) != 0){ std::cerr << "sigaction(SIGTERM) failed: " << sig_result << std::endl; std::terminate(); } /* //version 1: signal can be caught. while(true){ std::cerr<<"loop"<<std::endl; if(got_signal){ std::cerr<<"caught signal"<<std::endl; break; } sleep(1); } */ //version 2: signal can NOT be caught. cppcms::thread_pool pool(3); //pools.post(...); cppcms::service s(argc,argv); s.run(); if(got_signal){ pool.stop(); //Stop all in progress threads before program terminates. std::cerr<<"caught signal"<<std::endl; } return 0; } void signal_handler(int signal_number) { got_signal=true; } //------------------------------------------------ Regards, CN -- http://www.fastmail.fm - mmm... Fastmail... |
|
From: Artyom B. <art...@ya...> - 2013-12-09 16:09:05
|
By default, when the SIGTERM is sent to the application, the cppcms::run() exits and you can perform any cleanup you need. No need to do anything special. The situation described in wiki is for reloading (i.e. restarting the service rather than exiting the full program) Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ -------------------------------------------- On Mon, 12/9/13, CN <cn...@fa...> wrote: Subject: [Cppcms-users] Can't catch signals after service::run() To: cpp...@li... Date: Monday, December 9, 2013, 5:35 PM I have read http://cppcms.com/wikipp/en/page/cppcms_1x_reload_application and the mailing list archive dated 2013-11-19 08:05:50 GMT before posting my question. I need to do some clean-up tasks after the program is killed (kill <pid>), but before it terminates. As shown below, version 1 can catch signals. Version 2 fails to catch signals. I wonder this is because cppcms::service::run() overwrites all the signal specifications set before it. How do I circumvent it? //------------------------------------------------ #include <iostream> #include <signal.h> #include <unistd.h> #include <cstring> #include <cppcms/service.h> #include <cppcms/thread_pool.h> void signal_handler(int signal_number); static bool got_signal=false; int main(int argc,char ** argv) { struct sigaction actions; memset(&actions,0,sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags=0; actions.sa_handler=signal_handler; int sig_result; if((sig_result=sigaction(SIGHUP,&actions,NULL)) != 0){ std::cerr << "sigaction(SIGHUP) failed: " << sig_result << std::endl; std::terminate(); } if((sig_result=sigaction(SIGTERM,&actions,NULL)) != 0){ std::cerr << "sigaction(SIGTERM) failed: " << sig_result << std::endl; std::terminate(); } /* //version 1: signal can be caught. while(true){ std::cerr<<"loop"<<std::endl; if(got_signal){ std::cerr<<"caught signal"<<std::endl; break; } sleep(1); } */ //version 2: signal can NOT be caught. cppcms::thread_pool pool(3); //pools.post(...); cppcms::service s(argc,argv); s.run(); if(got_signal){ pool.stop(); //Stop all in progress threads before program terminates. std::cerr<<"caught signal"<<std::endl; } return 0; } void signal_handler(int signal_number) { got_signal=true; } //------------------------------------------------ Regards, CN -- http://www.fastmail.fm - mmm... Fastmail... ------------------------------------------------------------------------------ Sponsored by Intel(R) XDK Develop, test and display web and hybrid apps with a single code base. Download it for free now! http://pubads.g.doubleclick.net/gampad/clk?id=111408631&iu=/4140/ostg.clktrk _______________________________________________ Cppcms-users mailing list Cpp...@li... https://lists.sourceforge.net/lists/listinfo/cppcms-users |
|
From: CN <cn...@fa...> - 2013-12-10 02:14:42
|
Hmm... Below simple example shows that statements following run() never
get executed once the program is killed:
#include <iostream>
#include <cstring>
#include <cppcms/service.h>
#include <cppcms/thread_pool.h>
int main(int argc,char ** argv)
{
cppcms::thread_pool pool(3);
//pool.post(...); //Launch long running threads.
cppcms::service s(argc,argv);
s.run();
//Program counter never reaches here.
std::cerr<<"after run()"<<std::endl;
pool.stop(); //Wait until all threads complete.
return 0;
}
Can incorrect configuration content in config.js cause programs to
malfunction?
Best regards,
CN
On Tue, Dec 10, 2013, at 12:08 AM, Artyom Beilis wrote:
> By default, when the SIGTERM is sent to the application, the
> cppcms::run() exits and you can perform any cleanup you need.
>
> No need to do anything special.
>
> The situation described in wiki is for reloading (i.e. restarting the
> service rather than exiting the full program)
>
> Artyom Beilis
--
http://www.fastmail.fm - Access all of your messages and folders
wherever you are
|