Skip to content

php-fpm: graceful restart without blocking/losing requests #3758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions sapi/fpm/fpm/fpm_process_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ static void fpm_pctl_exit() /* {{{ */

static void fpm_pctl_exec() /* {{{ */
{
zlog(ZLOG_DEBUG, "Blocking some signals before reexec");
if (0 > fpm_signals_block()) {
zlog(ZLOG_WARNING, "concurrent reloads may be unstable");
switch (fork()) {
case 0:
break;
case -1:
zlog(ZLOG_SYSERROR, "failed to reload: fork() failed");
/* no break */
default:
fpm_global_config.pid_file = NULL;
return;
}

zlog(ZLOG_NOTICE, "reloading: execvp(\"%s\", {\"%s\""
"%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
"%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s" "%s%s%s"
Expand All @@ -99,6 +104,11 @@ static void fpm_pctl_exec() /* {{{ */
optional_arg(10)
);

zlog(ZLOG_DEBUG, "Blocking some signals before reexec");
if (0 > fpm_signals_block()) {
zlog(ZLOG_WARNING, "concurrent reloads may be unstable");
}

fpm_cleanups_run(FPM_CLEANUP_PARENT_EXEC);
execvp(saved_argv[0], saved_argv);
zlog(ZLOG_SYSERROR, "failed to reload: execvp() failed");
Expand All @@ -110,9 +120,8 @@ static void fpm_pctl_action_last() /* {{{ */
{
switch (fpm_state) {
case FPM_PCTL_STATE_RELOADING:
fpm_pctl_exec();
break;

zlog(ZLOG_NOTICE, "exiting after reload");
exit(FPM_EXIT_OK);
case FPM_PCTL_STATE_FINISHING:
case FPM_PCTL_STATE_TERMINATING:
fpm_pctl_exit();
Expand Down Expand Up @@ -199,6 +208,10 @@ static void fpm_pctl_action_next() /* {{{ */
fpm_pctl_kill_all(sig);
fpm_signal_sent = sig;
fpm_pctl_timeout_set(timeout);

if (fpm_signal_sent == SIGQUIT && fpm_state == FPM_PCTL_STATE_RELOADING) {
fpm_pctl_exec();
}
}
/* }}} */

Expand Down
12 changes: 11 additions & 1 deletion sapi/fpm/fpm/fpm_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */

if (which == FPM_CLEANUP_PARENT_EXIT_MAIN) {
if (ls->type == FPM_AF_UNIX) {
unlink(ls->key);
struct sockaddr_un sa_un;

memset(&sa_un, 0, sizeof(sa_un));
strlcpy(sa_un.sun_path, ls->key, sizeof(sa_un.sun_path));
sa_un.sun_family = AF_UNIX;

if (fpm_socket_unix_test_connect(&sa_un, sizeof(sa_un)) == 0) {
zlog(ZLOG_WARNING, "Keeping unix socket, another FPM instance seems to already listen on %s", ls->key);
} else {
unlink(ls->key);
}
}
}
free(ls->key);
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/tests/bug68442-signal-reload.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ $tester->ping('{{ADDR}}');
$tester->signal('USR2');
$tester->expectLogNotice('Reloading in progress ...');
$tester->expectLogNotice('reloading: .*');
$tester->expectLogNotice('exiting after reload');
$tester->expectLogNotice('using inherited socket fd=\d+, "127.0.0.1:\d+"');
$tester->expectLogStartNotices();
$tester->ping('{{ADDR}}');
$tester->signal('TERM');
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/tests/bug74083-concurrent-reload.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ $tester->getLogLines(2000);
$tester->signal('USR2');
$tester->expectLogNotice('Reloading in progress ...');
$tester->expectLogNotice('reloading: .*');
$tester->expectLogNotice('exiting after reload');
$tester->expectLogNotice('using inherited socket fd=\d+, "127.0.0.1:\d+"');
$tester->expectLogStartNotices();
$tester->ping('{{ADDR}}');

$tester->signal('TERM');
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();
Expand Down
2 changes: 2 additions & 0 deletions sapi/fpm/tests/bug76601-reload-child-signals.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ if (!$tester->expectLogNotice('reloading: .*')) {
echo "Skipped messages\n";
echo implode('', $skipped);
}
$tester->expectLogNotice('exiting after reload');
$tester->expectLogNotice('using inherited socket fd=\d+, "127.0.0.1:\d+"');
$tester->expectLogStartNotices();

$tester->signal('TERM');
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();
Expand Down
3 changes: 3 additions & 0 deletions sapi/fpm/tests/bug77934-reload-process-control.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ $tester->ping('{{ADDR}}');
$tester->signal('USR2');
$tester->expectLogNotice('Reloading in progress ...');
$tester->expectLogNotice('reloading: .*');
$tester->expectLogNotice('exiting after reload');
$tester->expectLogNotice('using inherited socket fd=\d+, "127.0.0.1:\d+"');
$tester->expectLogStartNotices();
$tester->ping('{{ADDR}}');

$tester->signal('TERM');
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();
Expand Down
3 changes: 3 additions & 0 deletions sapi/fpm/tests/reload-uses-sigkill-as-last-measure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ sleep(2);
$tester->expectLogNotice('using inherited socket fd=\d+, "127.0.0.1:\d+"');
$tester->expectLogStartNotices();
$tester->ping('{{ADDR}}');
$tester->expectLogNotice('exiting after reload');

$tester->signal('TERM');
$tester->terminate();
$tester->expectLogTerminatingNotices();
$tester->close();
Expand Down