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.

pyworker.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 "pyworker.h"
  20. static short _wtimeout;
  21. /**@brief 1 if worker idle else 0 */
  22. static short _worker_idle;
  23. /**@brief Indicate that a worker is idle */
  24. static inline void worker_set_idle();
  25. /**@brief Indicate that a worker is busy */
  26. static inline void worker_set_busy();
  27. /**@brief Process results from a pep333 worker
  28. * @param FCGX_Stream* out stream from libFCGI
  29. * @param PyObject* application function returned value
  30. */
  31. static inline int work333_send_result(FCGX_Stream*, PyObject* ret);
  32. static int worker_piper_sigrcv = 0;
  33. int work333(int wrk_id)
  34. {
  35. PyObject *entry_fun, *py_osmod, *entry_ret, *environ,
  36. *start_response, *args;
  37. FCGX_Stream *in_stream, *out_stream, *err_stream;
  38. char **envp;
  39. int count;
  40. int max_reqs;
  41. struct timeval start, stop;
  42. FCGX_Request *request;
  43. max_reqs = PyFCGI_conf.max_reqs;
  44. pyfcgi_log(LOG_INFO, "Worker started with PEP333 App");
  45. pyinit();
  46. pyfcgi_log(LOG_DEBUG, "Python started");
  47. //importing os
  48. py_osmod = python_osmod();
  49. pyfcgi_log(LOG_DEBUG, "Python OSMOD");
  50. //importing libpyfcgi
  51. if(!pyinit_libpyfcgi())
  52. {
  53. exit(PYFCGI_FATAL);
  54. }
  55. pyfcgi_log(LOG_DEBUG, "libpyfcgi imported");
  56. // loading module
  57. entry_fun = import_entrypoint();
  58. pyfcgi_log(LOG_INFO, "Waiting request with %s.%s()",
  59. PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
  60. if(!entry_fun) //but exit if import failed
  61. {
  62. pyfcgi_log(LOG_ALERT, "Unable to import entrypoint");
  63. exit(PYFCGI_FATAL);
  64. }
  65. start_response = get_start_response();
  66. // Initialise FCGI request
  67. request = &(PyFCGI_conf.context.fcgi_request);
  68. if(FCGX_InitRequest(request, PyFCGI_conf.context.fcgi_socket,
  69. FCGI_FAIL_ACCEPT_ON_INTR) == -1)
  70. {
  71. pyfcgi_log(LOG_ALERT, "Unable to init FCGI request");
  72. exit(PYFCGI_FATAL);
  73. }
  74. _worker_idle = 0;
  75. worker_set_idle();
  76. // requests accepting loop
  77. count = 0;
  78. while ((!count || count != max_reqs) &&
  79. FCGX_Accept_r(request) == 0)
  80. {
  81. in_stream = request->in;
  82. out_stream = request->out;
  83. err_stream = request->err;
  84. envp = request->envp;
  85. pyfcgi_wd_arm();
  86. sem_post(PyFCGI_SEM(SEM_WREQS).sem); // increment request counter discarding OF
  87. gettimeofday(&start, NULL);
  88. worker_set_busy();
  89. count++;
  90. environ = update_pyenv(py_osmod, envp);
  91. _wtimeout = 0;
  92. libpyfcgi_clean_response();
  93. libpyfcgi.out = out_stream;
  94. libpyfcgi.in = in_stream;
  95. args = Py_BuildValue("OO", environ, start_response);
  96. // Call application function
  97. entry_ret = PyObject_CallObject(entry_fun, args);
  98. if(entry_ret && entry_ret != Py_None )
  99. {
  100. Py_INCREF(entry_ret);
  101. }
  102. if(_wtimeout)
  103. {
  104. PyObject *type, *exc, *tb, *tmp;
  105. // Changing exception to a TimeoutError, but keeping
  106. // the traceback to display
  107. PyErr_Fetch(&type, &exc, &tb);
  108. PyErr_SetString(PyExc_TimeoutError, "Request timeout");
  109. PyErr_Fetch(&type, &exc, &tmp);
  110. PyErr_Restore(type, exc, tb);
  111. log_expt(LOG_ERR);
  112. libpyfcgi_timeout(); // Sending headers if possible
  113. }
  114. if(PyErr_Occurred())
  115. {
  116. pyfcgi_log(LOG_ERR, "PEP333 entrypoint function triggered an exception");
  117. log_expt(LOG_ERR);
  118. }
  119. // able to process returned value
  120. // Simulate python call of libpyfcgi.write_body()
  121. if(entry_ret && entry_ret != Py_None)
  122. {
  123. _pyfcgi_write_body(entry_ret);
  124. if(PyErr_Occurred())
  125. {
  126. log_expt(LOG_ERR);
  127. }
  128. Py_DECREF(entry_ret);
  129. }
  130. // clean stuffs
  131. Py_DECREF(args);
  132. Py_DECREF(environ);
  133. FCGX_FClose(out_stream);
  134. FCGX_FClose(in_stream);
  135. FCGX_FClose(err_stream);
  136. FCGX_Finish_r(request);
  137. gettimeofday(&stop, NULL);
  138. stop.tv_sec = stop.tv_sec - start.tv_sec;
  139. stop.tv_usec = stop.tv_usec - start.tv_usec;
  140. if(stop.tv_usec < 0)
  141. {
  142. stop.tv_usec += 1000000;
  143. stop.tv_sec -= 1;
  144. }
  145. pyfcgi_wd_pause();
  146. if(_wtimeout)
  147. {
  148. exit(PYFCGI_TIMEOUT);
  149. }
  150. pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
  151. wrk_id, count, libpyfcgi.rep_sz, stop.tv_sec, stop.tv_usec);
  152. worker_set_idle();
  153. }
  154. worker_set_busy();
  155. return 0;
  156. }
  157. static inline int work333_send_result(FCGX_Stream *out, PyObject* ret)
  158. {
  159. return 0;
  160. }
  161. int work(int wrk_id)
  162. {
  163. PyObject *entry_fun, *pystdout_flush, *pystderr_flush, *py_osmod,
  164. *environ;
  165. FCGX_Stream *in_stream, *out_stream, *err_stream;
  166. char **envp;
  167. int count, pipe_out[2], pipe_err[2], pipe_ctl[2], err, piper_status;
  168. int max_reqs;
  169. struct sigaction act;
  170. struct timeval start, stop;
  171. sigset_t emptyset;
  172. char buf[PIPE_BUF];
  173. size_t rep_sz;
  174. piper_args_t piper_args;
  175. char *piper_stack;
  176. max_reqs = PyFCGI_conf.max_reqs;
  177. piper_args.wrk_id = wrk_id;
  178. piper_args.act = &act;
  179. // preparing sigaction for piper
  180. if(sigemptyset(&emptyset))
  181. {
  182. err = errno;
  183. pyfcgi_log( LOG_ALERT, "sigemptyset fails in piper : %s",
  184. strerror(err));
  185. exit(err);
  186. }
  187. act.sa_handler = worker_piper_sighandler;
  188. act.sa_mask = emptyset;
  189. //act.sa_flags = SA_RESTART;
  190. act.sa_flags = 0;
  191. act.sa_restorer = NULL;
  192. if( !(piper_stack = malloc(PIPER_STACK_SZ)) )
  193. {
  194. err = errno;
  195. pyfcgi_log(LOG_ALERT, "Error while allocating piper stack : %s",
  196. strerror(err));
  197. exit(err);
  198. }
  199. pyfcgi_log(LOG_INFO, "Worker %d started", wrk_id);
  200. update_python_path(); // add cwd to python path
  201. Py_Initialize(); // "start" python
  202. update_python_fd(pipe_out, pipe_err);
  203. piper_args.pystdout = pipe_out[0];
  204. piper_args.pystderr = pipe_err[0];
  205. fetch_pyflush(&pystdout_flush, &pystderr_flush);
  206. pyfcgi_log( LOG_INFO,
  207. "Worker[%d] Python started", wrk_id);
  208. //importing os
  209. py_osmod = python_osmod();
  210. // loading module
  211. entry_fun = import_entrypoint();
  212. pyfcgi_log( LOG_INFO,
  213. "Worker[%d] Waiting request with %s.%s()", wrk_id,
  214. PyFCGI_conf.py_entrymod, PyFCGI_conf.py_entryfun);
  215. if(!entry_fun) //but exit if import failed
  216. {
  217. pyfcgi_log(LOG_ALERT, "Unable to import entrypoint");
  218. exit(PYFCGI_FATAL);
  219. }
  220. if(pipe(pipe_ctl) == -1)
  221. {
  222. err = errno;
  223. pyfcgi_log(LOG_ALERT,
  224. "Worker[%d] Pipe fails for piper ctl : %s",
  225. wrk_id, strerror(err));
  226. exit(err);
  227. }
  228. piper_args.ctl_pipe = pipe_ctl[1];
  229. _worker_idle = 0;
  230. worker_set_idle();
  231. count = 0;
  232. while ((!count || count != max_reqs) && FCGX_Accept(&in_stream, &out_stream, &err_stream, &envp) >= 0)
  233. {
  234. pyfcgi_wd_arm();
  235. sem_post(PyFCGI_SEM(SEM_WREQS).sem); // increment request counter discarding OF
  236. gettimeofday(&start, NULL);
  237. worker_set_busy();
  238. count++;
  239. piper_args.out = out_stream;
  240. //piper_args.req_id = count;
  241. worker_piper_sigrcv = 0;
  242. // if(!PyFCGI_conf.pep333)
  243. /**@todo avoid clone on each request... (using named pipes ?) */
  244. pid_t pid = clone(worker_piper, piper_stack + PIPER_STACK_SZ - 1,
  245. CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | \
  246. SIGCHLD | \
  247. CLONE_FILES | CLONE_FS | CLONE_IO | CLONE_VM,
  248. &piper_args);
  249. if(pid < 0)
  250. {
  251. err = errno;
  252. pyfcgi_log(LOG_ALERT,
  253. "Worker[%d] req #%d Fork failed for piper : %s",
  254. wrk_id, count, strerror(err));
  255. exit(err);
  256. }
  257. environ = update_pyenv(py_osmod, envp);
  258. Py_DECREF(environ);
  259. //close(pipe_ctl[1]);
  260. PyObject_CallObject(entry_fun, NULL);
  261. if(PyErr_Occurred())
  262. {
  263. log_expt(LOG_ERR);
  264. }
  265. else
  266. {
  267. /*
  268. pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d funcall [OK]",
  269. wrk_id, count);
  270. */
  271. }
  272. PyObject_CallObject(pystdout_flush, NULL);
  273. PyObject_CallObject(pystderr_flush, NULL);
  274. read(pipe_ctl[0], &buf, 1); // unblock when child ready
  275. FCGX_FClose(in_stream);
  276. FCGX_FClose(err_stream);
  277. //close(pipe_ctl[0]);
  278. kill(pid, WPIPER_SIG); //indicate child python call ended
  279. waitpid(pid, &piper_status, 0);
  280. if(WIFSIGNALED(piper_status))
  281. {
  282. pyfcgi_log(LOG_ERR,
  283. "Woker[%d] req #%d piper terminated by sig %d",
  284. wrk_id, count, WTERMSIG(piper_status));
  285. exit(40);
  286. }
  287. else if(WEXITSTATUS(piper_status))
  288. {
  289. pyfcgi_log(LOG_ERR,
  290. "Woker[%d] req #%d piper exited with error status %d",
  291. wrk_id, count, WEXITSTATUS(piper_status));
  292. exit(41);
  293. }
  294. FCGI_Finish();
  295. if(ctl_get_rep_sz(pipe_ctl[0], &rep_sz))
  296. {
  297. rep_sz = 0;
  298. }
  299. //Increase sem showing the worker is idle
  300. worker_set_idle();
  301. gettimeofday(&stop, NULL);
  302. stop.tv_sec = stop.tv_sec - start.tv_sec;
  303. stop.tv_usec = stop.tv_usec - start.tv_usec;
  304. if(stop.tv_usec < 0)
  305. {
  306. stop.tv_usec += 1000000;
  307. stop.tv_sec -= 1;
  308. }
  309. pyfcgi_wd_pause();
  310. pyfcgi_log(LOG_DEBUG, "Worker[%d] request %d END [OK] %lu bytes in %ld.%06lds",
  311. wrk_id, count, rep_sz, stop.tv_sec, stop.tv_usec);
  312. }
  313. worker_set_busy();
  314. free(piper_stack);
  315. Py_Exit(count == max_reqs ?0:EXIT_PYERR);
  316. }
  317. /*
  318. void worker_piper(int wrk_id, int req_id, int pystdout, int pystderr,
  319. int ctl_pipe, FCGX_Stream* out)
  320. */
  321. int worker_piper(void *ptr)
  322. {
  323. piper_args_t *args;
  324. struct pollfd fds[2];
  325. int err, ret, cont, poll_ret;
  326. short revents, closed, nfds;
  327. char buf[PIPE_BUF];
  328. size_t out_sz;
  329. args = ptr;
  330. int wrk_id, pystdout, pystderr, ctl_pipe;
  331. //int req_id;
  332. FCGX_Stream *out;
  333. struct sigaction *act;
  334. wrk_id = args->wrk_id;
  335. //req_id = args->req_id;
  336. pystdout = args->pystdout;
  337. pystderr = args->pystderr;
  338. ctl_pipe = args->ctl_pipe;
  339. out = args->out;
  340. act = args->act;
  341. if(sigaction(WPIPER_SIG, act, NULL))
  342. {
  343. err = errno;
  344. pyfcgi_log( LOG_ALERT, "Unable to sigaction in piper : %s",
  345. strerror(err));
  346. exit(err);
  347. }
  348. write(ctl_pipe, " ", 1);
  349. //pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d piper", wrk_id, req_id);
  350. fds[0].fd = pystderr;
  351. fds[1].fd = pystdout;
  352. fds[0].events = fds[1].events = POLLIN;
  353. fds[0].revents = fds[1].revents = 0;
  354. nfds = 2;
  355. closed = out_sz = 0;
  356. cont = 2;
  357. while(cont)
  358. {
  359. poll_ret = poll(fds, nfds, 0);
  360. //pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d poll_ret = %d", wrk_id, req_id, poll_ret);
  361. if(poll_ret < 0)
  362. {
  363. err = errno;
  364. if(err == EINTR)
  365. {
  366. continue;
  367. }
  368. pyfcgi_log( LOG_WARNING, "Poll error : %s",
  369. strerror(err));
  370. fds[0].revents = fds[1].revents = 0;
  371. continue;
  372. }
  373. if(!poll_ret && worker_piper_sigrcv)
  374. {
  375. cont--;
  376. continue;
  377. }
  378. if(poll_ret && (revents = fds[1].revents))
  379. {
  380. /*
  381. pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d STDOUT evt !",
  382. wrk_id, req_id);
  383. */
  384. poll_ret--;
  385. if(revents & POLLIN)
  386. {
  387. /*
  388. pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d POLLIN STDOUT !",
  389. wrk_id, req_id);
  390. */
  391. ret = read(pystdout, buf, PIPE_BUF);
  392. /*
  393. pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d read(stdout) ret %d",
  394. wrk_id, req_id, ret);
  395. */
  396. if(ret < 0)
  397. {
  398. err = errno;
  399. if(err == EINTR)
  400. {
  401. continue;
  402. }
  403. pyfcgi_log( LOG_ERR,
  404. "Error reading python stdout : %s",
  405. strerror(err));
  406. exit(err);
  407. }
  408. //buf[ret] = '\0';
  409. ret = FCGX_PutStr(buf, ret, out);
  410. out_sz += ret;
  411. if(ret < PIPE_BUF && worker_piper_sigrcv)
  412. {
  413. FCGX_FClose(out);
  414. FCGI_Finish();
  415. write(ctl_pipe, &out_sz, sizeof(size_t));
  416. closed = 1;
  417. nfds = 1;
  418. }
  419. }
  420. //TODO handle other poll events
  421. }
  422. if(poll_ret && (revents = fds[0].revents))
  423. {
  424. /*
  425. pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d STDERR evt !",
  426. wrk_id, req_id);
  427. */
  428. if(revents & POLLIN)
  429. {
  430. /*
  431. pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d POLLIN STDERR !",
  432. wrk_id, req_id);
  433. */
  434. while(1)
  435. {
  436. ret = read(pystderr, buf, PIPE_BUF);
  437. if(ret < 0)
  438. {
  439. err = errno;
  440. if(err == EINTR)
  441. {
  442. continue;
  443. }
  444. pyfcgi_log( LOG_ERR,
  445. "Error reading python stdout : %s",
  446. strerror(err));
  447. exit(err);
  448. }
  449. buf[ret] = '\0';
  450. pyfcgi_log(LOG_ERR,
  451. "Worker[%d] Python error : %s",
  452. wrk_id, buf);
  453. break;
  454. }
  455. }
  456. //TODO handle other poll events
  457. }
  458. fds[0].revents = fds[1].revents = 0;
  459. }
  460. /*
  461. pyfcgi_log(LOG_DEBUG, "Worker[%d] req #%d piper exiting...",
  462. wrk_id, req_id);
  463. */
  464. if(!closed)
  465. {
  466. FCGX_FClose(out);
  467. FCGI_Finish();
  468. write(ctl_pipe, &out_sz, sizeof(size_t));
  469. }
  470. return 0;
  471. }
  472. void worker_log_pipes(int pipe_std, int pipe_err, PyObject *flush[2])
  473. {
  474. size_t ret;
  475. char buff[4096];
  476. int i, poll_ret;
  477. int loglevels[2] = {LOG_INFO, LOG_WARNING};
  478. struct pollfd fds[2];
  479. short isflush[2] = {0,0};
  480. char *outnames[2] = {"sys.stdout", "sys.stderr"};
  481. fds[0].fd = pipe_std;
  482. fds[1].fd = pipe_err;
  483. fds[0].events = fds[1].events = POLLIN;
  484. fds[0].revents = fds[1].revents = 0;
  485. for(i=0; i<2; i++)
  486. {
  487. do
  488. {
  489. poll_ret = poll(&(fds[i]), 1, 0);
  490. if(poll_ret)
  491. {
  492. ret = read(pipe_std, buff, 4096);
  493. if(ret == -1)
  494. {
  495. pyfcgi_log(LOG_ERR, "Error reading python %s pipe : %s",
  496. outnames[i], strerror(errno));
  497. exit(PYFCGI_WORKER_FAIL);
  498. }
  499. pyfcgi_log(loglevels[i], "Pyton %s : '%s'",
  500. outnames[i], buff);
  501. }
  502. if(!isflush[i] && (!poll_ret || ret < 4096))
  503. {
  504. isflush[i] = 1;
  505. PyObject_CallObject(flush[i], NULL);
  506. if(PyErr_Occurred())
  507. {
  508. pyfcgi_log(LOG_ERR, "Exception while flushing %s",
  509. outnames[i]);
  510. log_expt(LOG_ERR);
  511. exit(PYFCGI_WORKER_FAIL);
  512. }
  513. }
  514. }while(poll_ret && !isflush[i]);
  515. }
  516. }
  517. void worker_piper_sighandler(int signum)
  518. {
  519. worker_piper_sigrcv = 1;
  520. //pyfcgi_log(LOG_DEBUG, "SIG");
  521. }
  522. int ctl_get_rep_sz(int ctl_pipe, size_t* rep_sz)
  523. {
  524. struct pollfd fds;
  525. int ret, err;
  526. fds.fd = ctl_pipe;
  527. fds.events = POLLIN;
  528. fds.revents = 0;
  529. if( (ret = poll(&fds, 1, 0)) < 0)
  530. {
  531. err = errno;
  532. pyfcgi_log(LOG_ERR, "Failed to poll ctl pipe : %s",
  533. strerror(err));
  534. return -1;
  535. }
  536. if(!ret)
  537. {
  538. pyfcgi_log(LOG_ERR, "No data in ctl pipe for rep_sz...");
  539. return -1;
  540. }
  541. ret = read(ctl_pipe, rep_sz, sizeof(size_t));
  542. if(ret < 0)
  543. {
  544. pyfcgi_log(LOG_ERR, "Error reading ctl pipe : %s",
  545. strerror(errno));
  546. return -1;
  547. }
  548. if(ret < sizeof(size_t))
  549. {
  550. pyfcgi_log(LOG_ERR, "Incomplete read from ctl pipe, no rep_sz...");
  551. return -1;
  552. }
  553. return 0;
  554. }
  555. static void worker_set_idle()
  556. {
  557. int err;
  558. if(_worker_idle) { return; }
  559. if(sem_post(PyFCGI_SEM(SEM_WSTATE).sem) < 0)
  560. {
  561. err = errno;
  562. pyfcgi_log(LOG_ERR, "error incrementing the WSTATE semaphore : %s",
  563. strerror(err));
  564. return;
  565. }
  566. _worker_idle = 1;
  567. }
  568. static void worker_set_busy()
  569. {
  570. int err;
  571. short retry;
  572. struct timespec timeout;
  573. timeout.tv_sec = 0;
  574. timeout.tv_nsec = 100000; //0.0001s
  575. if(!_worker_idle) { return; }
  576. /**@todo The pool handler WILL decrement the sem to figure if the pool
  577. * is busy -__- Using sem_wait make sure that the worker will be able
  578. * to set busy, but it can also freeze if not able to set busy....
  579. * sem_timedwait require to get abstime -_- The better way is
  580. * maybe to nanosleep 0.01s and retry a sem_trywait... SysV sem are
  581. * better T_T
  582. */
  583. err = 0;
  584. retry = 10;
  585. do
  586. {
  587. err = 0;
  588. if(sem_trywait(PyFCGI_SEM(SEM_WSTATE).sem) < 0)
  589. {
  590. err = errno;
  591. if(err == EAGAIN)
  592. {
  593. nanosleep(&timeout, NULL);
  594. err = 1;
  595. }
  596. else
  597. {
  598. pyfcgi_log(LOG_ERR,
  599. "error decrementing the WSTATE semaphore : %s",
  600. strerror(err));
  601. kill(PyFCGI_conf.context.ppid, SIGTERM);
  602. return;
  603. }
  604. }
  605. retry--;
  606. }while(err & retry);
  607. if(err)
  608. {
  609. pyfcgi_log(LOG_ALERT,
  610. "Unable to set busy ! WSTATE sem is allready 0 !!!");
  611. _worker_idle = 0;
  612. kill(PyFCGI_conf.context.ppid, SIGTERM);
  613. kill(PyFCGI_conf.context.pid, SIGTERM);
  614. return;
  615. }
  616. _worker_idle = 0;
  617. }
  618. void worker_sighandler(int signum)
  619. {
  620. pyfcgi_log(LOG_INFO, "%s signal received, exiting...", strsignal(signum));
  621. worker_set_busy();
  622. pyfcgi_IPC_close(IPC_WSTATE | IPC_WREQS);
  623. exit(0);
  624. }
  625. void worker_sigalrmhandler(int signum)
  626. {
  627. _wtimeout = 1;
  628. if(!_worker_idle)
  629. {
  630. // Call PyErr_Interrupt() in order to stop python
  631. PyErr_SetInterrupt();
  632. }
  633. worker_set_busy();
  634. pyfcgi_IPC_close(IPC_WSTATE | IPC_WREQS);
  635. return;
  636. }