summaryrefslogtreecommitdiff
path: root/php-fcgi-graceful.patch
blob: 96c6496e02ae6176c527dd54a8bc72baa72de66a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Related links:

http://bugs.php.net/bug.php?id=43224 this patch

http://bugs.php.net/bug.php?id=41593
http://bugs.php.net/bug.php?id=36158
http://php-fpm.anight.org/

test script too:
<?php
echo php_sapi_name(), ' running ', PHP_VERSION, "<br>\n";
$i = 0;
while ($i < 35) {
    echo (++$i), "<br>\n";
    flush();
    sleep(1);
}
echo "end!<br>\n";
?>

--- php-7.1.0alpha2/sapi/cgi/cgi_main.c~	2016-07-04 14:56:49.000000000 +0300
+++ php-7.1.0alpha2/sapi/cgi/cgi_main.c	2016-07-04 14:58:17.522958766 +0300
@@ -101,6 +101,9 @@
  */
 static int parent = 1;
 
+/* Socket we are listening on incoming connections */
+static int fcgi_fd = 0;
+
 #ifndef PHP_WIN32
 /* Did parent received exit signals SIG_TERM/SIG_INT/SIG_QUIT */
 static int exit_signal = 0;
@@ -1221,6 +1224,21 @@
 }
 #endif
 
+/**
+ * Graceful shutdown. Close listening sockets.
+ */
+void fastcgi_graceful_shutdown(int signal)
+{
+#ifdef DEBUG_FASTCGI
+	fprintf(stderr, "FastCGI graceful shutdown, pid %d\n", getpid());
+#endif
+
+	/* Close the listening socket so new processes can reuse the same port */
+	closesocket(fcgi_fd);
+	fcgi_fd = 0;
+}
+
+
 PHP_INI_BEGIN()
 	STD_PHP_INI_BOOLEAN("cgi.rfc2616_headers",     "0",  PHP_INI_ALL,    OnUpdateBool,   rfc2616_headers, php_cgi_globals_struct, php_cgi_globals)
 	STD_PHP_INI_BOOLEAN("cgi.nph",                 "0",  PHP_INI_ALL,    OnUpdateBool,   nph, php_cgi_globals_struct, php_cgi_globals)
@@ -1764,7 +1764,6 @@
 	int requests = 0;
 	int fastcgi;
 	char *bindpath = NULL;
-	int fcgi_fd = 0;
 	fcgi_request *request = NULL;
 	int warmup_repeats = 0;
 	int repeats = 1;
@@ -2080,9 +2080,14 @@
 						parent = 0;
 
 						/* don't catch our signals */
-						sigaction(SIGTERM, &old_term, 0);
 						sigaction(SIGQUIT, &old_quit, 0);
 						sigaction(SIGINT,  &old_int,  0);
+
+						/* call graceful shutdown handler for SIGTERM */
+						act.sa_flags = 0;
+						act.sa_handler = fastcgi_graceful_shutdown;
+						sigaction(SIGTERM, &act, &old_term);
+
 						zend_signal_init();
 						break;
 					case -1: