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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. if(PyFCGI_conf.context.n_wrk)
  25. {
  26. kill(PyFCGI_conf.context.pid, SIGTERM);
  27. }
  28. pyfcgi_IPC_close(IPC_WSTATE | IPC_WREQS | IPC_SEMST | IPC_SHMST);
  29. pyfcgi_IPC_destroy(IPC_WSTATE | IPC_WREQS | IPC_SEMST | IPC_SHMST);
  30. exit(status);
  31. }
  32. pid_t spawn_pool_handler()
  33. {
  34. pid_t res;
  35. struct sigaction act;
  36. act.sa_handler = pyfcgi_sighandler_drop;
  37. sigemptyset(&act.sa_mask);
  38. act.sa_flags = 0;
  39. act.sa_restorer = NULL;
  40. res = fork();
  41. if(res < 0)
  42. {
  43. pyfcgi_log(LOG_ALERT, "Failed to fork pool_handler : %s",
  44. strerror(errno));
  45. return -1;
  46. }
  47. if(!res)
  48. {
  49. if(sigaction(SIGINT, &act, NULL))
  50. {
  51. pyfcgi_log(LOG_WARNING,
  52. "Unable to sigaction SIGINT handler : %s",
  53. strerror(errno));
  54. }
  55. responder_loop();
  56. exit((unsigned char)-1);
  57. }
  58. return res;
  59. }
  60. void init_context()
  61. {
  62. PyFCGI_conf.context.pid = getpid();
  63. PyFCGI_conf.context.ppid = getppid();
  64. if(pyfcgi_IPC_create(IPC_WSTATE | IPC_WREQS | IPC_SEMST | IPC_SHMST) < 0)
  65. {
  66. pyfcgi_log(LOG_ALERT, "Pool handler process is unable to init IPC components");
  67. sleep(1);
  68. clean_exit(PYFCGI_FATAL);
  69. }
  70. if(sem_post(PyFCGI_SEM(SEM_STATS).sem) < 0)
  71. {
  72. pyfcgi_log(LOG_ALERT, "Unable to POST stat semaphore : %s",
  73. strerror(errno));
  74. clean_exit(PYFCGI_FATAL);
  75. }
  76. //Alloc workers PID array
  77. PyFCGI_conf.context.wrk_pids = malloc(
  78. sizeof(pid_t)*(PyFCGI_conf.max_wrk+1));
  79. if(!PyFCGI_conf.context.wrk_pids)
  80. {
  81. pyfcgi_log(LOG_ALERT,
  82. "Unable to allocate worker PID array : %s",
  83. strerror(errno));
  84. clean_exit(PYFCGI_FATAL);
  85. }
  86. bzero(PyFCGI_conf.context.wrk_pids,
  87. sizeof(pid_t) * (PyFCGI_conf.max_wrk + 1));
  88. }
  89. int responder_loop()
  90. {
  91. unsigned int n_wrk, wanted_n, n;
  92. pid_t *wrk_pids;
  93. int status;
  94. pid_t ret;
  95. /**@brief poll timeout */
  96. struct timespec timeout;
  97. struct timespec idle_timeout;
  98. /**@brief watchdog timeout */
  99. struct timespec pool_timeout;
  100. time_t idle_start, busy_start;
  101. short idle, busy;
  102. struct sigaction act;
  103. char *statusstr;
  104. time_t last_update, now;
  105. act.sa_handler = pool_sighandler;
  106. sigemptyset(&act.sa_mask);
  107. sigaddset(&act.sa_mask, SIGTERM);
  108. act.sa_flags = 0;
  109. act.sa_restorer = NULL;
  110. if(sigaction(SIGTERM, &act, NULL))
  111. {
  112. pyfcgi_log(LOG_ALERT,
  113. "Sigaction error for SIGTERM pool process : %s",
  114. strerror(errno));
  115. exit(PYFCGI_FATAL);
  116. }
  117. idle_timeout.tv_sec = 0;
  118. idle_timeout.tv_nsec = 100000; //0.0001s
  119. timeout.tv_sec = 0;
  120. timeout.tv_nsec = 100000000; //0.1s
  121. idle = busy = 0;
  122. pyfcgi_logger_set_ident("Workpool");
  123. if(PyFCGI_conf.pool_timeout)
  124. {
  125. pool_timeout.tv_nsec = 0;
  126. pool_timeout.tv_sec = PyFCGI_conf.pool_timeout;
  127. pyfcgi_wd_init(pool_wd_sighandler, &pool_timeout);
  128. }
  129. pyfcgi_log(LOG_INFO, "Preparing workers");
  130. init_context();
  131. pyfcgi_wd_arm();
  132. wrk_pids = PyFCGI_conf.context.wrk_pids;
  133. PyFCGI_conf.context.n_wrk = 0;
  134. wanted_n = PyFCGI_conf.min_wrk;
  135. n_wrk = 0;
  136. // prespawning minimum worker count
  137. for(n_wrk=0; n_wrk < wanted_n; n_wrk++)
  138. {
  139. wrk_pids[n_wrk] = spawn(n_wrk);
  140. PyFCGI_conf.context.n_wrk = n_wrk;
  141. }
  142. //Wait at least for a process to be ready
  143. while(!pyfcgi_pool_idle(&idle_timeout));
  144. last_update = 0;
  145. // main loop, taking care to restart terminated workers,
  146. // spawn new one if needed, etc.
  147. while(1)
  148. {
  149. pyfcgi_wd_arm();
  150. PyFCGI_conf.context.n_wrk = n_wrk;
  151. if(last_update != (now = time(NULL)))
  152. {
  153. pyfcgi_log(LOG_DEBUG, "Infos : n_wrk=%d max=%d min=%d",
  154. n_wrk, PyFCGI_conf.max_wrk, PyFCGI_conf.min_wrk);
  155. pyfcgi_pool_shm_update(n_wrk);
  156. last_update = now;
  157. }
  158. if( (ret = waitpid(0, &status, WNOHANG)) )
  159. {
  160. if(ret < 0)
  161. {
  162. //TODO : error
  163. }
  164. for(n=0; n<n_wrk; n++)
  165. {
  166. if(wrk_pids[n] == ret)
  167. {
  168. break;
  169. }
  170. }
  171. if(n == n_wrk)
  172. {
  173. pyfcgi_log(LOG_WARNING,
  174. "Child %d stopped but was notregistered",
  175. ret);
  176. continue;
  177. }
  178. if(WIFSIGNALED(status))
  179. {
  180. if(WTERMSIG(status) == 9)
  181. {
  182. pyfcgi_log(LOG_ALERT,
  183. "Worker[%d] get killed ! No guaranty that SEM_WSTATE is OK, exiting...",
  184. n);
  185. clean_exit(PYFCGI_WORKER_FAIL);
  186. }
  187. if(WTERMSIG(status) == 11)
  188. {
  189. pyfcgi_log(LOG_ALERT,
  190. "Worker[%d] segfault ! No guaranty that SEM_WSTATE is OK, exiting...",
  191. n);
  192. clean_exit(PYFCGI_WORKER_FAIL);
  193. }
  194. else
  195. {
  196. pyfcgi_log(LOG_ALERT,
  197. "Worker[%d] terminated by signal %s(%d)",
  198. n, strsignal(WTERMSIG(status)),
  199. WTERMSIG(status));
  200. }
  201. }
  202. if(WEXITSTATUS(status))
  203. {
  204. statusstr = status2str(WEXITSTATUS(status));
  205. pyfcgi_log((WEXITSTATUS(status)&PYFCGI_FATAL)?
  206. LOG_ALERT:LOG_WARNING,
  207. "Worker[%d] exited with status %s",
  208. n, statusstr);
  209. free(statusstr);
  210. }
  211. if(!status)
  212. {
  213. pyfcgi_log(LOG_INFO,
  214. "Worker[%d] PID %d exited normally",
  215. n, wrk_pids[n]);
  216. }
  217. // respawn on same slot
  218. pyfcgi_log(LOG_DEBUG, "respawning worker #%d", n);
  219. wrk_pids[n] = spawn(n);
  220. }
  221. // Check if the pool is idle or busy
  222. if(pyfcgi_pool_idle(&idle_timeout))
  223. {
  224. // workers idle
  225. busy = 0;
  226. if(!idle)
  227. {
  228. idle = 1;
  229. idle_start = time(NULL);
  230. }
  231. else if((time(NULL) - idle_start) > PyFCGI_conf.worker_gc_timeout &&
  232. wanted_n > PyFCGI_conf.min_wrk
  233. && n_wrk - wanted_n < 2)
  234. {
  235. wanted_n--;
  236. idle = 0;
  237. }
  238. }
  239. else
  240. {
  241. idle = 0;
  242. if(!busy)
  243. {
  244. busy = 1;
  245. busy_start = time(NULL);
  246. }
  247. else if(time(NULL) - busy_start > 0 &&
  248. wanted_n < PyFCGI_conf.max_wrk)
  249. {
  250. pyfcgi_log( LOG_DEBUG,
  251. "All workers busy, spawning a new one");
  252. n = n_wrk;
  253. n_wrk++;
  254. wanted_n = n_wrk;
  255. wrk_pids[n] = spawn(n);
  256. if(!PyFCGI_conf.worker_fast_spawn)
  257. {
  258. busy_start = time(NULL);
  259. }
  260. }
  261. }
  262. // Stopping & deleting useless childs
  263. if(wanted_n < n_wrk && idle)
  264. {
  265. busy = 0;
  266. n_wrk--;
  267. kill(wrk_pids[n_wrk], SIGTERM); // kill last worker
  268. nanosleep(&timeout, NULL);
  269. if( (ret = waitpid(wrk_pids[n_wrk], &status, WNOHANG)) < 0 )
  270. {
  271. pyfcgi_log(LOG_ERR, "Pool idle since %ds but unable to kill child %d (PID %d)",
  272. PyFCGI_conf.worker_gc_timeout,
  273. n_wrk, wrk_pids[n_wrk]);
  274. kill(wrk_pids[n_wrk], SIGKILL);
  275. }
  276. else
  277. {
  278. pyfcgi_log(LOG_INFO, "Pool idle since %ds : worker[%d](%d) killed",
  279. PyFCGI_conf.worker_gc_timeout,
  280. n_wrk, wrk_pids[n_wrk]);
  281. }
  282. wrk_pids[n_wrk] = 0;
  283. idle = 0;
  284. continue;
  285. }
  286. nanosleep(&timeout, NULL);
  287. }
  288. pyfcgi_wd_arm();
  289. //Debug wait & exit
  290. for(; n_wrk != 0; n_wrk--)
  291. {
  292. waitpid(wrk_pids[n_wrk], &status, 0);
  293. pyfcgi_log(LOG_DEBUG, "Child %d stopped with status %d",
  294. wrk_pids[n_wrk], status);
  295. PyFCGI_conf.context.n_wrk = n_wrk;
  296. }
  297. //printf("Content-Type: text/html\r\n\r\nHello world !\n");
  298. pyfcgi_wd_stop();
  299. pyfcgi_log(LOG_INFO,"Child workers stoped, stopping responder");
  300. exit(0);
  301. }
  302. pid_t spawn(int wrk_id)
  303. {
  304. pid_t res;
  305. struct timespec wd_timeout;
  306. struct sigaction act;
  307. char ident[128];
  308. act.sa_handler = worker_sighandler;
  309. sigemptyset(&act.sa_mask);
  310. act.sa_flags = 0;
  311. act.sa_restorer = NULL;
  312. res = fork();
  313. if(res == -1)
  314. {
  315. pyfcgi_log(LOG_ERR, "Fork fails for worker #%d : %s",
  316. wrk_id, strerror(errno));
  317. return -1;
  318. }
  319. else if(!res)
  320. {
  321. // Child process
  322. PyFCGI_conf.context.ppid = PyFCGI_conf.context.pid;
  323. PyFCGI_conf.context.pid = getpid();
  324. snprintf(ident, 128, "Worker%2d", wrk_id);
  325. pyfcgi_logger_set_ident(ident);
  326. // Init IPC components
  327. if(pyfcgi_IPC_init(IPC_WSTATE | IPC_WREQS) < 0)
  328. {
  329. pyfcgi_log(LOG_ALERT, "Unable to initialize semaphore when spawning process...");
  330. exit(PYFCGI_FATAL);
  331. }
  332. // Set handler for SIGINT & SIGTERM
  333. /*
  334. if(sigaction(SIGINT, &(PyFCGI_conf.context.master_old_sigint),
  335. NULL))
  336. {
  337. pyfcgi_log(LOG_ALERT,
  338. "Sigaction error for worker process when restoring SIGINT handler: %s",
  339. strerror(errno));
  340. exit(PYFCGI_FATAL);
  341. }
  342. */
  343. if(sigaction(SIGTERM, &act, NULL))
  344. {
  345. pyfcgi_log(LOG_ALERT,
  346. "Sigaction error for worker process : %s",
  347. strerror(errno));
  348. exit(PYFCGI_FATAL);
  349. }
  350. // Set watchdog
  351. if(PyFCGI_conf.worker_timeout)
  352. {
  353. wd_timeout.tv_nsec = 0;
  354. wd_timeout.tv_sec = PyFCGI_conf.worker_timeout;
  355. pyfcgi_wd_init(worker_sigalrmhandler, &wd_timeout);
  356. }
  357. if(PyFCGI_conf.pep333)
  358. {
  359. exit(work333(wrk_id));
  360. }
  361. else
  362. {
  363. exit(work(wrk_id));
  364. }
  365. }
  366. pyfcgi_IPC_init(IPC_WSTATE | IPC_WREQS | IPC_SEMST);
  367. // Sleep to avoid spawning like hell thinking all workers are
  368. // busy. Let some time to this one to go up...
  369. // TODO: find a better way to avoid spawning to max_wrk
  370. //nanosleep(&timeout, NULL);
  371. pyfcgi_log( LOG_INFO,
  372. "Worker #%d spawned with PID %d", wrk_id, res);
  373. return res;
  374. }
  375. int pyfcgi_pool_state()
  376. {
  377. int err, res;
  378. if(sem_getvalue(PyFCGI_SEM(SEM_WSTATE).sem, &res) < 0)
  379. {
  380. err = errno;
  381. pyfcgi_log(LOG_ALERT, "Unable to read WSTATE semaphore value : %s",
  382. strerror(err));
  383. clean_exit(PYFCGI_FATAL);
  384. }
  385. return res;
  386. }
  387. int pyfcgi_pool_idle(const struct timespec *timeout)
  388. {
  389. int err;
  390. struct timespec abs_timeout;
  391. if(clock_gettime(CLOCK_REALTIME_COARSE, &abs_timeout) < 0)
  392. {
  393. //clock error
  394. pyfcgi_log(LOG_WARNING, "Unable to fetch asbtime for WSTATE sem_timedwait : %s",
  395. strerror(errno));
  396. }
  397. abs_timeout.tv_sec += timeout->tv_sec;
  398. if(abs_timeout.tv_nsec + timeout->tv_nsec > 999999999)
  399. {
  400. abs_timeout.tv_nsec = abs_timeout.tv_nsec + timeout->tv_nsec - 999999999;
  401. abs_timeout.tv_sec +=1;
  402. }
  403. else
  404. {
  405. abs_timeout.tv_nsec = timeout->tv_nsec;
  406. }
  407. if(sem_timedwait(PyFCGI_SEM(SEM_WSTATE).sem, &abs_timeout) < 0)
  408. {
  409. err = errno;
  410. switch(err)
  411. {
  412. case ETIMEDOUT:
  413. case EAGAIN:
  414. return 0; //busy
  415. case EINVAL:
  416. sleep(1);
  417. return 1;
  418. default:
  419. pyfcgi_log(LOG_ALERT, "Unable to wait WSTATE sem : %s",
  420. strerror(err));
  421. clean_exit(PYFCGI_FATAL);
  422. }
  423. }
  424. if(sem_post(PyFCGI_SEM(SEM_WSTATE).sem) < 0)
  425. {
  426. pyfcgi_log(LOG_ALERT,
  427. "Unable to sempost after a sem_timedwait : %s",
  428. strerror(errno));
  429. clean_exit(PYFCGI_FATAL);
  430. }
  431. return 1; //idle
  432. }
  433. void pool_sighandler(int signum)
  434. {
  435. unsigned int i, retry;
  436. int status, ret;
  437. struct timespec req;
  438. pyfcgi_log(LOG_NOTICE, "Received signal %s, cleaning & exiting...",
  439. strsignal(signum));
  440. if(PyFCGI_conf.context.n_wrk < 1) { clean_exit(0); }
  441. for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
  442. {
  443. pyfcgi_log(LOG_INFO, "Sending SIGTERM to child #%d (pid %d)",
  444. i,PyFCGI_conf.context.wrk_pids[i]);
  445. kill(PyFCGI_conf.context.wrk_pids[i], SIGTERM);
  446. }
  447. retry = i = 0;
  448. while(i<PyFCGI_conf.context.n_wrk)
  449. {
  450. ret = waitpid(PyFCGI_conf.context.wrk_pids[i], &status,
  451. WNOHANG);
  452. if(ret <= 0 && retry < 3)
  453. {
  454. retry++;
  455. req.tv_sec = 0;
  456. req.tv_nsec = 100000000; //0.1s
  457. nanosleep(&req, NULL);
  458. }
  459. else
  460. {
  461. if(retry < 3)
  462. {
  463. PyFCGI_conf.context.wrk_pids[i] = 0;
  464. }
  465. retry = 0;
  466. i++;
  467. }
  468. }
  469. for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
  470. {
  471. if(PyFCGI_conf.context.wrk_pids[i])
  472. {
  473. pyfcgi_log(LOG_INFO, "Sending SIGKILL to child %d", i);
  474. kill(PyFCGI_conf.context.wrk_pids[i], SIGKILL);
  475. }
  476. }
  477. PyFCGI_conf.context.n_wrk = 0;
  478. clean_exit(0);
  479. }
  480. void pool_wd_sighandler(int signum)
  481. {
  482. unsigned int i;
  483. pyfcgi_log(LOG_ALERT, "Worker pool timeout ! Attempt to kill all childs");
  484. for(i=0; i<PyFCGI_conf.context.n_wrk; i++)
  485. {
  486. pyfcgi_log(LOG_ALERT, "Child[%d] PID %d", i, PyFCGI_conf.context.wrk_pids[i]);
  487. kill(PyFCGI_conf.context.wrk_pids[i], SIGALRM);
  488. }
  489. while(PyFCGI_conf.context.n_wrk)
  490. {
  491. kill(PyFCGI_conf.context.wrk_pids[PyFCGI_conf.context.n_wrk], SIGALRM);
  492. PyFCGI_conf.context.n_wrk--;
  493. }
  494. pyfcgi_wd_stop();
  495. kill(PyFCGI_conf.context.pid, SIGTERM);
  496. clean_exit(PYFCGI_TIMEOUT);
  497. exit(PYFCGI_TIMEOUT);
  498. }
  499. void pyfcgi_pool_shm_update(int nworker)
  500. {
  501. short retry;
  502. int err;
  503. pyfcgi_stats_shm_t *data;
  504. struct timespec req;
  505. req.tv_sec = 0;
  506. req.tv_nsec = 10000000; //0.01s
  507. retry = 0;
  508. while(1)
  509. {
  510. if(sem_trywait(PyFCGI_SEM(SEM_STATS).sem) < 0)
  511. {
  512. err = errno;
  513. if(err == EAGAIN)
  514. {
  515. if(retry >= 5)
  516. {
  517. pyfcgi_log(LOG_ALERT,
  518. "Deadlock on SEM_STATS");
  519. clean_exit(PYFCGI_FATAL);
  520. }
  521. nanosleep(&req, NULL);
  522. continue;
  523. }
  524. pyfcgi_log(LOG_ALERT,
  525. "Unable to wait stats semaphore : %s",
  526. strerror(err));
  527. clean_exit(PYFCGI_FATAL);
  528. }
  529. break;
  530. }
  531. data = (pyfcgi_stats_shm_t*)PyFCGI_conf.shm.ptr;
  532. data->nworker = nworker;
  533. err = 0;
  534. if(sem_getvalue(PyFCGI_SEM(SEM_WSTATE).sem, &(data->pool_load)) < 0)
  535. {
  536. data->pool_load = -1;
  537. pyfcgi_log(LOG_ALERT,
  538. "Unable to get semaphore value for SEM_WSTATE : ",
  539. strerror(errno));
  540. err = 1;
  541. }
  542. if(sem_post(PyFCGI_SEM(SEM_STATS).sem) < 0)
  543. {
  544. pyfcgi_log(LOG_ALERT, "Unable to post sem at shm update : %s",
  545. strerror(errno));
  546. clean_exit(PYFCGI_FATAL);
  547. }
  548. if(err)
  549. {
  550. clean_exit(PYFCGI_FATAL);
  551. }
  552. }