Skip to content

Commit 129cf47

Browse files
authored
Merge pull request #2740 from wynnw/bugfix/gracefully-kill-them-all-nohang
Fix for gracefully_kill_them_all() which broke setups using fifo-base…
2 parents 9b1bb83 + 79f00d1 commit 129cf47

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

core/uwsgi.c

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,38 +1319,57 @@ void kill_them_all(int signum) {
13191319

13201320
// gracefully destroy
13211321
void gracefully_kill_them_all(int signum) {
1322+
if (uwsgi_instance_is_dying) return;
1323+
uwsgi.status.gracefully_destroying = 1;
13221324

1323-
int waitpid_status;
1324-
1325-
if (uwsgi_instance_is_dying) return;
1326-
uwsgi.status.gracefully_destroying = 1;
1327-
1328-
// unsubscribe if needed
1329-
uwsgi_unsubscribe_all();
1325+
// unsubscribe if needed
1326+
uwsgi_unsubscribe_all();
13301327

1331-
uwsgi_log_verbose("graceful shutdown triggered...\n");
1328+
uwsgi_log_verbose("graceful shutdown triggered...\n");
13321329

1333-
int i;
1334-
for (i = 1; i <= uwsgi.numproc; i++) {
1335-
if (uwsgi.workers[i].pid > 0) {
1330+
int i;
1331+
for (i = 1; i <= uwsgi.numproc; i++) {
1332+
if (uwsgi.workers[i].pid > 0) {
13361333
if (uwsgi.shutdown_sockets)
13371334
uwsgi.workers[i].shutdown_sockets = 1;
1338-
uwsgi_curse(i, SIGHUP);
1339-
}
1340-
}
1341-
for (i = 0; i < uwsgi.mules_cnt; i++) {
1342-
if (uwsgi.mules[i].pid > 0) {
1343-
uwsgi_curse_mule(i, SIGHUP);
1344-
}
1345-
}
1335+
uwsgi_curse(i, SIGHUP);
1336+
}
1337+
}
13461338

1347-
for (i = 1; i <= uwsgi.numproc; i++) {
1348-
if (uwsgi.workers[i].pid > 0) {
1349-
waitpid(uwsgi.workers[i].pid, &waitpid_status, 0);
1350-
}
1351-
}
1339+
for (i = 0; i < uwsgi.mules_cnt; i++) {
1340+
if (uwsgi.mules[i].pid > 0) {
1341+
uwsgi_curse_mule(i, SIGHUP);
1342+
}
1343+
}
1344+
1345+
// avoid breaking other child process signal handling logic by doing nohang checks on the workers
1346+
// until they are all done.
1347+
int keep_waiting = 1;
1348+
while (keep_waiting == 1) {
1349+
int still_running = 0;
1350+
int errors = 0;
1351+
for (i = 1; i <= uwsgi.numproc; i++) {
1352+
if (uwsgi.workers[i].pid > 0) {
1353+
pid_t rval = waitpid(uwsgi.workers[i].pid, NULL, WNOHANG);
1354+
if (rval == uwsgi.workers[i].pid) {
1355+
uwsgi.workers[i].pid = 0;
1356+
} else if (rval == 0) {
1357+
still_running++;
1358+
} else if (rval < 0) {
1359+
errors++;
1360+
}
1361+
}
1362+
}
1363+
1364+
// exit out if everything is done or we got errors as we can't do much about the errors at this point
1365+
if (still_running == 0 || errors > 0) {
1366+
keep_waiting = 0;
1367+
break;
1368+
}
1369+
sleep(1);
1370+
}
13521371

1353-
uwsgi_destroy_processes();
1372+
uwsgi_destroy_processes();
13541373
}
13551374

13561375

0 commit comments

Comments
 (0)