comparison src/event/ngx_event_pipe.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 bffcc5af1d72
children
comparison
equal deleted inserted replaced
9203:0de20f43db25 9204:631ee3c6d38c
55 } 55 }
56 56
57 do_write = 1; 57 do_write = 1;
58 } 58 }
59 59
60 if (p->upstream->fd != (ngx_socket_t) -1) { 60 if (p->upstream
61 && p->upstream->fd != (ngx_socket_t) -1)
62 {
61 rev = p->upstream->read; 63 rev = p->upstream->read;
62 64
63 flags = (rev->eof || rev->error) ? NGX_CLOSE_EVENT : 0; 65 flags = (rev->eof || rev->error) ? NGX_CLOSE_EVENT : 0;
64 66
65 if (ngx_handle_read_event(rev, flags) != NGX_OK) { 67 if (ngx_handle_read_event(rev, flags) != NGX_OK) {
106 ngx_int_t rc; 108 ngx_int_t rc;
107 ngx_buf_t *b; 109 ngx_buf_t *b;
108 ngx_msec_t delay; 110 ngx_msec_t delay;
109 ngx_chain_t *chain, *cl, *ln; 111 ngx_chain_t *chain, *cl, *ln;
110 112
111 if (p->upstream_eof || p->upstream_error || p->upstream_done) { 113 if (p->upstream_eof || p->upstream_error || p->upstream_done
114 || p->upstream == NULL)
115 {
112 return NGX_OK; 116 return NGX_OK;
113 } 117 }
114 118
115 #if (NGX_THREADS) 119 #if (NGX_THREADS)
116 120