Browse Source

Worker PID storage enhancement

Yann Weber 4 years ago
parent
commit
2a49fb1c00
3 changed files with 35 additions and 24 deletions
  1. 1
    1
      include/conf.h
  2. 8
    0
      src/conf.c
  3. 26
    23
      src/responder.c

+ 1
- 1
include/conf.h View File

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

+ 8
- 0
src/conf.c View File

@@ -163,6 +163,14 @@ int parse_args(int argc, char *argv[])
163 163
 		usage();
164 164
 		exit(3);
165 165
 	}
166
+
167
+	//Alloc workers PID array
168
+	PyFCGI_conf.context.wrk_pids = malloc(sizeof(pid_t)*(PyFCGI_conf.max_wrk+1));
169
+	if(!PyFCGI_conf.context.wrk_pids)
170
+	{
171
+		perror("Unable to allocate worker PID array");
172
+		exit(PYFCGI_FATAL);
173
+	}
166 174
 	return 0;
167 175
 }
168 176
 

+ 26
- 23
src/responder.c View File

@@ -75,13 +75,25 @@ void init_context()
75 75
 			strerror(errno));
76 76
 		clean_exit(PYFCGI_FATAL);
77 77
 	}
78
+
79
+	//Alloc workers PID array
80
+	PyFCGI_conf.context.wrk_pids = malloc(
81
+		sizeof(pid_t)*(PyFCGI_conf.max_wrk+1));
82
+	if(!PyFCGI_conf.context.wrk_pids)
83
+	{
84
+		pyfcgi_log(LOG_ALERT,
85
+			"Unable to allocate worker PID array : %s",
86
+			strerror(errno));
87
+		clean_exit(PYFCGI_FATAL);
88
+	}
89
+	bzero(PyFCGI_conf.context.wrk_pids,
90
+		sizeof(pid_t) * (PyFCGI_conf.max_wrk + 1));
78 91
 }
79 92
 
80 93
 int responder_loop()
81 94
 {
82 95
 	unsigned int n_wrk, wanted_n, n;
83 96
 	pid_t *wrk_pids;
84
-	int err;
85 97
 	int status;
86 98
 	pid_t ret;
87 99
 	/**@brief poll timeout */
@@ -130,18 +142,8 @@ int responder_loop()
130 142
 
131 143
 	pyfcgi_wd_arm();
132 144
 
133
-	PyFCGI_conf.context.wrk_pids = &wrk_pids;
145
+	wrk_pids = PyFCGI_conf.context.wrk_pids;
134 146
 	PyFCGI_conf.context.n_wrk = 0;
135
-	wrk_pids = malloc(sizeof(int) * PyFCGI_conf.max_wrk);
136
-	if(!wrk_pids)
137
-	{
138
-		err = errno;
139
-		pyfcgi_log(	LOG_ALERT,
140
-			"Unable to allocate memory for childs PID : %s",
141
-			strerror(err));
142
-		clean_exit(err);
143
-	}
144
-	bzero(wrk_pids, sizeof(int) * PyFCGI_conf.max_wrk);
145 147
 
146 148
 	wanted_n = PyFCGI_conf.min_wrk;
147 149
 	n_wrk = 0;
@@ -268,10 +270,10 @@ int responder_loop()
268 270
 
269 271
 		// Stopping & deleting useless childs
270 272
 		if(wanted_n < n_wrk && idle)
271
-		{	// need to shift the list and dec n_wrk
273
+		{
272 274
 			busy = 0;
273 275
 			n_wrk--;
274
-			kill(wrk_pids[n_wrk], SIGTERM);
276
+			kill(wrk_pids[n_wrk], SIGTERM); // kill last worker
275 277
 			nanosleep(&timeout, NULL);
276 278
 			if( (ret = waitpid(wrk_pids[n_wrk], &status, WNOHANG)) < 0 )
277 279
 			{
@@ -286,6 +288,7 @@ int responder_loop()
286 288
 					PyFCGI_conf.worker_gc_timeout,
287 289
 					n_wrk, wrk_pids[n_wrk]);
288 290
 			}
291
+			wrk_pids[n_wrk] = 0;
289 292
 			idle = 0;
290 293
 			continue;
291 294
 		}
@@ -456,13 +459,13 @@ void pool_sighandler(int signum)
456 459
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
457 460
 	{
458 461
 		pyfcgi_log(LOG_INFO, "Sending SIGTERM to child #%d (pid %d)",
459
-			i,(*PyFCGI_conf.context.wrk_pids)[i]);
460
-		kill((*PyFCGI_conf.context.wrk_pids)[i], SIGTERM);
462
+			i,PyFCGI_conf.context.wrk_pids[i]);
463
+		kill(PyFCGI_conf.context.wrk_pids[i], SIGTERM);
461 464
 	}
462 465
 	retry = i = 0;
463 466
 	while(i<PyFCGI_conf.context.n_wrk)
464 467
 	{
465
-		ret = waitpid((*PyFCGI_conf.context.wrk_pids)[i], &status,
468
+		ret = waitpid(PyFCGI_conf.context.wrk_pids[i], &status,
466 469
 			WNOHANG);
467 470
 		if(ret <= 0 && retry < 3)
468 471
 		{
@@ -475,7 +478,7 @@ void pool_sighandler(int signum)
475 478
 		{
476 479
 			if(retry < 3)
477 480
 			{
478
-				(*PyFCGI_conf.context.wrk_pids)[i] = 0;
481
+				PyFCGI_conf.context.wrk_pids[i] = 0;
479 482
 			}
480 483
 			retry = 0;
481 484
 			i++;
@@ -483,10 +486,10 @@ void pool_sighandler(int signum)
483 486
 	}
484 487
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
485 488
 	{
486
-		if((*PyFCGI_conf.context.wrk_pids)[i])
489
+		if(PyFCGI_conf.context.wrk_pids[i])
487 490
 		{
488 491
 			pyfcgi_log(LOG_INFO, "Sending SIGKILL to child %d", i);
489
-			kill((*PyFCGI_conf.context.wrk_pids)[i], SIGKILL);
492
+			kill(PyFCGI_conf.context.wrk_pids[i], SIGKILL);
490 493
 		}
491 494
 		
492 495
 	}
@@ -499,12 +502,12 @@ void pool_wd_sighandler(int signum)
499 502
 	pyfcgi_log(LOG_ALERT, "Worker pool timeout ! Attempt to kill all childs");
500 503
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
501 504
 	{
502
-		pyfcgi_log(LOG_ALERT, "Child[%d] PID %d", i, (*PyFCGI_conf.context.wrk_pids)[i]);
503
-		kill((*PyFCGI_conf.context.wrk_pids)[i], SIGALRM);
505
+		pyfcgi_log(LOG_ALERT, "Child[%d] PID %d", i, PyFCGI_conf.context.wrk_pids[i]);
506
+		kill(PyFCGI_conf.context.wrk_pids[i], SIGALRM);
504 507
 	}
505 508
 	while(PyFCGI_conf.context.n_wrk)
506 509
 	{
507
-		kill((*PyFCGI_conf.context.wrk_pids)[PyFCGI_conf.context.n_wrk], SIGALRM);
510
+		kill(PyFCGI_conf.context.wrk_pids[PyFCGI_conf.context.n_wrk], SIGALRM);
508 511
 		PyFCGI_conf.context.n_wrk--;
509 512
 	}
510 513
 	pyfcgi_wd_stop();

Loading…
Cancel
Save