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
 	short wd;
190
 	short wd;
191
 
191
 
192
 	/**@brief array of worker pids (pool handler context) */
192
 	/**@brief array of worker pids (pool handler context) */
193
-	pid_t **wrk_pids;
193
+	pid_t *wrk_pids;
194
 	/**@brief workers count */
194
 	/**@brief workers count */
195
 	unsigned int n_wrk;
195
 	unsigned int n_wrk;
196
 
196
 

+ 8
- 0
src/conf.c View File

163
 		usage();
163
 		usage();
164
 		exit(3);
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
 	return 0;
174
 	return 0;
167
 }
175
 }
168
 
176
 

+ 26
- 23
src/responder.c View File

75
 			strerror(errno));
75
 			strerror(errno));
76
 		clean_exit(PYFCGI_FATAL);
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
 int responder_loop()
93
 int responder_loop()
81
 {
94
 {
82
 	unsigned int n_wrk, wanted_n, n;
95
 	unsigned int n_wrk, wanted_n, n;
83
 	pid_t *wrk_pids;
96
 	pid_t *wrk_pids;
84
-	int err;
85
 	int status;
97
 	int status;
86
 	pid_t ret;
98
 	pid_t ret;
87
 	/**@brief poll timeout */
99
 	/**@brief poll timeout */
130
 
142
 
131
 	pyfcgi_wd_arm();
143
 	pyfcgi_wd_arm();
132
 
144
 
133
-	PyFCGI_conf.context.wrk_pids = &wrk_pids;
145
+	wrk_pids = PyFCGI_conf.context.wrk_pids;
134
 	PyFCGI_conf.context.n_wrk = 0;
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
 	wanted_n = PyFCGI_conf.min_wrk;
148
 	wanted_n = PyFCGI_conf.min_wrk;
147
 	n_wrk = 0;
149
 	n_wrk = 0;
268
 
270
 
269
 		// Stopping & deleting useless childs
271
 		// Stopping & deleting useless childs
270
 		if(wanted_n < n_wrk && idle)
272
 		if(wanted_n < n_wrk && idle)
271
-		{	// need to shift the list and dec n_wrk
273
+		{
272
 			busy = 0;
274
 			busy = 0;
273
 			n_wrk--;
275
 			n_wrk--;
274
-			kill(wrk_pids[n_wrk], SIGTERM);
276
+			kill(wrk_pids[n_wrk], SIGTERM); // kill last worker
275
 			nanosleep(&timeout, NULL);
277
 			nanosleep(&timeout, NULL);
276
 			if( (ret = waitpid(wrk_pids[n_wrk], &status, WNOHANG)) < 0 )
278
 			if( (ret = waitpid(wrk_pids[n_wrk], &status, WNOHANG)) < 0 )
277
 			{
279
 			{
286
 					PyFCGI_conf.worker_gc_timeout,
288
 					PyFCGI_conf.worker_gc_timeout,
287
 					n_wrk, wrk_pids[n_wrk]);
289
 					n_wrk, wrk_pids[n_wrk]);
288
 			}
290
 			}
291
+			wrk_pids[n_wrk] = 0;
289
 			idle = 0;
292
 			idle = 0;
290
 			continue;
293
 			continue;
291
 		}
294
 		}
456
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
459
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
457
 	{
460
 	{
458
 		pyfcgi_log(LOG_INFO, "Sending SIGTERM to child #%d (pid %d)",
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
 	retry = i = 0;
465
 	retry = i = 0;
463
 	while(i<PyFCGI_conf.context.n_wrk)
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
 			WNOHANG);
469
 			WNOHANG);
467
 		if(ret <= 0 && retry < 3)
470
 		if(ret <= 0 && retry < 3)
468
 		{
471
 		{
475
 		{
478
 		{
476
 			if(retry < 3)
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
 			retry = 0;
483
 			retry = 0;
481
 			i++;
484
 			i++;
483
 	}
486
 	}
484
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
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
 			pyfcgi_log(LOG_INFO, "Sending SIGKILL to child %d", i);
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
 	pyfcgi_log(LOG_ALERT, "Worker pool timeout ! Attempt to kill all childs");
502
 	pyfcgi_log(LOG_ALERT, "Worker pool timeout ! Attempt to kill all childs");
500
 	for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
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
 	while(PyFCGI_conf.context.n_wrk)
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
 		PyFCGI_conf.context.n_wrk--;
511
 		PyFCGI_conf.context.n_wrk--;
509
 	}
512
 	}
510
 	pyfcgi_wd_stop();
513
 	pyfcgi_wd_stop();

Loading…
Cancel
Save