[php-maint] Bug#349891: ``Incomplete headers'' problem caused by bad SIGTERM handling in php4 under FastCGI

Chris Lightfoot chris at mysociety.org
Wed Jan 25 20:12:15 UTC 2006


Package: php4-cgi
Version: 4.3.10-16

The FastCGI code in php4 does not handle SIGTERM
correctly. It should catch the signal and shut down
gracefully; instead it terminates immediately, even if
part-way through handling a request; in such a case, the
server will log ``Incomplete headers (0 bytes) received
from server...'' and the client will see a 500 HTTP error.
Since apache mod_fastcgi uses SIGTERM to (e.g.) shut down
excess dynamic FastCGI servers during the normal course of
operations, this is a problem in practice.

The problem is fixed by the patch below, against 4.3.10-16
(but should apply cleanly to later versions); I've put a
copy of this at
    http://bitter.ukcod.org.uk/~chris/tmp/20060125/php-4.3.10-fastcgi-sigterm-fix.patch
in case it hasn't pasted cleanly (php uses tabs in source
code :-( ). Any chance this could make it into php4-cgi in
stable?

--- php4-4.3.10/sapi/cgi/cgi_main.c.orig	2004-07-14 23:38:18.000000000 +0100
+++ php4-4.3.10/sapi/cgi/cgi_main.c	2006-01-25 19:38:24.000000000 +0000
@@ -948,6 +957,18 @@
 }
 #endif
 
+#if PHP_FASTCGI
+static sig_atomic_t fastcgi_finish;
+void fastcgi_child_terminate(int signal)
+{
+#ifdef DEBUG_FASTCGI
+	fprintf( stderr, "FastCGI termination signalled, pid %d\n", getpid() );
+#endif
+
+	fastcgi_finish = 1;
+}
+#endif
+
 /* {{{ main
  */
 int main(int argc, char *argv[])
@@ -1182,6 +1203,12 @@
 		php_php_import_environment_variables = php_import_environment_variables;
 		php_import_environment_variables = cgi_php_import_environment_variables;
 
+		/* Need to shut down cleanly on SIGTERM. */
+		act.sa_flags = 0;
+		act.sa_handler = fastcgi_child_terminate;
+		sigaction(SIGTERM, &act, NULL);
+		old_term = act;
+
 		/* library is already initialized, now init our request */
 		FCGX_Init();
 		FCGX_InitRequest( &request, fcgi_fd, 0 );
@@ -1304,7 +1331,7 @@
 #endif
 
 		while (!fastcgi
-			|| FCGX_Accept_r( &request ) >= 0) {
+			|| (!fastcgi_finish && FCGX_Accept_r( &request ) >= 0)) {
 #endif
 
 #if PHP_FASTCGI
@@ -1662,18 +1689,21 @@
 			if (!fastcgi) break;
 			/* only fastcgi will get here */
 			requests++;
-			if(max_requests && (requests == max_requests)) {
-				FCGX_Finish_r(&request);
-#ifndef PHP_WIN32
-				if (bindpath) {
-					free(bindpath);
-				}
-#endif
+			if(max_requests && (requests == max_requests))
 				break;
-			}
+
 			/* end of fastcgi loop */
 		}
 #endif
+		if (fastcgi) {
+			FCGX_Finish_r(&request);
+
+#ifndef PHP_WIN32
+			if (bindpath) {
+				free(bindpath);
+			}
+#endif
+		}
 
 		if (cgi_sapi_module.php_ini_path_override) {
 			free(cgi_sapi_module.php_ini_path_override);

-- 
``Dear Mr. Gadaffi,
  You must be very proud. It's not every day that a duck becomes president.''
  (from `I'm Sorry, I Haven't A Clue')




More information about the pkg-php-maint mailing list