comparison src/http/ngx_http_upstream.c @ 9204:631ee3c6d38c

Upstream: fixed usage of closed sockets with filter finalization. When filter finalization is triggered when working with an upstream server, and error_page redirects request processing to some simple handler, ngx_http_request_finalize() triggers request termination when the response is sent. In particular, via the upstream cleanup handler, nginx will close the upstream connection and the corresponding socket. Still, this can happen to be with ngx_event_pipe() on stack. While the code will set p->downstream_error due to NGX_ERROR returned from the output filter chain by filter finalization, otherwise the error will be ignored till control returns to ngx_http_upstream_process_request(). And event pipe might try reading from the (already closed) socket, resulting in "readv() failed (9: Bad file descriptor) while reading upstream" errors (or even segfaults with SSL). Such errors were seen with the following configuration: location /t2 { proxy_pass http://127.0.0.1:8080/big; image_filter_buffer 10m; image_filter resize 150 100; error_page 415 = /empty; } location /empty { return 204; } location /big { # big enough static file } Fix is to clear p->upstream in ngx_http_upstream_finalize_request(), and ensure that p->upstream is checked in ngx_event_pipe_read_upstream() and when handling events at ngx_event_pipe() exit.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 30 Jan 2024 03:20:10 +0300
parents 0de20f43db25
children 6765e5f6d991
comparison
equal deleted inserted replaced
9203:0de20f43db25 9204:631ee3c6d38c
4572 ngx_close_connection(u->peer.connection); 4572 ngx_close_connection(u->peer.connection);
4573 } 4573 }
4574 4574
4575 u->peer.connection = NULL; 4575 u->peer.connection = NULL;
4576 4576
4577 if (u->pipe) {
4578 u->pipe->upstream = NULL;
4579 }
4580
4577 if (u->pipe && u->pipe->temp_file) { 4581 if (u->pipe && u->pipe->temp_file) {
4578 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 4582 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
4579 "http upstream temp fd: %d", 4583 "http upstream temp fd: %d",
4580 u->pipe->temp_file->file.fd); 4584 u->pipe->temp_file->file.fd);
4581 } 4585 }