diff options
author | Max Romanov <max.romanov@nginx.com> | 2020-11-18 22:33:53 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2020-11-18 22:33:53 +0300 |
commit | 6c3c83561a97b91f18a771e0c582c5ed4013a9a6 (patch) | |
tree | 32ebaf18f1ea9efdac9fc2b32eb5f7d49727d603 /src/python/nxt_python_asgi_http.c | |
parent | 300347a5cffa4187921384bdd02c5bb90875f9e5 (diff) | |
download | unit-6c3c83561a97b91f18a771e0c582c5ed4013a9a6.tar.gz unit-6c3c83561a97b91f18a771e0c582c5ed4013a9a6.tar.bz2 |
Libunit: closing active requests on quit.
Diffstat (limited to 'src/python/nxt_python_asgi_http.c')
-rw-r--r-- | src/python/nxt_python_asgi_http.c | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/python/nxt_python_asgi_http.c b/src/python/nxt_python_asgi_http.c index a5034ea6..5ea8e0a5 100644 --- a/src/python/nxt_python_asgi_http.c +++ b/src/python/nxt_python_asgi_http.c @@ -24,6 +24,7 @@ typedef struct { uint64_t content_length; uint64_t bytes_sent; int complete; + int closed; PyObject *send_body; Py_ssize_t send_body_off; } nxt_py_asgi_http_t; @@ -94,6 +95,7 @@ nxt_py_asgi_http_create(nxt_unit_request_info_t *req) http->content_length = -1; http->bytes_sent = 0; http->complete = 0; + http->closed = 0; http->send_body = NULL; http->send_body_off = 0; } @@ -115,7 +117,13 @@ nxt_py_asgi_http_receive(PyObject *self, PyObject *none) nxt_unit_req_debug(req, "asgi_http_receive"); - msg = nxt_py_asgi_http_read_msg(http); + if (nxt_slow_path(http->closed || nxt_unit_response_is_sent(req))) { + msg = nxt_py_asgi_new_msg(req, nxt_py_http_disconnect_str); + + } else { + msg = nxt_py_asgi_http_read_msg(http); + } + if (nxt_slow_path(msg == NULL)) { return NULL; } @@ -561,6 +569,48 @@ fail: } +void +nxt_py_asgi_http_close_handler(nxt_unit_request_info_t *req) +{ + PyObject *msg, *future, *res; + nxt_py_asgi_http_t *http; + + http = req->data; + + nxt_unit_req_debug(req, "asgi_http_close_handler"); + + http->closed = 1; + + if (http->receive_future == NULL) { + return; + } + + msg = nxt_py_asgi_new_msg(req, nxt_py_http_disconnect_str); + if (nxt_slow_path(msg == NULL)) { + return; + } + + if (msg == Py_None) { + Py_DECREF(msg); + return; + } + + future = http->receive_future; + http->receive_future = NULL; + + res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg, NULL); + if (nxt_slow_path(res == NULL)) { + nxt_unit_req_alert(req, "'set_result' call failed"); + nxt_python_print_exception(); + } + + Py_XDECREF(res); + Py_DECREF(future); + + Py_DECREF(msg); +} + + static PyObject * nxt_py_asgi_http_done(PyObject *self, PyObject *future) { |