Python: safety checks for request processing context.
An application can store request related functions and mistakenly call them
outside of request processing.  Previously this resulted in segmentation
fault due to unset nxt_python_run_ctx.  Now an exception will be raised.
@@ -773,6 +773,14 @@ nxt_py_start_resp(PyObject *self, PyObject *args)
static const u_char cr_lf[] = "\r\n";
static const u_char sc_sp[] = ": ";
+ ctx = nxt_python_run_ctx;
+
+ if (nxt_slow_path(ctx == NULL)) {
+ return PyErr_Format(PyExc_RuntimeError,
+ "start_response() is called "
+ "outside of WSGI request processing");
+ }
+
n = PyTuple_GET_SIZE(args);
if (n < 2 || n > 3) {
@@ -781,8 +789,6 @@ nxt_py_start_resp(PyObject *self, PyObject *args)
string = PyTuple_GET_ITEM(args, 0);
- ctx = nxt_python_run_ctx;
-
nxt_python_write(ctx, status, sizeof(status) - 1, 0, 0);
rc = nxt_python_write_py_str(ctx, string, 0, 0);
@@ -862,6 +868,12 @@ nxt_py_input_read(nxt_py_input_t *self, PyObject *args)
ctx = nxt_python_run_ctx;
+ if (nxt_slow_path(ctx == NULL)) {
+ return PyErr_Format(PyExc_RuntimeError,
+ "wsgi.input.read() is called "
+ "outside of WSGI request processing");
+ }
+
size = ctx->body_preread_size;
n = PyTuple_GET_SIZE(args);
