Worker PID storage enhancement

This commit is contained in:
Yann Weber 2019-08-11 15:46:04 +02:00
commit 2a49fb1c00
3 changed files with 35 additions and 24 deletions

View file

@ -190,7 +190,7 @@ struct pyfcgi_context_s {
short wd;
/**@brief array of worker pids (pool handler context) */
pid_t **wrk_pids;
pid_t *wrk_pids;
/**@brief workers count */
unsigned int n_wrk;

View file

@ -163,6 +163,14 @@ int parse_args(int argc, char *argv[])
usage();
exit(3);
}
//Alloc workers PID array
PyFCGI_conf.context.wrk_pids = malloc(sizeof(pid_t)*(PyFCGI_conf.max_wrk+1));
if(!PyFCGI_conf.context.wrk_pids)
{
perror("Unable to allocate worker PID array");
exit(PYFCGI_FATAL);
}
return 0;
}

View file

@ -75,13 +75,25 @@ void init_context()
strerror(errno));
clean_exit(PYFCGI_FATAL);
}
//Alloc workers PID array
PyFCGI_conf.context.wrk_pids = malloc(
sizeof(pid_t)*(PyFCGI_conf.max_wrk+1));
if(!PyFCGI_conf.context.wrk_pids)
{
pyfcgi_log(LOG_ALERT,
"Unable to allocate worker PID array : %s",
strerror(errno));
clean_exit(PYFCGI_FATAL);
}
bzero(PyFCGI_conf.context.wrk_pids,
sizeof(pid_t) * (PyFCGI_conf.max_wrk + 1));
}
int responder_loop()
{
unsigned int n_wrk, wanted_n, n;
pid_t *wrk_pids;
int err;
int status;
pid_t ret;
/**@brief poll timeout */
@ -130,18 +142,8 @@ int responder_loop()
pyfcgi_wd_arm();
PyFCGI_conf.context.wrk_pids = &wrk_pids;
wrk_pids = PyFCGI_conf.context.wrk_pids;
PyFCGI_conf.context.n_wrk = 0;
wrk_pids = malloc(sizeof(int) * PyFCGI_conf.max_wrk);
if(!wrk_pids)
{
err = errno;
pyfcgi_log( LOG_ALERT,
"Unable to allocate memory for childs PID : %s",
strerror(err));
clean_exit(err);
}
bzero(wrk_pids, sizeof(int) * PyFCGI_conf.max_wrk);
wanted_n = PyFCGI_conf.min_wrk;
n_wrk = 0;
@ -268,10 +270,10 @@ int responder_loop()
// Stopping & deleting useless childs
if(wanted_n < n_wrk && idle)
{ // need to shift the list and dec n_wrk
{
busy = 0;
n_wrk--;
kill(wrk_pids[n_wrk], SIGTERM);
kill(wrk_pids[n_wrk], SIGTERM); // kill last worker
nanosleep(&timeout, NULL);
if( (ret = waitpid(wrk_pids[n_wrk], &status, WNOHANG)) < 0 )
{
@ -286,6 +288,7 @@ int responder_loop()
PyFCGI_conf.worker_gc_timeout,
n_wrk, wrk_pids[n_wrk]);
}
wrk_pids[n_wrk] = 0;
idle = 0;
continue;
}
@ -456,13 +459,13 @@ void pool_sighandler(int signum)
for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
{
pyfcgi_log(LOG_INFO, "Sending SIGTERM to child #%d (pid %d)",
i,(*PyFCGI_conf.context.wrk_pids)[i]);
kill((*PyFCGI_conf.context.wrk_pids)[i], SIGTERM);
i,PyFCGI_conf.context.wrk_pids[i]);
kill(PyFCGI_conf.context.wrk_pids[i], SIGTERM);
}
retry = i = 0;
while(i<PyFCGI_conf.context.n_wrk)
{
ret = waitpid((*PyFCGI_conf.context.wrk_pids)[i], &status,
ret = waitpid(PyFCGI_conf.context.wrk_pids[i], &status,
WNOHANG);
if(ret <= 0 && retry < 3)
{
@ -475,7 +478,7 @@ void pool_sighandler(int signum)
{
if(retry < 3)
{
(*PyFCGI_conf.context.wrk_pids)[i] = 0;
PyFCGI_conf.context.wrk_pids[i] = 0;
}
retry = 0;
i++;
@ -483,10 +486,10 @@ void pool_sighandler(int signum)
}
for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
{
if((*PyFCGI_conf.context.wrk_pids)[i])
if(PyFCGI_conf.context.wrk_pids[i])
{
pyfcgi_log(LOG_INFO, "Sending SIGKILL to child %d", i);
kill((*PyFCGI_conf.context.wrk_pids)[i], SIGKILL);
kill(PyFCGI_conf.context.wrk_pids[i], SIGKILL);
}
}
@ -499,12 +502,12 @@ void pool_wd_sighandler(int signum)
pyfcgi_log(LOG_ALERT, "Worker pool timeout ! Attempt to kill all childs");
for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
{
pyfcgi_log(LOG_ALERT, "Child[%d] PID %d", i, (*PyFCGI_conf.context.wrk_pids)[i]);
kill((*PyFCGI_conf.context.wrk_pids)[i], SIGALRM);
pyfcgi_log(LOG_ALERT, "Child[%d] PID %d", i, PyFCGI_conf.context.wrk_pids[i]);
kill(PyFCGI_conf.context.wrk_pids[i], SIGALRM);
}
while(PyFCGI_conf.context.n_wrk)
{
kill((*PyFCGI_conf.context.wrk_pids)[PyFCGI_conf.context.n_wrk], SIGALRM);
kill(PyFCGI_conf.context.wrk_pids[PyFCGI_conf.context.n_wrk], SIGALRM);
PyFCGI_conf.context.n_wrk--;
}
pyfcgi_wd_stop();