diff --git a/loop.c b/loop.c index bb7448e9..02e5afa3 100644 --- a/loop.c +++ b/loop.c @@ -80,9 +80,6 @@ void *simple_loop(void *arg1) { while (uwsgi.workers[uwsgi.mywid].manage_next_request) { - - UWSGI_CLEAR_STATUS; - wsgi_req_setup(wsgi_req, core_id, NULL); if (wsgi_req_accept(wsgi_req)) { @@ -96,11 +93,7 @@ void *simple_loop(void *arg1) { uwsgi_close_request(wsgi_req); } -#ifdef UWSGI_THREADING pthread_exit(NULL); -#else - exit(0); -#endif //never here return NULL; @@ -164,9 +157,6 @@ void *zeromq_loop(void *arg1) { while (uwsgi.workers[uwsgi.mywid].manage_next_request) { - - UWSGI_CLEAR_STATUS; - wsgi_req_setup(wsgi_req, core_id, NULL); uwsgi.edge_triggered = 1; @@ -210,12 +200,8 @@ void *zeromq_loop(void *arg1) { uwsgi_close_request(wsgi_req); } -#ifdef UWSGI_THREADING - pthread_exit(NULL); -#else - exit(0); -#endif + pthread_exit(NULL); //never here return NULL; } diff --git a/master.c b/master.c index 507447e4..26c2f6b0 100644 --- a/master.c +++ b/master.c @@ -136,9 +136,6 @@ void master_loop(char **argv, char **environ) { pid_t diedpid; int waitpid_status; - int working_workers = 0; - int blocking_workers = 0; - int ready_to_reload = 0; int ready_to_die = 0; @@ -848,8 +845,6 @@ void master_loop(char **argv, char **environ) { master_cycles++; - working_workers = 0; - blocking_workers = 0; // recalculate requests counter on race conditions risky configurations // a bit of inaccuracy is better than locking;) @@ -940,14 +935,9 @@ void master_loop(char **argv, char **environ) { uwsgi.workers[i].harakiri = 0; } } - /* load counters */ - if (uwsgi.workers[i].status & UWSGI_STATUS_IN_REQUEST) - working_workers++; - if (uwsgi.workers[i].status & UWSGI_STATUS_BLOCKING) - blocking_workers++; - - uwsgi.workers[i].last_running_time = uwsgi.workers[i].running_time; + // need to find a better way + //uwsgi.workers[i].last_running_time = uwsgi.workers[i].running_time; } #ifdef UWSGI_UDP diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 8c116b86..5068e78e 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -874,7 +874,6 @@ PyObject *py_uwsgi_lock(PyObject * self, PyObject * args) { if (uwsgi.numproc > 1 && uwsgi.mypid != uwsgi.workers[0].pid) { #endif uwsgi_lock(uwsgi.user_lock); - UWSGI_SET_LOCKING; } Py_INCREF(Py_None); @@ -884,7 +883,6 @@ PyObject *py_uwsgi_lock(PyObject * self, PyObject * args) { PyObject *py_uwsgi_unlock(PyObject * self, PyObject * args) { uwsgi_unlock(uwsgi.user_lock); - UWSGI_UNSET_LOCKING; Py_INCREF(Py_None); return Py_None; @@ -2007,7 +2005,6 @@ PyObject *py_uwsgi_reload(PyObject * self, PyObject * args) { PyObject *py_uwsgi_set_blocking(PyObject * self, PyObject * args) { if (uwsgi.master_process) { - uwsgi.workers[uwsgi.mywid].status |= UWSGI_STATUS_BLOCKING; Py_INCREF(Py_True); return Py_True; } diff --git a/utils.c b/utils.c index 8b29bd81..0829b449 100644 --- a/utils.c +++ b/utils.c @@ -613,12 +613,13 @@ void wsgi_req_setup(struct wsgi_request *wsgi_req, int async_id, struct uwsgi_so wsgi_req->socket = uwsgi_sock; } + uwsgi.core[wsgi_req->async_id]->in_request = 0; } #ifdef UWSGI_ASYNC int wsgi_req_async_recv(struct wsgi_request *wsgi_req) { - UWSGI_SET_IN_REQUEST; + uwsgi.core[wsgi_req->async_id]->in_request = 1; gettimeofday(&wsgi_req->start_of_request, NULL); @@ -643,7 +644,7 @@ int wsgi_req_async_recv(struct wsgi_request *wsgi_req) { int wsgi_req_recv(struct wsgi_request *wsgi_req) { - UWSGI_SET_IN_REQUEST; + uwsgi.core[wsgi_req->async_id]->in_request = 1; gettimeofday(&wsgi_req->start_of_request, NULL); diff --git a/uwsgi.c b/uwsgi.c index 3f41f735..1edffa54 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -322,48 +322,69 @@ void warn_pipe() { } } -void gracefully_kill(int signum) { - uwsgi_log("Gracefully killing worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid); - if (UWSGI_IS_IN_REQUEST) { - uwsgi.workers[uwsgi.mywid].manage_next_request = 0; - } - else { - reload_me(0); - } -} +#ifdef UWSGI_THREADING +// in threading mode we need to use the cancel pthread subsystem +void wait_for_threads() { + int i, ret; -void reload_me(int signum) { - exit(UWSGI_RELOAD_CODE); + pthread_mutex_lock(&uwsgi.six_feet_under_lock); + for(i=0;ithread_id, pthread_self())) { + pthread_cancel(uwsgi.core[i]->thread_id); + } + } + + // wait for thread termination + for(i=0;ithread_id, pthread_self())) { + ret = pthread_join(uwsgi.core[i]->thread_id, NULL); + if (ret) { + uwsgi_log("pthread_join() = %d\n", ret); + } + } + } + + pthread_mutex_unlock(&uwsgi.six_feet_under_lock); +} +#endif + + +void gracefully_kill(int signum) { + struct wsgi_request *wsgi_req = current_wsgi_req(); + + uwsgi_log("Gracefully killing worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid); + uwsgi.workers[uwsgi.mywid].manage_next_request = 0; +#ifdef UWSGI_THREADING + if (uwsgi.threads > 1) { + wait_for_threads(); + if (!uwsgi.core[wsgi_req->async_id]->in_request) { + exit(UWSGI_RELOAD_CODE); + } + return; + // never here + } +#endif + + // still not found a way to gracefully reload in async mode + if (uwsgi.async > 1) { + exit(UWSGI_RELOAD_CODE); + } + + if (!uwsgi.core[0]->in_request) { + exit(UWSGI_RELOAD_CODE); + } } void end_me(int signum) { exit(UWSGI_END_CODE); } + void goodbye_cruel_world() { - // in threading mode we need to use the cancel pthread subsystem #ifdef UWSGI_THREADING if (uwsgi.threads > 1 && !uwsgi.to_hell) { - pthread_mutex_lock(&uwsgi.six_feet_under_lock); - int i, ret; - for(i=0;ithread_id, pthread_self())) { - pthread_cancel(uwsgi.core[i]->thread_id); - } - } - - // wait for thread termination - for(i=0;ithread_id, pthread_self())) { - ret = pthread_join(uwsgi.core[i]->thread_id, NULL); - if (ret) { - uwsgi_log("pthread_join() = %d\n", ret); - } - } - } - - pthread_mutex_unlock(&uwsgi.six_feet_under_lock); + wait_for_threads(); } #endif @@ -2105,7 +2126,7 @@ int uwsgi_start(void *v_argv) { } uwsgi_unix_signal(SIGHUP, gracefully_kill); uwsgi_unix_signal(SIGINT, end_me); - uwsgi_unix_signal(SIGTERM, reload_me); + uwsgi_unix_signal(SIGTERM, end_me); uwsgi_unix_signal(SIGUSR1, stats); diff --git a/uwsgi.h b/uwsgi.h index c56b5880..de5a8871 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -436,23 +436,6 @@ struct uwsgi_opt { #define UWSGI_ACCEPTING 2 #define UWSGI_PAUSED 3 -#define UWSGI_CLEAR_STATUS uwsgi.workers[uwsgi.mywid].status = 0 - -#define UWSGI_STATUS_IN_REQUEST 1 << 0 -#define UWSGI_IS_IN_REQUEST uwsgi.workers[uwsgi.mywid].status & UWSGI_STATUS_IN_REQUEST -#define UWSGI_SET_IN_REQUEST uwsgi.workers[uwsgi.mywid].status |= UWSGI_STATUS_IN_REQUEST -#define UWSGI_UNSET_IN_REQUEST uwsgi.workers[uwsgi.mywid].status ^= UWSGI_STATUS_IN_REQUEST - -#define UWSGI_STATUS_BLOCKING 1 << 1 -#define UWSGI_IS_BLOCKING uwsgi.workers[uwsgi.mywid].status & UWSGI_STATUS_BLOCKING -#define UWSGI_SET_BLOCKING uwsgi.workers[uwsgi.mywid].status |= UWSGI_STATUS_BLOCKING -#define UWSGI_UNSET_BLOCKING uwsgi.workers[uwsgi.mywid].status ^= UWSGI_STATUS_BLOCKING - -#define UWSGI_STATUS_LOCKING 1 << 2 -#define UWSGI_IS_LOCKING uwsgi.workers[uwsgi.mywid].status & UWSGI_STATUS_LOCKING -#define UWSGI_SET_LOCKING uwsgi.workers[uwsgi.mywid].status |= UWSGI_STATUS_LOCKING -#define UWSGI_UNSET_LOCKING uwsgi.workers[uwsgi.mywid].status ^= UWSGI_STATUS_LOCKING - #ifdef __linux__ #include #elif __sun__ @@ -1368,6 +1351,7 @@ struct uwsgi_core { //multiple ts per - core are needed only with multiple_interpreter + threads void *ts[MAX_APPS]; + int in_request; }; struct uwsgi_worker { @@ -1404,7 +1388,6 @@ void gracefully_kill(int); void reap_them_all(int); void kill_them_all(int); void grace_them_all(int); -void reload_me(int); void end_me(int); int bind_to_unix(char *, int, int, int); int bind_to_tcp(char *, int, char *);