From 5cdf838325b7199c978b9d9f722622fbb51eac62 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Tue, 17 Nov 2015 08:48:35 +0000 Subject: [PATCH] Fix error handling in async resolver PUBLISHED_FROM=1e8558ec08f8e72d70163d34e0920f80f74372d1 --- mongoose.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/mongoose.c b/mongoose.c index 39094767..22dc3b51 100644 --- a/mongoose.c +++ b/mongoose.c @@ -7796,8 +7796,6 @@ static void mg_resolve_async_eh(struct mg_connection *nc, int ev, void *data) { case MG_EV_CONNECT: case MG_EV_POLL: if (req->retries > req->max_retries) { - req->callback(NULL, req->data); - MG_FREE(req); nc->flags |= MG_F_CLOSE_IMMEDIATELY; break; } @@ -7812,13 +7810,28 @@ static void mg_resolve_async_eh(struct mg_connection *nc, int ev, void *data) { if (mg_parse_dns(nc->recv_mbuf.buf, *(int *) data, msg) == 0 && msg->num_answers > 0) { req->callback(msg, req->data); - } else { - req->callback(NULL, req->data); + nc->user_data = NULL; + MG_FREE(req); } - MG_FREE(req); MG_FREE(msg); nc->flags |= MG_F_CLOSE_IMMEDIATELY; break; + case MG_EV_SEND: + /* + * If a send error occurs, prevent closing of the connection by the core. + * We will retry after timeout. + */ + nc->flags &= ~MG_F_CLOSE_IMMEDIATELY; + mbuf_remove(&nc->send_mbuf, nc->send_mbuf.len); + break; + case MG_EV_CLOSE: + /* If we got here with request still not done, fire an error callback. */ + if (req != NULL) { + req->callback(NULL, req->data); + nc->user_data = NULL; + MG_FREE(req); + } + break; } }