Skip to content

Commit 8d116f7

Browse files
authored
Merge pull request #2743 from xrmx/forward-port-2740
Fix for gracefully_kill_them_all() which broke setups using fifo-based master graceful reload
2 parents 84aa2bd + f053966 commit 8d116f7

File tree

1 file changed

+43
-24
lines changed

1 file changed

+43
-24
lines changed

core/uwsgi.c

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,37 +1355,56 @@ void kill_them_all(int signum) {
13551355

13561356
// gracefully destroy
13571357
void gracefully_kill_them_all(int signum) {
1358+
if (uwsgi_instance_is_dying) return;
1359+
uwsgi.status.gracefully_destroying = 1;
13581360

1359-
int waitpid_status;
1361+
// unsubscribe if needed
1362+
uwsgi_unsubscribe_all();
13601363

1361-
if (uwsgi_instance_is_dying) return;
1362-
uwsgi.status.gracefully_destroying = 1;
1364+
uwsgi_log_verbose("graceful shutdown triggered...\n");
13631365

1364-
// unsubscribe if needed
1365-
uwsgi_unsubscribe_all();
1366+
int i;
1367+
for (i = 1; i <= uwsgi.numproc; i++) {
1368+
if (uwsgi.workers[i].pid > 0) {
1369+
uwsgi.workers[i].shutdown_sockets = 1;
1370+
uwsgi_curse(i, SIGHUP);
1371+
}
1372+
}
13661373

1367-
uwsgi_log_verbose("graceful shutdown triggered...\n");
1374+
for (i = 0; i < uwsgi.mules_cnt; i++) {
1375+
if (uwsgi.mules[i].pid > 0) {
1376+
uwsgi_curse_mule(i, SIGHUP);
1377+
}
1378+
}
13681379

1369-
int i;
1370-
for (i = 1; i <= uwsgi.numproc; i++) {
1371-
if (uwsgi.workers[i].pid > 0) {
1372-
uwsgi.workers[i].shutdown_sockets = 1;
1373-
uwsgi_curse(i, SIGHUP);
1374-
}
1375-
}
1376-
for (i = 0; i < uwsgi.mules_cnt; i++) {
1377-
if (uwsgi.mules[i].pid > 0) {
1378-
uwsgi_curse_mule(i, SIGHUP);
1379-
}
1380-
}
1380+
// avoid breaking other child process signal handling logic by doing nohang checks on the workers
1381+
// until they are all done.
1382+
int keep_waiting = 1;
1383+
while (keep_waiting == 1) {
1384+
int still_running = 0;
1385+
int errors = 0;
1386+
for (i = 1; i <= uwsgi.numproc; i++) {
1387+
if (uwsgi.workers[i].pid > 0) {
1388+
pid_t rval = waitpid(uwsgi.workers[i].pid, NULL, WNOHANG);
1389+
if (rval == uwsgi.workers[i].pid) {
1390+
uwsgi.workers[i].pid = 0;
1391+
} else if (rval == 0) {
1392+
still_running++;
1393+
} else if (rval < 0) {
1394+
errors++;
1395+
}
1396+
}
1397+
}
13811398

1382-
for (i = 1; i <= uwsgi.numproc; i++) {
1383-
if (uwsgi.workers[i].pid > 0) {
1384-
waitpid(uwsgi.workers[i].pid, &waitpid_status, 0);
1385-
}
1386-
}
1399+
// exit out if everything is done or we got errors as we can't do much about the errors at this point
1400+
if (still_running == 0 || errors > 0) {
1401+
keep_waiting = 0;
1402+
break;
1403+
}
1404+
sleep(1);
1405+
}
13871406

1388-
uwsgi_destroy_processes();
1407+
uwsgi_destroy_processes();
13891408
}
13901409

13911410

0 commit comments

Comments
 (0)