Tests about a simple python3 fastcgi runner using libfcgi and the Python-C API.
python
c
wsgi
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

responder.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (C) 2019 Weber Yann
  3. *
  4. * This file is part of PyFCGI.
  5. *
  6. * PyFCGI is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * PyFCGI is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PyFCGI. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "responder.h"
  20. /**@brief Exit after closing all stuff like semaphores
  21. * @ingroup work_master_proc */
  22. static void clean_exit(int status)
  23. {
  24. pyfcgi_IPC_close(IPC_WSTATE | IPC_WREQS | IPC_SEMST | IPC_SHMST);
  25. pyfcgi_IPC_destroy(IPC_WSTATE | IPC_WREQS | IPC_SEMST | IPC_SHMST);
  26. exit(status);
  27. }
  28. pid_t spawn_pool_handler()
  29. {
  30. pid_t res;
  31. res = fork();
  32. if(res < 0)
  33. {
  34. pyfcgi_log(LOG_ALERT, "Failed to fork pool_handler : %s",
  35. strerror(errno));
  36. return -1;
  37. }
  38. if(!res)
  39. {
  40. responder_loop();
  41. exit((unsigned char)-1);
  42. }
  43. return res;
  44. }
  45. void init_context()
  46. {
  47. PyFCGI_conf.context.pid = getpid();
  48. PyFCGI_conf.context.ppid = getppid();
  49. if(pyfcgi_IPC_create(IPC_WSTATE | IPC_WREQS | IPC_SEMST) < 0)
  50. {
  51. pyfcgi_log(LOG_ALERT, "Pool handler process is unable to init IPC components");
  52. sleep(1);
  53. clean_exit(PYFCGI_FATAL);
  54. }
  55. }
  56. int responder_loop()
  57. {
  58. unsigned int n_wrk, wanted_n, n;
  59. pid_t *wrk_pids;
  60. int err;
  61. int status;
  62. pid_t ret;
  63. /**@brief poll timeout */
  64. struct timespec timeout;
  65. /**@brief watchdog timeout */
  66. struct timespec pool_timeout;
  67. time_t idle_start, busy_start;
  68. short idle, busy;
  69. struct sigaction act;
  70. char *statusstr;
  71. act.sa_handler = pool_sighandler;
  72. sigemptyset(&act.sa_mask);
  73. sigaddset(&act.sa_mask, SIGTERM);
  74. act.sa_flags = 0;
  75. act.sa_restorer = NULL;
  76. if(sigaction(SIGINT, &act, NULL))
  77. {
  78. perror("Sigaction error for pool process");
  79. exit(PYFCGI_FATAL);
  80. }
  81. timeout.tv_sec = 0;
  82. timeout.tv_nsec = 100000000;
  83. idle = busy = 0;
  84. pyfcgi_logger_set_ident("Workpool");
  85. if(PyFCGI_conf.pool_timeout)
  86. {
  87. pool_timeout.tv_nsec = 0;
  88. pool_timeout.tv_sec = PyFCGI_conf.pool_timeout;
  89. pyfcgi_wd_init(pool_wd_sighandler, &pool_timeout);
  90. }
  91. pyfcgi_log(LOG_INFO, "Preparing workers");
  92. init_context();
  93. pyfcgi_wd_arm();
  94. PyFCGI_conf.context.wrk_pids = &wrk_pids;
  95. PyFCGI_conf.context.n_wrk = 0;
  96. wrk_pids = malloc(sizeof(int) * PyFCGI_conf.max_wrk);
  97. if(!wrk_pids)
  98. {
  99. err = errno;
  100. pyfcgi_log( LOG_ALERT,
  101. "Unable to allocate memory for childs PID : %s",
  102. strerror(err));
  103. clean_exit(err);
  104. }
  105. bzero(wrk_pids, sizeof(int) * PyFCGI_conf.max_wrk);
  106. wanted_n = PyFCGI_conf.min_wrk;
  107. n_wrk = 0;
  108. // prespawning minimum worker count
  109. for(n_wrk=0; n_wrk < wanted_n; n_wrk++)
  110. {
  111. wrk_pids[n_wrk] = spawn(n_wrk);
  112. PyFCGI_conf.context.n_wrk = n_wrk;
  113. }
  114. //Wait at least for a process to be ready
  115. while(!pyfcgi_pool_idle(&timeout));
  116. // main loop, taking care to restart terminated workers,
  117. // spawn new one if needed, etc.
  118. while(1)
  119. {
  120. pyfcgi_wd_arm();
  121. PyFCGI_conf.context.n_wrk = n_wrk;
  122. if( (ret = waitpid(0, &status, WNOHANG)) )
  123. {
  124. if(ret < 0)
  125. {
  126. //TODO : error
  127. }
  128. for(n=0; n<n_wrk; n++)
  129. {
  130. if(wrk_pids[n] == ret)
  131. {
  132. break;
  133. }
  134. }
  135. if(n == n_wrk)
  136. {
  137. pyfcgi_log(LOG_WARNING,
  138. "Child %d stopped but was notregistered",
  139. ret);
  140. continue;
  141. }
  142. if(WIFSIGNALED(status))
  143. {
  144. if(WTERMSIG(status) == 11)
  145. {
  146. pyfcgi_log(LOG_ALERT,
  147. "Worker[%d] segfault !",
  148. n);
  149. }
  150. else
  151. {
  152. pyfcgi_log(LOG_ALERT,
  153. "Worker[%d] terminated by signal %s(%d)",
  154. n, strsignal(WTERMSIG(status)),
  155. WTERMSIG(status));
  156. }
  157. }
  158. if(WEXITSTATUS(status))
  159. {
  160. statusstr = status2str(WEXITSTATUS(status));
  161. pyfcgi_log((WEXITSTATUS(status)&PYFCGI_FATAL)?
  162. LOG_ALERT:LOG_WARNING,
  163. "Worker[%d] exited with status %s",
  164. n, statusstr);
  165. free(statusstr);
  166. }
  167. if(!status)
  168. {
  169. pyfcgi_log(LOG_INFO,
  170. "Worker[%d] PID %d exited normally",
  171. n, wrk_pids[n]);
  172. }
  173. // respawn on same slot
  174. pyfcgi_log(LOG_DEBUG, "respawning worker #%d", n);
  175. wrk_pids[n] = spawn(n);
  176. continue;
  177. }
  178. // Check if the pool is idle or busy
  179. if(pyfcgi_pool_idle(&timeout))
  180. {
  181. // workers idle
  182. busy = 0;
  183. if(!idle)
  184. {
  185. idle = 1;
  186. idle_start = time(NULL);
  187. }
  188. else if((time(NULL) - idle_start) > PyFCGI_conf.worker_gc_timeout &&
  189. wanted_n > PyFCGI_conf.min_wrk
  190. && n_wrk - wanted_n < 2)
  191. {
  192. wanted_n--;
  193. idle = 0;
  194. }
  195. }
  196. else
  197. {
  198. idle = 0;
  199. if(!busy)
  200. {
  201. busy = 1;
  202. busy_start = time(NULL);
  203. }
  204. else if(time(NULL) - busy_start > 0 &&
  205. wanted_n < PyFCGI_conf.max_wrk)
  206. {
  207. pyfcgi_log( LOG_DEBUG,
  208. "All workers busy, spawning a new one");
  209. n = n_wrk;
  210. n_wrk++;
  211. wanted_n = n_wrk;
  212. wrk_pids[n] = spawn(n);
  213. if(!PyFCGI_conf.worker_fast_spawn)
  214. {
  215. busy_start = time(NULL);
  216. }
  217. }
  218. }
  219. // Stopping & deleting useless childs
  220. if(wanted_n < n_wrk && idle)
  221. { // need to shift the list and dec n_wrk
  222. busy = 0;
  223. n_wrk--;
  224. kill(wrk_pids[n_wrk], SIGTERM);
  225. nanosleep(&timeout, NULL);
  226. if( (ret = waitpid(wrk_pids[n_wrk], &status, WNOHANG)) < 0 )
  227. {
  228. pyfcgi_log(LOG_ERR, "Pool idle since %ds but unable to kill child %d (PID %d)",
  229. PyFCGI_conf.worker_gc_timeout,
  230. n_wrk, wrk_pids[n_wrk]);
  231. kill(wrk_pids[n_wrk], SIGKILL);
  232. }
  233. else
  234. {
  235. pyfcgi_log(LOG_INFO, "Pool idle since %ds : worker[%d](%d) killed",
  236. PyFCGI_conf.worker_gc_timeout,
  237. n_wrk, wrk_pids[n_wrk]);
  238. }
  239. idle = 0;
  240. continue;
  241. }
  242. nanosleep(&timeout, NULL);
  243. }
  244. pyfcgi_wd_arm();
  245. //Debug wait & exit
  246. for(; n_wrk != 0; n_wrk--)
  247. {
  248. waitpid(wrk_pids[n_wrk], &status, 0);
  249. pyfcgi_log(LOG_DEBUG, "Child %d stopped with status %d",
  250. wrk_pids[n_wrk], status);
  251. PyFCGI_conf.context.n_wrk = n_wrk;
  252. }
  253. //printf("Content-Type: text/html\r\n\r\nHello world !\n");
  254. pyfcgi_wd_stop();
  255. pyfcgi_log(LOG_INFO,"Child workers stoped, stopping responder");
  256. exit(0);
  257. }
  258. pid_t spawn(int wrk_id)
  259. {
  260. pid_t res;
  261. struct timespec wd_timeout;
  262. struct sigaction act;
  263. char ident[128];
  264. act.sa_handler = worker_sighandler;
  265. sigemptyset(&act.sa_mask);
  266. act.sa_flags = 0;
  267. act.sa_restorer = NULL;
  268. res = fork();
  269. if(res == -1)
  270. {
  271. pyfcgi_log(LOG_ERR, "Fork fails for worker #%d : %s",
  272. wrk_id, strerror(errno));
  273. return -1;
  274. }
  275. else if(!res)
  276. {
  277. // Child process
  278. PyFCGI_conf.context.ppid = PyFCGI_conf.context.pid;
  279. PyFCGI_conf.context.pid = getpid();
  280. snprintf(ident, 128, "Worker%2d", wrk_id);
  281. pyfcgi_logger_set_ident(ident);
  282. // Init IPC components
  283. if(pyfcgi_IPC_init(IPC_WSTATE | IPC_WREQS) < 0)
  284. {
  285. pyfcgi_log(LOG_ALERT, "Unable to initialize semaphore when spawning process...");
  286. exit(PYFCGI_FATAL);
  287. }
  288. // Set handler for SIGINT & SIGTERM
  289. if(sigaction(SIGINT, &act, NULL))
  290. {
  291. perror("Sigaction error for pool process");
  292. exit(PYFCGI_FATAL);
  293. }
  294. if(sigaction(SIGTERM, &act, NULL))
  295. {
  296. perror("Sigaction2 error for pool process");
  297. exit(PYFCGI_FATAL);
  298. }
  299. // Set watchdog
  300. if(PyFCGI_conf.worker_timeout)
  301. {
  302. wd_timeout.tv_nsec = 0;
  303. wd_timeout.tv_sec = PyFCGI_conf.worker_timeout;
  304. pyfcgi_wd_init(worker_sigalrmhandler, &wd_timeout);
  305. }
  306. if(PyFCGI_conf.pep333)
  307. {
  308. exit(work333(wrk_id));
  309. }
  310. else
  311. {
  312. exit(work(wrk_id));
  313. }
  314. }
  315. pyfcgi_IPC_init(IPC_WSTATE | IPC_WREQS | IPC_SEMST);
  316. // Sleep to avoid spawning like hell thinking all workers are
  317. // busy. Let some time to this one to go up...
  318. // TODO: find a better way to avoid spawning to max_wrk
  319. //nanosleep(&timeout, NULL);
  320. pyfcgi_log( LOG_INFO,
  321. "Worker #%d spawned with PID %d", wrk_id, res);
  322. return res;
  323. }
  324. int pyfcgi_pool_state()
  325. {
  326. int err, res;
  327. if(sem_getvalue(PyFCGI_SEM(SEM_WSTATE).sem, &res) < 0)
  328. {
  329. err = errno;
  330. pyfcgi_log(LOG_ALERT, "Unable to read WSTATE semaphore value : %s",
  331. strerror(err));
  332. clean_exit(PYFCGI_FATAL);
  333. }
  334. return res;
  335. }
  336. int pyfcgi_pool_idle(const struct timespec *timeout)
  337. {
  338. int err;
  339. struct timespec abs_timeout;
  340. if(clock_gettime(CLOCK_REALTIME_COARSE, &abs_timeout) < 0)
  341. {
  342. //clock error
  343. pyfcgi_log(LOG_WARNING, "Unable to fetch asbtime for WSTATE sem_timedwait : %s",
  344. strerror(errno));
  345. }
  346. abs_timeout.tv_sec += timeout->tv_sec;
  347. if(abs_timeout.tv_nsec + timeout->tv_nsec > 999999999)
  348. {
  349. abs_timeout.tv_nsec = abs_timeout.tv_nsec + timeout->tv_nsec - 999999999;
  350. abs_timeout.tv_sec +=1;
  351. }
  352. else
  353. {
  354. abs_timeout.tv_nsec = timeout->tv_nsec;
  355. }
  356. if(sem_timedwait(PyFCGI_SEM(SEM_WSTATE).sem, &abs_timeout) < 0)
  357. {
  358. err = errno;
  359. switch(err)
  360. {
  361. case ETIMEDOUT:
  362. case EAGAIN:
  363. return 0; //busy
  364. case EINVAL:
  365. sleep(1);
  366. return 1;
  367. default:
  368. pyfcgi_log(LOG_ALERT, "Unable to wait WSTATE sem : %s",
  369. strerror(err));
  370. clean_exit(PYFCGI_FATAL);
  371. }
  372. }
  373. sem_post(PyFCGI_SEM(SEM_WSTATE).sem); //Hope no worker fails to set busy...
  374. return 1; //idle
  375. }
  376. void pool_sighandler(int signum)
  377. {
  378. unsigned int i;
  379. struct timespec req;
  380. req.tv_sec = 0;
  381. req.tv_nsec = 200000000; //0.2s
  382. if(PyFCGI_conf.context.n_wrk < 1) { clean_exit(0); }
  383. for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
  384. {
  385. pyfcgi_log(LOG_INFO, "Sending SIGTERM to child #%d (pid %d)",
  386. i,(*PyFCGI_conf.context.wrk_pids)[i]);
  387. kill((*PyFCGI_conf.context.wrk_pids)[i], SIGTERM);
  388. nanosleep(&req, NULL); //waiting 0.2s
  389. }
  390. for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
  391. {
  392. if(kill((*PyFCGI_conf.context.wrk_pids)[i], SIGCONT))
  393. {
  394. pyfcgi_log(LOG_INFO, "Sending SIGKILL to child %d", i);
  395. kill((*PyFCGI_conf.context.wrk_pids)[i], SIGKILL);
  396. }
  397. }
  398. clean_exit(0);
  399. }
  400. void pool_wd_sighandler(int signum)
  401. {
  402. unsigned int i;
  403. pyfcgi_log(LOG_ALERT, "Worker pool timeout ! Attempt to kill all childs");
  404. for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
  405. {
  406. pyfcgi_log(LOG_ALERT, "Child[%d] PID %d", i, (*PyFCGI_conf.context.wrk_pids)[i]);
  407. kill((*PyFCGI_conf.context.wrk_pids)[i], SIGALRM);
  408. }
  409. while(PyFCGI_conf.context.n_wrk)
  410. {
  411. kill((*PyFCGI_conf.context.wrk_pids)[PyFCGI_conf.context.n_wrk], SIGALRM);
  412. PyFCGI_conf.context.n_wrk--;
  413. }
  414. pyfcgi_wd_stop();
  415. kill(PyFCGI_conf.context.pid, SIGTERM);
  416. clean_exit(PYFCGI_TIMEOUT);
  417. exit(PYFCGI_TIMEOUT);
  418. }