Skip to content

Commit dfbdc1c

Browse files
committed
Python: fixing exceptions in Future.set_result for ASGI implementation.
An ASGI application can cancel the Future object returned by the receive() call. In this case, Unit's ASGI implementation should not call set_result() because the Future is already handled. In particular, the Starlette framework was noted to cancel the received Future. This patch adds a done() check for the Future before attempting a set_result(). This is related to #564 issue on GitHub.
1 parent 5675452 commit dfbdc1c

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

src/python/nxt_python_asgi_http.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static PyObject *nxt_py_asgi_http_response_start(nxt_py_asgi_http_t *http,
3939
static PyObject *nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http,
4040
PyObject *dict);
4141
static void nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http);
42+
static void nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http,
43+
PyObject *future, PyObject *msg);
4244
static PyObject *nxt_py_asgi_http_done(PyObject *self, PyObject *future);
4345

4446

@@ -465,7 +467,7 @@ nxt_py_asgi_http_response_body(nxt_py_asgi_http_t *http, PyObject *dict)
465467
static void
466468
nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http)
467469
{
468-
PyObject *msg, *future, *res;
470+
PyObject *msg, *future;
469471

470472
if (http->receive_future == NULL) {
471473
return;
@@ -484,23 +486,45 @@ nxt_py_asgi_http_emit_disconnect(nxt_py_asgi_http_t *http)
484486
future = http->receive_future;
485487
http->receive_future = NULL;
486488

487-
res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg, NULL);
489+
nxt_py_asgi_http_set_result(http, future, msg);
490+
491+
Py_DECREF(msg);
492+
}
493+
494+
495+
static void
496+
nxt_py_asgi_http_set_result(nxt_py_asgi_http_t *http, PyObject *future,
497+
PyObject *msg)
498+
{
499+
PyObject *res;
500+
501+
res = PyObject_CallMethodObjArgs(future, nxt_py_done_str, NULL);
488502
if (nxt_slow_path(res == NULL)) {
489-
nxt_unit_req_alert(http->req, "'set_result' call failed");
503+
nxt_unit_req_alert(http->req, "'done' call failed");
490504
nxt_python_print_exception();
491505
}
492506

507+
if (nxt_fast_path(res == Py_False)) {
508+
res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg,
509+
NULL);
510+
if (nxt_slow_path(res == NULL)) {
511+
nxt_unit_req_alert(http->req, "'set_result' call failed");
512+
nxt_python_print_exception();
513+
}
514+
515+
} else {
516+
res = NULL;
517+
}
518+
493519
Py_XDECREF(res);
494520
Py_DECREF(future);
495-
496-
Py_DECREF(msg);
497521
}
498522

499523

500524
void
501525
nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req)
502526
{
503-
PyObject *msg, *future, *res;
527+
PyObject *msg, *future;
504528
nxt_py_asgi_http_t *http;
505529

506530
http = req->data;
@@ -524,14 +548,7 @@ nxt_py_asgi_http_data_handler(nxt_unit_request_info_t *req)
524548
future = http->receive_future;
525549
http->receive_future = NULL;
526550

527-
res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, msg, NULL);
528-
if (nxt_slow_path(res == NULL)) {
529-
nxt_unit_req_alert(req, "'set_result' call failed");
530-
nxt_python_print_exception();
531-
}
532-
533-
Py_XDECREF(res);
534-
Py_DECREF(future);
551+
nxt_py_asgi_http_set_result(http, future, msg);
535552

536553
Py_DECREF(msg);
537554
}
@@ -575,15 +592,7 @@ nxt_py_asgi_http_drain(nxt_queue_link_t *lnk)
575592
future = http->send_future;
576593
http->send_future = NULL;
577594

578-
res = PyObject_CallMethodObjArgs(future, nxt_py_set_result_str, Py_None,
579-
NULL);
580-
if (nxt_slow_path(res == NULL)) {
581-
nxt_unit_req_alert(http->req, "'set_result' call failed");
582-
nxt_python_print_exception();
583-
}
584-
585-
Py_XDECREF(res);
586-
Py_DECREF(future);
595+
nxt_py_asgi_http_set_result(http, future, Py_None);
587596

588597
return NXT_UNIT_OK;
589598

0 commit comments

Comments
 (0)