From 9ac598b9c3bb20dda0fc1a5cb4bf2cbc4b076ec8 Mon Sep 17 00:00:00 2001 From: "roberto@maverick64" Date: Fri, 21 Jan 2011 18:08:04 +0100 Subject: [PATCH] better harakiri on threaded modes --- loop.c | 8 ++++++++ signal.c | 3 +++ spooler.c | 52 ++++++++++++---------------------------------------- uwsgi.c | 41 +++++++++++++++++++++++++++++++++-------- uwsgi.h | 5 +++-- 5 files changed, 59 insertions(+), 50 deletions(-) diff --git a/loop.c b/loop.c index 8ea9cffb..ab1c6bef 100644 --- a/loop.c +++ b/loop.c @@ -130,11 +130,19 @@ void complex_loop() { } } + else if ( (int) uwsgi.async_events[i].ASYNC_FD == uwsgi.sockets[uwsgi.sockets_cnt].fd) { + // wake up cores waiting for signal + char byte; + if (read(uwsgi.sockets[uwsgi.sockets_cnt].fd, &byte, 1) == 1) { + uwsgi_log("signal %c received\n", byte); + } + } else { uwsgi.wsgi_req = find_wsgi_req_by_fd(uwsgi.async_events[i].ASYNC_FD, uwsgi.async_events[i].ASYNC_EV); if (uwsgi.wsgi_req) { uwsgi.wsgi_req->async_status = UWSGI_AGAIN; uwsgi.wsgi_req->async_waiting_fd = -1; + uwsgi.wsgi_req->async_waiting_signal = -1; uwsgi.wsgi_req->async_waiting_fd_monitored = 0; uwsgi.wsgi_req->async_timeout = 0; } diff --git a/signal.c b/signal.c index ddcd462e..a4f94dcc 100644 --- a/signal.c +++ b/signal.c @@ -101,5 +101,8 @@ void uwsgi_route_signal(uint8_t sig) { uwsgi_log("could not deliver signal %d to workers pool\n", sig); } break; + case KIND_NULL: + // unmanaged signal, pass to all the children + break; }; } diff --git a/spooler.c b/spooler.c index 4af41113..5029e659 100644 --- a/spooler.c +++ b/spooler.c @@ -28,21 +28,13 @@ pid_t spooler_start() { int spool_request(char *filename, int rn, char *buffer, int size) { - char hostname[256 + 1]; struct timeval tv; int fd; struct uwsgi_header uh; - if (gethostname(hostname, 256)) { - uwsgi_error("gethostname()"); - return 0; - } - gettimeofday(&tv, NULL); - hostname[256] = 0; - - if (snprintf(filename, 1024, "%s/uwsgi_spoolfile_on_%s_%d_%d_%llu_%llu", uwsgi.spool_dir, hostname, (int) getpid(), rn, (unsigned long long) tv.tv_sec, (unsigned long long) tv.tv_usec) <= 0) { + if (snprintf(filename, 1024, "%s/uwsgi_spoolfile_on_%s_%d_%d_%llu_%llu", uwsgi.spool_dir, uwsgi.hostname, (int) getpid(), rn, (unsigned long long) tv.tv_sec, (unsigned long long) tv.tv_usec) <= 0) { return 0; } @@ -90,18 +82,20 @@ clear: unlink(filename); close(fd); return 0; - } +} - void spooler() { - DIR *sdir; - struct dirent *dp; - //PyObject *spooler_callable, *spool_result, *spool_tuple, *spool_env; - int spool_fd; - uint16_t uwstrlen; - int rlen = 0; - int datasize; +void spooler() { + DIR *sdir; + struct dirent *dp; + + //PyObject *spooler_callable, *spool_result, *spool_tuple, *spool_env; + + int spool_fd; + uint16_t uwstrlen; + int rlen = 0; + int datasize; // prevent process blindly reading stdin to make mess int nullfd; @@ -111,34 +105,12 @@ clear: char *key; char *val; - /* - spool_tuple = PyTuple_New(1); - - if (!spool_tuple) { - uwsgi_log( "could not create spooler tuple.\n"); - exit(1); - } - - - spool_env = PyDict_New(); - if (!spool_env) { - uwsgi_log( "could not create spooler env.\n"); - exit(1); - } - - if (PyTuple_SetItem(spool_tuple, 0, spool_env)) { - PyErr_Print(); - exit(1); - } - */ - if (chdir(uwsgi.spool_dir)) { uwsgi_error("chdir()"); exit(1); } // asked by Marco Beri - #ifdef __HAIKU__ uwsgi_log( "lowering spooler priority to %d\n", B_LOW_PRIORITY); set_thread_priority(find_thread(NULL), B_LOW_PRIORITY); diff --git a/uwsgi.c b/uwsgi.c index 8bd38509..ca922143 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -274,14 +274,34 @@ void stats() void what_i_am_doing() { - struct wsgi_request *wsgi_req = current_wsgi_req(); + struct wsgi_request *wsgi_req; + int i; - if (uwsgi.async < 2 && wsgi_req->uri_len > 0) { - - if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < time(NULL)) { - uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) & wsgi_req->start_of_request.tv_sec)); - } else { - uwsgi_log("SIGUSR2: --- uWSGI worker %d (pid: %d) is managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) & wsgi_req->start_of_request.tv_sec)); + if (uwsgi.cores > 1) { + for(i=0;iuri_len > 0) { + if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < time(NULL)) { + uwsgi_log("HARAKIRI: --- uWSGI worker %d core %d (pid: %d) WAS managing request %.*s since %.*s ---\n", + (int) uwsgi.mywid, i, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) & wsgi_req->start_of_request.tv_sec)); + } + else { + uwsgi_log("SIGUSR2: --- uWSGI worker %d core %d (pid: %d) is managing request %.*s since %.*s ---\n", + (int) uwsgi.mywid, i, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) & wsgi_req->start_of_request.tv_sec)); + } + } + } + } + else { + wsgi_req = uwsgi.wsgi_requests[0]; + if (wsgi_req->uri_len > 0) { + if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < time(NULL)) { + uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS managing request %.*s since %.*s ---\n", + (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) & wsgi_req->start_of_request.tv_sec)); + } else { + uwsgi_log("SIGUSR2: --- uWSGI worker %d (pid: %d) is managing request %.*s since %.*s ---\n", + (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) & wsgi_req->start_of_request.tv_sec)); + } } } } @@ -1421,7 +1441,7 @@ uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100 } } #ifdef UWSGI_SPOOLER - if (uwsgi.spool_dir != NULL && uwsgi.numproc > 0) { + if (uwsgi.spool_dir != NULL && uwsgi.sockets_cnt > 0) { uwsgi.shared->spooler_pid = spooler_start(); } #endif @@ -1580,6 +1600,11 @@ uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100 if (uwsgi.master_process) { uwsgi.sockets_poll[uwsgi.sockets_cnt].fd = uwsgi.shared->worker_signal_pipe[1]; uwsgi.sockets_poll[uwsgi.sockets_cnt].events = POLLIN; +#ifdef UWSGI_ASYNC + if (uwsgi.async > 1) { + async_add(uwsgi.async_queue, uwsgi.sockets_poll[uwsgi.sockets_cnt].fd, ASYNC_IN); + } +#endif } diff --git a/uwsgi.h b/uwsgi.h index aeb22109..4998855f 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -200,7 +200,7 @@ union uwsgi_sockaddr_ptr { // Gateways are processes (managed by the master) that extends the // server core features -// -- Gateways can prefork -- +// -- Gateways can prefork or spawn threads -- struct uwsgi_gateway { @@ -208,6 +208,7 @@ struct uwsgi_gateway { void (*loop)(void); pid_t pid; int num; + int use_signals; }; // maintain alignment here !!! @@ -586,6 +587,7 @@ struct wsgi_request { int async_waiting_fd; int async_waiting_fd_type; int async_waiting_fd_monitored; + int async_waiting_signal; int switches; @@ -1071,7 +1073,6 @@ struct uwsgi_worker { uint64_t exceptions; - }; char *uwsgi_get_cwd(void);