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

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