diff --git a/plugins/python/pyloader.c b/plugins/python/pyloader.c index ceb595df..19882219 100644 --- a/plugins/python/pyloader.c +++ b/plugins/python/pyloader.c @@ -539,6 +539,7 @@ PyObject *uwsgi_mount_loader(void *arg1) { if ( !strcmp(what+strlen(what)-3, ".py") || !strcmp(what+strlen(what)-5, ".wsgi")) { callable = uwsgi_file_loader((void *)what); + if (!callable) exit(1); } else if (!strcmp(what+strlen(what)-4, ".ini")) { callable = uwsgi_paste_loader((void *)what); @@ -605,30 +606,44 @@ PyObject *uwsgi_file_loader(void *arg1) { char *callable = up.callable; if (!callable) callable = "application"; - wsgi_file_module = uwsgi_pyimport_by_filename(uwsgi_concat2("uwsgi_file_", uwsgi_pythonize(filename)), filename); + char *py_filename = uwsgi_concat2("uwsgi_file_", uwsgi_pythonize(filename)); + + wsgi_file_module = uwsgi_pyimport_by_filename(py_filename, filename); if (!wsgi_file_module) { PyErr_Print(); - exit(1); + free(py_filename); + return NULL; } wsgi_file_dict = PyModule_GetDict(wsgi_file_module); if (!wsgi_file_dict) { PyErr_Print(); - exit(1); + Py_DECREF(wsgi_file_module); + free(py_filename); + return NULL; } wsgi_file_callable = PyDict_GetItemString(wsgi_file_dict, callable); if (!wsgi_file_callable) { PyErr_Print(); + Py_DECREF(wsgi_file_dict); + Py_DECREF(wsgi_file_module); + free(py_filename); uwsgi_log( "unable to find \"application\" callable in file %s\n", filename); - exit(1); + return NULL; } if (!PyFunction_Check(wsgi_file_callable) && !PyCallable_Check(wsgi_file_callable)) { uwsgi_log( "\"application\" must be a callable object in file %s\n", filename); - exit(1); + Py_DECREF(wsgi_file_callable); + Py_DECREF(wsgi_file_dict); + Py_DECREF(wsgi_file_module); + free(py_filename); + return NULL; } + free(py_filename); + return wsgi_file_callable; } diff --git a/plugins/python/python_plugin.c b/plugins/python/python_plugin.c index 8d42c368..dcc8af82 100644 --- a/plugins/python/python_plugin.c +++ b/plugins/python/python_plugin.c @@ -1097,6 +1097,9 @@ void uwsgi_python_enable_threads() { up.gil_get = gil_real_get; up.gil_release = gil_real_release; + up.swap_ts = simple_threaded_swap_ts; + up.reset_ts = simple_threaded_reset_ts; + if (uwsgi.threads > 1) { up.swap_ts = threaded_swap_ts; up.reset_ts = threaded_reset_ts; diff --git a/plugins/python/uwsgi_python.h b/plugins/python/uwsgi_python.h index f1e9ae48..97123d8a 100644 --- a/plugins/python/uwsgi_python.h +++ b/plugins/python/uwsgi_python.h @@ -259,8 +259,10 @@ PyObject *uwsgi_pyimport_by_filename(char *, char *); void threaded_swap_ts(struct wsgi_request *, struct uwsgi_app *); void simple_swap_ts(struct wsgi_request *, struct uwsgi_app *); +void simple_threaded_swap_ts(struct wsgi_request *, struct uwsgi_app *); void threaded_reset_ts(struct wsgi_request *, struct uwsgi_app *); void simple_reset_ts(struct wsgi_request *, struct uwsgi_app *); +void simple_threaded_reset_ts(struct wsgi_request *, struct uwsgi_app *); int uwsgi_python_profiler_call(PyObject *, PyFrameObject *, int, PyObject *); diff --git a/plugins/python/wsgi_handlers.c b/plugins/python/wsgi_handlers.c index ce4fe62d..d19614f5 100644 --- a/plugins/python/wsgi_handlers.c +++ b/plugins/python/wsgi_handlers.c @@ -599,3 +599,23 @@ void simple_swap_ts(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { PyThreadState_Swap(wi->interpreter); } } + +void simple_threaded_reset_ts(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { + if (uwsgi.single_interpreter == 0 && wi->interpreter != up.main_thread) { + // restoring main interpreter + UWSGI_GET_GIL + PyThreadState_Swap(up.main_thread); + UWSGI_RELEASE_GIL + } +} + + +void simple_threaded_swap_ts(struct wsgi_request *wsgi_req, struct uwsgi_app *wi) { + + if (uwsgi.single_interpreter == 0 && wi->interpreter != up.main_thread) { + // set the interpreter + UWSGI_GET_GIL + PyThreadState_Swap(wi->interpreter); + UWSGI_RELEASE_GIL + } +}