Skip to content

Commit

Permalink
Merge branch 'master' of ssh://github.com/unbit/uwsgi
Browse files Browse the repository at this point in the history
  • Loading branch information
unbit committed Dec 10, 2014
2 parents 5e29359 + 69be96b commit 3f8c585
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 36 deletions.
13 changes: 12 additions & 1 deletion core/emperor.c
Expand Up @@ -12,6 +12,7 @@ void emperor_send_stats(int);

time_t emperor_throttle;
int emperor_throttle_level;
int emperor_warming_up = 1;

struct uwsgi_instance *ui;

Expand Down Expand Up @@ -1081,7 +1082,16 @@ void emperor_add_with_attrs(struct uwsgi_emperor_scanner *ues, char *name, time_
#ifdef UWSGI_DEBUG
uwsgi_log("emperor throttle = %d\n", emperor_throttle_level);
#endif
usleep(emperor_throttle_level);
if (emperor_warming_up) {
if (emperor_throttle_level > 0) {
// wait 10 milliseconds in case of fork-bombing
// pretty random value, but should avoid the load average to increase
usleep(10);
}
}
else {
usleep(emperor_throttle_level);
}

if (uwsgi.emperor_tyrant) {
if (uid == 0 || gid == 0) {
Expand Down Expand Up @@ -1841,6 +1851,7 @@ void uwsgi_emperor_run_scanners(void) {
ues->monitor->func(ues);
ues = ues->next;
}
emperor_warming_up = 0;
}

void emperor_build_scanners() {
Expand Down
13 changes: 3 additions & 10 deletions core/fsmon.c
Expand Up @@ -61,16 +61,9 @@ static void fsmon_reload(struct uwsgi_fsmon *fs) {
}

static void fsmon_brutal_reload(struct uwsgi_fsmon *fs) {
if (uwsgi.die_on_term) {
uwsgi_block_signal(SIGQUIT);
reap_them_all(0);
uwsgi_unblock_signal(SIGQUIT);
}
else {
uwsgi_block_signal(SIGTERM);
reap_them_all(0);
uwsgi_unblock_signal(SIGTERM);
}
uwsgi_block_signal(SIGQUIT);
reap_them_all(0);
uwsgi_unblock_signal(SIGQUIT);
}

static void fsmon_signal(struct uwsgi_fsmon *fs) {
Expand Down
12 changes: 4 additions & 8 deletions core/master.c
Expand Up @@ -333,14 +333,10 @@ int master_loop(char **argv, char **environ) {

uwsgi_unix_signal(SIGTSTP, suspend_resume_them_all);
uwsgi_unix_signal(SIGHUP, grace_them_all);
if (uwsgi.die_on_term) {
uwsgi_unix_signal(SIGTERM, kill_them_all);
uwsgi_unix_signal(SIGQUIT, reap_them_all);
}
else {
uwsgi_unix_signal(SIGTERM, reap_them_all);
uwsgi_unix_signal(SIGQUIT, kill_them_all);
}

uwsgi_unix_signal(SIGTERM, kill_them_all);
uwsgi_unix_signal(SIGQUIT, reap_them_all);

uwsgi_unix_signal(SIGINT, kill_them_all);
uwsgi_unix_signal(SIGUSR1, stats);

Expand Down
13 changes: 13 additions & 0 deletions core/master_checks.c
Expand Up @@ -138,12 +138,25 @@ void uwsgi_master_check_idle() {
uwsgi.workers[i].cheaped = 1;
if (uwsgi.workers[i].pid == 0)
continue;
// first send SIGINT
kill(uwsgi.workers[i].pid, SIGINT);
// and start waiting upto 3 seconds
int j;
for(j=0;j<3;j++) {
sleep(1);
int ret = waitpid(uwsgi.workers[i].pid, &waitpid_status, WNOHANG);
if (ret == 0) continue;
if (ret == (int) uwsgi.workers[i].pid) goto done;
// on error, directly send SIGKILL
break;
}
kill(uwsgi.workers[i].pid, SIGKILL);
if (waitpid(uwsgi.workers[i].pid, &waitpid_status, 0) < 0) {
if (errno != ECHILD)
uwsgi_error("uwsgi_master_check_idle()/waitpid()");
}
else {
done:
uwsgi.workers[i].pid = 0;
uwsgi.workers[i].rss_size = 0;
uwsgi.workers[i].vsz_size = 0;
Expand Down
13 changes: 3 additions & 10 deletions core/master_events.c
Expand Up @@ -118,16 +118,9 @@ int uwsgi_master_manage_events(int interesting_fd) {
else {
uwsgi_log_verbose("*** fd %d ready !!! ***\n", interesting_fd);
}
if (uwsgi.die_on_term) {
uwsgi_block_signal(SIGQUIT);
reap_them_all(0);
uwsgi_unblock_signal(SIGQUIT);
}
else {
uwsgi_block_signal(SIGTERM);
reap_them_all(0);
uwsgi_unblock_signal(SIGTERM);
}
uwsgi_block_signal(SIGQUIT);
reap_them_all(0);
uwsgi_unblock_signal(SIGQUIT);
if (usl->custom2 > 8) free(tmp);
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions core/master_utils.c
Expand Up @@ -162,6 +162,7 @@ int uwsgi_calc_cheaper(void) {
ignore_algo = 1;
}
uwsgi.cheaper_fifo_delta = 0;
goto safe;
}

// if cheaper limits wants to change worker count, then skip cheaper algo
Expand All @@ -172,6 +173,7 @@ int uwsgi_calc_cheaper(void) {
needed_workers = 0;
}

safe:
if (needed_workers > 0) {
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.workers[i].cheaped == 1 && uwsgi.workers[i].pid == 0) {
Expand Down
5 changes: 4 additions & 1 deletion core/socket.c
Expand Up @@ -191,7 +191,10 @@ int bind_to_unix(char *socket_name, int listen_queue, int chmod_socket, int abst

memset(uws_addr, 0, sizeof(struct sockaddr_un));
serverfd = create_server_socket(AF_UNIX, SOCK_STREAM);
if (serverfd < 0) return -1;
if (serverfd < 0) {
free(uws_addr);
return -1;
}
if (abstract_socket == 0) {
if (unlink(socket_name) != 0 && errno != ENOENT) {
uwsgi_error("error removing unix socket, unlink()");
Expand Down
2 changes: 1 addition & 1 deletion core/uwsgi.c
Expand Up @@ -265,7 +265,7 @@ static struct uwsgi_option uwsgi_base_options[] = {
{"worker-reload-mercy", required_argument, 0, "set the maximum time (in seconds) a worker can take to reload/shutdown (default is 60)", uwsgi_opt_set_int, &uwsgi.worker_reload_mercy, 0},
{"mule-reload-mercy", required_argument, 0, "set the maximum time (in seconds) a mule can take to reload/shutdown (default is 60)", uwsgi_opt_set_int, &uwsgi.mule_reload_mercy, 0},
{"exit-on-reload", no_argument, 0, "force exit even if a reload is requested", uwsgi_opt_true, &uwsgi.exit_on_reload, 0},
{"die-on-term", no_argument, 0, "exit instead of brutal reload on SIGTERM", uwsgi_opt_true, &uwsgi.die_on_term, 0},
{"die-on-term", no_argument, 0, "exit instead of brutal reload on SIGTERM (no more needed)", uwsgi_opt_deprecated, &uwsgi.die_on_term, 0},
{"force-gateway", no_argument, 0, "force the spawn of the first registered gateway without a master", uwsgi_opt_true, &uwsgi.force_gateway, 0},
{"help", no_argument, 'h', "show this help", uwsgi_help, NULL, UWSGI_OPT_IMMEDIATE},
{"usage", no_argument, 'h', "show this help", uwsgi_help, NULL, UWSGI_OPT_IMMEDIATE},
Expand Down
42 changes: 42 additions & 0 deletions plugins/gevent/gevent.c
Expand Up @@ -101,6 +101,31 @@ PyObject *py_uwsgi_gevent_graceful(PyObject *self, PyObject *args) {
return Py_None;
}

PyObject *py_uwsgi_gevent_int(PyObject *self, PyObject *args) {

uwsgi_log("Brutally killing worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid);
uwsgi.workers[uwsgi.mywid].manage_next_request = 0;

uwsgi_log_verbose("stopping gevent signals watchers for worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid);
PyObject_CallMethod(ugevent.my_signal_watcher, "stop", NULL);
PyObject_CallMethod(ugevent.signal_watcher, "stop", NULL);

uwsgi_log_verbose("stopping gevent sockets watchers for worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid);
int i,count = uwsgi_count_sockets(uwsgi.sockets);
for(i=0;i<count;i++) {
PyObject_CallMethod(ugevent.watchers[i], "stop", NULL);
}
uwsgi_log_verbose("main gevent watchers stopped for worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid);

if (!ugevent.wait_for_hub) {
PyObject_CallMethod(ugevent.ctrl_gl, "kill", NULL);
}

Py_INCREF(Py_None);
return Py_None;
}


static void uwsgi_gevent_gbcw() {

// already running
Expand Down Expand Up @@ -352,6 +377,7 @@ PyMethodDef uwsgi_gevent_signal_def[] = { {"uwsgi_gevent_signal", py_uwsgi_geven
PyMethodDef uwsgi_gevent_my_signal_def[] = { {"uwsgi_gevent_my_signal", py_uwsgi_gevent_my_signal, METH_VARARGS, ""} };
PyMethodDef uwsgi_gevent_signal_handler_def[] = { {"uwsgi_gevent_signal_handler", py_uwsgi_gevent_signal_handler, METH_VARARGS, ""} };
PyMethodDef uwsgi_gevent_unix_signal_handler_def[] = { {"uwsgi_gevent_unix_signal_handler", py_uwsgi_gevent_graceful, METH_VARARGS, ""} };
PyMethodDef uwsgi_gevent_unix_signal_int_handler_def[] = { {"uwsgi_gevent_unix_signal_int_handler", py_uwsgi_gevent_int, METH_VARARGS, ""} };
PyMethodDef uwsgi_gevent_ctrl_gl_def[] = { {"uwsgi_gevent_ctrl_gl_handler", py_uwsgi_gevent_ctrl_gl, METH_VARARGS, ""} };

static void gil_gevent_get() {
Expand Down Expand Up @@ -510,6 +536,22 @@ static void gevent_loop() {

python_call(ugevent.signal, ge_signal_tuple, 0, NULL);

// map SIGINT/SIGTERM with gevent.signal
ge_signal_tuple = PyTuple_New(2);
PyTuple_SetItem(ge_signal_tuple, 0, PyInt_FromLong(SIGINT));
PyObject *uwsgi_gevent_unix_signal_int_handler = PyCFunction_New(uwsgi_gevent_unix_signal_int_handler_def, NULL);
Py_INCREF(uwsgi_gevent_unix_signal_int_handler);
PyTuple_SetItem(ge_signal_tuple, 1, uwsgi_gevent_unix_signal_int_handler);
python_call(ugevent.signal, ge_signal_tuple, 0, NULL);

ge_signal_tuple = PyTuple_New(2);
PyTuple_SetItem(ge_signal_tuple, 0, PyInt_FromLong(SIGTERM));
PyTuple_SetItem(ge_signal_tuple, 1, uwsgi_gevent_unix_signal_int_handler);
python_call(ugevent.signal, ge_signal_tuple, 0, NULL);




PyObject *wait_for_me = ugevent.hub;

if (!ugevent.wait_for_hub) {
Expand Down
15 changes: 10 additions & 5 deletions plugins/mono/mono_plugin.c
Expand Up @@ -543,7 +543,7 @@ static int uwsgi_mono_request(struct wsgi_request *wsgi_req) {

wsgi_req->app_id = uwsgi_get_app_id(NULL, key, key_len, mono_plugin.modifier1);
// if it is -1, try to load a dynamic app
if (wsgi_req->app_id == -1) {
if (wsgi_req->app_id == -1 && key_len > 0) {
if (uwsgi.threads > 1) {
pthread_mutex_lock(&umono.lock_loader);
}
Expand All @@ -562,10 +562,15 @@ static int uwsgi_mono_request(struct wsgi_request *wsgi_req) {


if (wsgi_req->app_id == -1) {
uwsgi_500(wsgi_req);
uwsgi_log("--- unable to find Mono/ASP.NET application ---\n");
// nothing to clear/free
return UWSGI_OK;
if (!uwsgi.no_default_app && uwsgi.default_app > -1 && uwsgi_apps[uwsgi.default_app].modifier1 == mono_plugin.modifier1) {
wsgi_req->app_id = uwsgi.default_app;
}
else {
uwsgi_500(wsgi_req);
uwsgi_log("--- unable to find Mono/ASP.NET application ---\n");
// nothing to clear/free
return UWSGI_OK;
}
}

struct uwsgi_app *app = &uwsgi_apps[wsgi_req->app_id];
Expand Down

0 comments on commit 3f8c585

Please sign in to comment.