Re: handling termination in a FastCGI

"Michael Hartley" <michael.hartley-jEoFCcbHCOQze0waTqBjZBy+ShKaMYb+@public.gmane.org>
Newsgroups gmane.comp.web.fastcgi.devel
Organization Openfield Solutions LTD
Message-ID <[email protected]>
Hi

Now this is the FCGI treasure I spent so many hours reading through the   
	undocumented source code for. If you examine the source for FCGX_Accept_r  
you will discover the blocking socket breaks out every 1 second.  By  
setting a shutdownPending flag via the FCGX_ShutdownPending() call, the  
accept routine will not attempt to entry the socket listener section  
again, instead following the clean up process.

Look for FCGX_ShutdownPending();

Kind regards
Michael Hartley
http://www.openfieldsolutions.co.uk




On Tue, 04 Apr 2006 18:57:22 +0100, Basile STARYNKEVITCH  
<[email protected]> wrote:

>
> Dear All,
>
> I don't really understand how to handle graceful termination with a
> FastCGI (running under Debian/Sid Apache2.0.55 mod_fcgid)
>
> Here is my code fragment (I removed includes). Yes I do know that
> calling syslog from a signal handler is unsafe, but in practice it
> is called.
>
>
>
> ################################################################
> volatile int barkpp_again;
>
> static void sigterm_handler(int sig) {
>   barkpp_again = 0;
>   syslog(LOG_INFO, "inside sigterm handler");
> }
>
> int main(int argc, char** argv) {
>   int count=0;
>   FCGX_Stream *in, *out, *err;
>   FCGX_ParamArray envp;
>   barkpp_again = 1;
>   openlog("barkpp-FCGI", LOG_CONS|LOG_NDELAY|LOG_PID, LOG_LOCAL2);
>   syslog(LOG_INFO, "* barkpp pid %d starting compiled " __DATE__ ";"  
> __TIME__ "\n", (int) getpid());
>   signal(SIGTERM, sigterm_handler);
>   while(barkpp_again && FCGX_Accept(&in, &out, &err, &envp) >= 0) {
>     FCGX_FPrintF(out,
> 		 "Content-type: text/html\r\n"
> 		 "\r\n"
> 		 "<html><head><title>test barkcpp</title></head>\n"
> 		 "<body><h1>barkcpp test</h1>\n"
> 		 "<p> pid %d counter %d <small>" __DATE__ " " __TIME__ "</small></p>\n"
> 		 "</body></html>\n",
> 		 (int)getpid(), ++count);
>   }
>   syslog(LOG_INFO, "* barkpp pid %d terminating\n", (int)getpid());
>   return 0;
> }
> ################################################################
>
> My browser behave as expected.
>
> In  /var/log/apache2/error.log
>
> [Tue Apr 04 19:33:54 2006] [notice] mod_fcgid: server  
> /var/www/BARKPP/barkpp.fcgi(13418) started
> [Tue Apr 04 19:39:21 2006] [notice] mod_fcgid: process 13418 going  
> graceful shutdown, sending SIGTERM
> [Tue Apr 04 19:39:27 2006] [warn] mod_fcgid: process 13418 graceful  
> shutdown timeouted, sending SIGKILL
> [Tue Apr 04 19:39:33 2006] [notice] mod_fcgid: process  
> /var/www/BARKPP/barkpp.fcgi(13418) exit(idle timeout), get stop signal 9
>
> In /var/log/messages In only have
> Apr  4 19:33:54 hector barkpp-FCGI[13418]: * barkpp pid 13418 starting  
> compiled Apr  4 2006;19:19:31
> Apr  4 19:39:21 hector barkpp-FCGI[13418]: inside sigterm handler
>
>
> Why is my program not exiting the loop properly? I am expecting that
> the SIGTERM signal sent by Apache would interrupt FCGX_Accept, clear
> barkpp_again (in the called sigterm handler), and exit the loop
> properly. But this is not the case, the sigterm handler is called but
> the terminating syslog is never called, as if there is some loop
> withing FCGX_Accept?
>
>
> Does FCGX_Accept enter some internal poll or select system call?
>
> How do you handle properly termination? The intent would be to save
> persistent FCGI process state in its termination routine....
>
> Regards.



-- 

Michael Hartley
Openfield Solutions Ltd
www.openfieldsolutions.co.uk

___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.