From 93b1d621d7fac629ddf8047f0e59f299710f5bc2 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Wed, 5 Jun 2019 10:27:20 +0200 Subject: [PATCH 1/2] Remove memleak from Python httpclient. We grab a reference to the pyhttp_client for the client_op data structure but never removed it. This caused the pyhttp_client object to never be released when out of scope. --- include/kore/python_methods.h | 1 + src/python.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h index df993f0..7df75e0 100644 --- a/include/kore/python_methods.h +++ b/include/kore/python_methods.h @@ -675,6 +675,7 @@ struct pyhttp_client_op { int headers; struct kore_curl curl; struct python_coro *coro; + struct pyhttp_client *client; }; static PyObject *pyhttp_client_op_await(PyObject *); diff --git a/src/python.c b/src/python.c index 485f115..4ed0b7d 100644 --- a/src/python.c +++ b/src/python.c @@ -4228,6 +4228,7 @@ pyhttp_client_request(struct pyhttp_client *client, int m, PyObject *kwargs) op->state = PYHTTP_CLIENT_OP_RUN; Py_INCREF(client); + op->client = client; kore_curl_http_setup(&op->curl, m, ptr, length); kore_curl_bind_callback(&op->curl, python_curl_callback, op); @@ -4302,6 +4303,7 @@ pyhttp_client_request(struct pyhttp_client *client, int m, PyObject *kwargs) static void pyhttp_client_op_dealloc(struct pyhttp_client_op *op) { + Py_DECREF(op->client); kore_curl_cleanup(&op->curl); PyObject_Del((PyObject *)op); } From 89e58fa474c9a1ef33f8ea904f5d1029df942f1e Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Wed, 5 Jun 2019 10:35:47 +0200 Subject: [PATCH 2/2] Improve iterator support for Python req.response(). If the connection on which we are about to send the response was marked as disconnecting, do not go ahead and hook into the disconnect callback (it will never be called, it is already disconnecting). Instead just return, the connection will be removed anyway. --- src/python.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/python.c b/src/python.c index 4ed0b7d..49c2245 100644 --- a/src/python.c +++ b/src/python.c @@ -3159,11 +3159,14 @@ pyhttp_response(struct pyhttp_request *pyreq, PyObject *args) } else if (obj == Py_None) { http_response(pyreq->req, status, NULL, 0); } else { + c = pyreq->req->owner; + if (c->state == CONN_STATE_DISCONNECTING) { + Py_RETURN_FALSE; + } + if ((iterator = PyObject_GetIter(obj)) == NULL) return (NULL); - c = pyreq->req->owner; - iterobj = kore_pool_get(&iterobj_pool); iterobj->iterator = iterator; iterobj->connection = c;