comparison src/http/ngx_http_upstream.c @ 7041:6169dbad37d8

Upstream: fixed running posted requests (ticket #788). Previously, the upstream resolve handler always called ngx_http_run_posted_requests() to run posted requests after processing the resolver response. However, if the handler was called directly from the ngx_resolve_name() function (for example, if the resolver response was cached), running posted requests from the handler could lead to the following errors: - If the request was scheduled for termination, it could actually be terminated in the resolve handler. Upper stack frames could reference the freed request object in this case. - If a significant number of requests were posted, and for each of them the resolve handler was called directly from the ngx_resolve_name() function, posted requests could be run recursively and lead to stack overflow. Now ngx_http_run_posted_requests() is only called from asynchronously invoked resolve handlers.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 14 Jun 2017 20:13:41 +0300
parents 610a219b28f6
children bd2f97a3aecc
comparison
equal deleted inserted replaced
7040:d49b74a683b1 7041:6169dbad37d8
1141 1141
1142 1142
1143 static void 1143 static void
1144 ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx) 1144 ngx_http_upstream_resolve_handler(ngx_resolver_ctx_t *ctx)
1145 { 1145 {
1146 ngx_uint_t run_posted;
1146 ngx_connection_t *c; 1147 ngx_connection_t *c;
1147 ngx_http_request_t *r; 1148 ngx_http_request_t *r;
1148 ngx_http_upstream_t *u; 1149 ngx_http_upstream_t *u;
1149 ngx_http_upstream_resolved_t *ur; 1150 ngx_http_upstream_resolved_t *ur;
1151
1152 run_posted = ctx->async;
1150 1153
1151 r = ctx->data; 1154 r = ctx->data;
1152 c = r->connection; 1155 c = r->connection;
1153 1156
1154 u = r->upstream; 1157 u = r->upstream;
1209 1212
1210 ngx_http_upstream_connect(r, u); 1213 ngx_http_upstream_connect(r, u);
1211 1214
1212 failed: 1215 failed:
1213 1216
1214 ngx_http_run_posted_requests(c); 1217 if (run_posted) {
1218 ngx_http_run_posted_requests(c);
1219 }
1215 } 1220 }
1216 1221
1217 1222
1218 static void 1223 static void
1219 ngx_http_upstream_handler(ngx_event_t *ev) 1224 ngx_http_upstream_handler(ngx_event_t *ev)