diff options
author | Max Romanov <max.romanov@nginx.com> | 2017-09-15 20:30:29 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2017-09-15 20:30:29 +0300 |
commit | 1449e27cb42a5b3ca2d2106a39fee3f1d54a340e (patch) | |
tree | a44a1ef5cb5cbf33424ab8b15085331a1d80b3ae /src | |
parent | 0bec14878e99de280046e1d1b1a0195e5478c808 (diff) | |
download | unit-1449e27cb42a5b3ca2d2106a39fee3f1d54a340e.tar.gz unit-1449e27cb42a5b3ca2d2106a39fee3f1d54a340e.tar.bz2 |
Fixing memory leak of request parse context.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_application.c | 34 | ||||
-rw-r--r-- | src/nxt_application.h | 2 | ||||
-rw-r--r-- | src/nxt_router.c | 21 |
3 files changed, 38 insertions, 19 deletions
diff --git a/src/nxt_application.c b/src/nxt_application.c index d795c230..30b9d1fa 100644 --- a/src/nxt_application.c +++ b/src/nxt_application.c @@ -772,21 +772,39 @@ static nxt_http_fields_hash_entry_t nxt_app_request_fields[] = { }; -nxt_int_t -nxt_app_http_req_init(nxt_task_t *task, nxt_app_parse_ctx_t *ctx) +nxt_app_parse_ctx_t * +nxt_app_http_req_init(nxt_task_t *task) { - nxt_int_t rc; + nxt_mp_t *mp; + nxt_int_t rc; + nxt_app_parse_ctx_t *ctx; + + mp = nxt_mp_create(1024, 128, 256, 32); + if (nxt_slow_path(mp == NULL)) { + return NULL; + } + + ctx = nxt_mp_retain(mp, sizeof(nxt_app_parse_ctx_t)); + if (nxt_slow_path(ctx == NULL)) { + nxt_mp_destroy(mp); - ctx->mem_pool = nxt_mp_create(1024, 128, 256, 32); + return NULL; + } - rc = nxt_http_parse_request_init(&ctx->parser, ctx->mem_pool); + nxt_memzero(ctx, sizeof(nxt_app_parse_ctx_t)); + + ctx->mem_pool = mp; + + rc = nxt_http_parse_request_init(&ctx->parser, mp); if (nxt_slow_path(rc != NXT_OK)) { - return rc; + nxt_mp_release(mp, ctx); + + return NULL; } ctx->parser.fields_hash = nxt_app_request_fields_hash; - return NXT_OK; + return ctx; } @@ -879,7 +897,7 @@ nxt_app_http_req_body_read(nxt_task_t *task, nxt_app_parse_ctx_t *ctx, nxt_int_t nxt_app_http_req_done(nxt_task_t *task, nxt_app_parse_ctx_t *ctx) { - nxt_mp_destroy(ctx->mem_pool); + nxt_mp_release(ctx->mem_pool, ctx); return NXT_OK; } diff --git a/src/nxt_application.h b/src/nxt_application.h index 3f5c01b5..02e7edeb 100644 --- a/src/nxt_application.h +++ b/src/nxt_application.h @@ -123,7 +123,7 @@ struct nxt_app_parse_ctx_s { }; -nxt_int_t nxt_app_http_req_init(nxt_task_t *task, nxt_app_parse_ctx_t *ctx); +nxt_app_parse_ctx_t *nxt_app_http_req_init(nxt_task_t *task); nxt_int_t nxt_app_http_req_header_parse(nxt_task_t *task, nxt_app_parse_ctx_t *ctx, nxt_buf_t *buf); diff --git a/src/nxt_router.c b/src/nxt_router.c index 7357667d..34078511 100644 --- a/src/nxt_router.c +++ b/src/nxt_router.c @@ -2791,15 +2791,10 @@ nxt_router_conn_http_header_parse(nxt_task_t *task, void *obj, void *data) nxt_debug(task, "router conn http header parse"); if (ap == NULL) { - ap = nxt_mp_zalloc(c->mem_pool, sizeof(nxt_app_parse_ctx_t)); + ap = nxt_app_http_req_init(task); if (nxt_slow_path(ap == NULL)) { - nxt_router_conn_close(task, c, data); - return; - } - - ret = nxt_app_http_req_init(task, ap); - if (nxt_slow_path(ret != NXT_OK)) { - nxt_router_conn_close(task, c, data); + nxt_router_gen_error(task, c, 500, + "Failed to allocate parse context"); return; } @@ -3003,8 +2998,6 @@ nxt_router_process_http_request(nxt_task_t *task, nxt_conn_t *c, nxt_debug(task, "stream #%uD linked to conn %p at engine %p", rc->stream, c, engine); - c->socket.data = NULL; - ra = nxt_router_ra_create(task, rc); ra->ap = ap; @@ -3451,12 +3444,20 @@ nxt_router_conn_free(nxt_task_t *task, void *obj, void *data) { nxt_conn_t *c; nxt_req_conn_link_t *rc; + nxt_app_parse_ctx_t *ap; nxt_socket_conf_joint_t *joint; c = obj; + ap = data; nxt_debug(task, "router conn close done"); + if (ap != NULL) { + nxt_app_http_req_done(task, ap); + + c->socket.data = NULL; + } + nxt_queue_each(rc, &c->requests, nxt_req_conn_link_t, link) { nxt_debug(task, "conn %p close, stream #%uD", c, rc->stream); |