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

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