Mercurial > hg > nginx
comparison src/http/ngx_http_file_cache.c @ 9203:0de20f43db25
Fixed request termination with AIO and subrequests (ticket #2555).
When a request was terminated due to an error via ngx_http_terminate_request()
while an AIO operation was running in a subrequest, various issues were
observed. This happened because ngx_http_request_finalizer() was only set
in the subrequest where ngx_http_terminate_request() was called, but not
in the subrequest where the AIO operation was running. After completion
of the AIO operation normal processing of the subrequest was resumed, leading
to issues.
In particular, in case of the upstream module, termination of the request
called upstream cleanup, which closed the upstream connection. Attempts to
further work with the upstream connection after AIO operation completion
resulted in segfaults in ngx_ssl_recv(), "readv() failed (9: Bad file
descriptor) while reading upstream" errors, or socket leaks.
In ticket #2555, issues were observed with the following configuration
with cache background update (with thread writing instrumented to
introduce a delay, when a client closes the connection during an update):
location = /background-and-aio-write {
proxy_pass ...
proxy_cache one;
proxy_cache_valid 200 1s;
proxy_cache_background_update on;
proxy_cache_use_stale updating;
aio threads;
aio_write on;
limit_rate 1000;
}
Similarly, the same issue can be seen with SSI, and can be caused by
errors in subrequests, such as in the following configuration
(where "/proxy" uses AIO, and "/sleep" returns 444 after some delay,
causing request termination):
location = /ssi-active-boom {
ssi on;
ssi_types *;
return 200 '
<!--#include virtual="/proxy" -->
<!--#include virtual="/sleep" -->
';
limit_rate 1000;
}
Or the same with both AIO operation and the error in non-active subrequests
(which needs slightly different handling, see below):
location = /ssi-non-active-boom {
ssi on;
ssi_types *;
return 200 '
<!--#include virtual="/static" -->
<!--#include virtual="/proxy" -->
<!--#include virtual="/sleep" -->
';
limit_rate 1000;
}
Similarly, issues can be observed with just static files. However,
with static files potential impact is limited due to timeout safeguards
in ngx_http_writer(), and the fact that c->error is set during request
termination.
In a simple configuration with an AIO operation in the active subrequest,
such as in the following configuration, the connection is closed right
after completion of the AIO operation anyway, since ngx_http_writer()
tries to write to the connection and fails due to c->error set:
location = /ssi-active-static-boom {
ssi on;
ssi_types *;
return 200 '
<!--#include virtual="/static-aio" -->
<!--#include virtual="/sleep" -->
';
limit_rate 1000;
}
In the following configuration, with an AIO operation in a non-active
subrequest, the connection is closed only after send_timeout expires:
location = /ssi-non-active-static-boom {
ssi on;
ssi_types *;
return 200 '
<!--#include virtual="/static" -->
<!--#include virtual="/static-aio" -->
<!--#include virtual="/sleep" -->
';
limit_rate 1000;
}
Fix is to introduce r->main->terminated flag, which is to be checked
by AIO event handlers when the r->main->blocked counter is decremented.
When the flag is set, handlers are expected to wake up the connection
instead of the subrequest (which might be already cleaned up).
Additionally, now ngx_http_request_finalizer() is always set in the
active subrequest, so waking up the connection properly finalizes the
request even if termination happened in a non-active subrequest.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 30 Jan 2024 03:20:05 +0300 |
parents | e88cdaa0f1ff |
children |
comparison
equal
deleted
inserted
replaced
9202:e88cdaa0f1ff | 9203:0de20f43db25 |
---|---|
12 | 12 |
13 | 13 |
14 static ngx_int_t ngx_http_file_cache_lock(ngx_http_request_t *r, | 14 static ngx_int_t ngx_http_file_cache_lock(ngx_http_request_t *r, |
15 ngx_http_cache_t *c); | 15 ngx_http_cache_t *c); |
16 static void ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev); | 16 static void ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev); |
17 static void ngx_http_file_cache_lock_wait(ngx_http_request_t *r, | 17 static ngx_int_t ngx_http_file_cache_lock_wait(ngx_http_request_t *r, |
18 ngx_http_cache_t *c); | 18 ngx_http_cache_t *c); |
19 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r, | 19 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r, |
20 ngx_http_cache_t *c); | 20 ngx_http_cache_t *c); |
21 static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r, | 21 static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r, |
22 ngx_http_cache_t *c); | 22 ngx_http_cache_t *c); |
461 | 461 |
462 | 462 |
463 static void | 463 static void |
464 ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev) | 464 ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev) |
465 { | 465 { |
466 ngx_int_t rc; | |
466 ngx_connection_t *c; | 467 ngx_connection_t *c; |
467 ngx_http_request_t *r; | 468 ngx_http_request_t *r; |
468 | 469 |
469 r = ev->data; | 470 r = ev->data; |
470 c = r->connection; | 471 c = r->connection; |
472 ngx_http_set_log_request(c->log, r); | 473 ngx_http_set_log_request(c->log, r); |
473 | 474 |
474 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, | 475 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, |
475 "http file cache wait: \"%V?%V\"", &r->uri, &r->args); | 476 "http file cache wait: \"%V?%V\"", &r->uri, &r->args); |
476 | 477 |
477 ngx_http_file_cache_lock_wait(r, r->cache); | 478 rc = ngx_http_file_cache_lock_wait(r, r->cache); |
478 | 479 |
479 ngx_http_run_posted_requests(c); | 480 if (rc == NGX_AGAIN) { |
480 } | 481 return; |
481 | 482 } |
482 | 483 |
483 static void | 484 r->cache->waiting = 0; |
485 r->main->blocked--; | |
486 | |
487 if (r->main->terminated) { | |
488 /* | |
489 * trigger connection event handler if the request was | |
490 * terminated | |
491 */ | |
492 | |
493 c->write->handler(c->write); | |
494 | |
495 } else { | |
496 r->write_event_handler(r); | |
497 ngx_http_run_posted_requests(c); | |
498 } | |
499 } | |
500 | |
501 | |
502 static ngx_int_t | |
484 ngx_http_file_cache_lock_wait(ngx_http_request_t *r, ngx_http_cache_t *c) | 503 ngx_http_file_cache_lock_wait(ngx_http_request_t *r, ngx_http_cache_t *c) |
485 { | 504 { |
486 ngx_uint_t wait; | 505 ngx_uint_t wait; |
487 ngx_msec_t now, timer; | 506 ngx_msec_t now, timer; |
488 ngx_http_file_cache_t *cache; | 507 ngx_http_file_cache_t *cache; |
493 | 512 |
494 if ((ngx_msec_int_t) timer <= 0) { | 513 if ((ngx_msec_int_t) timer <= 0) { |
495 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, | 514 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
496 "cache lock timeout"); | 515 "cache lock timeout"); |
497 c->lock_timeout = 0; | 516 c->lock_timeout = 0; |
498 goto wakeup; | 517 return NGX_OK; |
499 } | 518 } |
500 | 519 |
501 cache = c->file_cache; | 520 cache = c->file_cache; |
502 wait = 0; | 521 wait = 0; |
503 | 522 |
511 | 530 |
512 ngx_shmtx_unlock(&cache->shpool->mutex); | 531 ngx_shmtx_unlock(&cache->shpool->mutex); |
513 | 532 |
514 if (wait) { | 533 if (wait) { |
515 ngx_add_timer(&c->wait_event, (timer > 500) ? 500 : timer); | 534 ngx_add_timer(&c->wait_event, (timer > 500) ? 500 : timer); |
516 return; | 535 return NGX_AGAIN; |
517 } | 536 } |
518 | 537 |
519 wakeup: | 538 return NGX_OK; |
520 | |
521 c->waiting = 0; | |
522 r->main->blocked--; | |
523 r->write_event_handler(r); | |
524 } | 539 } |
525 | 540 |
526 | 541 |
527 static ngx_int_t | 542 static ngx_int_t |
528 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c) | 543 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c) |
751 } | 766 } |
752 | 767 |
753 r->main->blocked--; | 768 r->main->blocked--; |
754 r->aio = 0; | 769 r->aio = 0; |
755 | 770 |
756 r->write_event_handler(r); | 771 if (r->main->terminated) { |
757 | 772 /* |
758 ngx_http_run_posted_requests(c); | 773 * trigger connection event handler if the request was |
774 * terminated | |
775 */ | |
776 | |
777 c->write->handler(c->write); | |
778 | |
779 } else { | |
780 r->write_event_handler(r); | |
781 ngx_http_run_posted_requests(c); | |
782 } | |
759 } | 783 } |
760 | 784 |
761 #endif | 785 #endif |
762 | 786 |
763 | 787 |
834 } | 858 } |
835 | 859 |
836 r->main->blocked--; | 860 r->main->blocked--; |
837 r->aio = 0; | 861 r->aio = 0; |
838 | 862 |
839 r->write_event_handler(r); | 863 if (r->main->terminated) { |
840 | 864 /* |
841 ngx_http_run_posted_requests(c); | 865 * trigger connection event handler if the request was |
866 * terminated | |
867 */ | |
868 | |
869 c->write->handler(c->write); | |
870 | |
871 } else { | |
872 r->write_event_handler(r); | |
873 ngx_http_run_posted_requests(c); | |
874 } | |
842 } | 875 } |
843 | 876 |
844 #endif | 877 #endif |
845 | 878 |
846 | 879 |