[nginx] Fixed background requests with asynchronous operations.

Eran Kornblau eran.kornblau at kaltura.com
Mon Jul 24 10:08:20 UTC 2017

Hi Roman,

I got a GitHub issue opened few days ago, that my module doesn't work following this commit -

The situation is as follows -
1. My module's content phase handler runs and performs an async read request -
	a. It calls ngx_file_aio_read which returns NGX_AGAIN
	b. Increments r->blocked
	c. Sets r->aio to 1
	(code here - https://github.com/kaltura/nginx-vod-module/blob/master/ngx_file_reader.c#L392)
2. The NGX_AGAIN status propagates up to ngx_http_core_content_phase which calls ngx_http_finalize_request
	with this status.
3. r->buffered, c->buffered, and r->postponed are all false, so this if:
	is skipped (it used to enter before the patch since r->blocked is 1)
	In addition, r == c->data, so at the end, ngx_http_finalize_request calls ngx_http_finalize_connection
4. Since r->main->count == 1 and r->keepalive == 1, this function calls ngx_http_set_keepalive
5. ngx_http_set_keepalive calls ngx_http_free_request which destroys the pool even though it has aio pending.

Please let me know if you think I'm doing something wrong here (I can, for example, increment r->main->count,
but this is not done in any of the other places that do blocked++)

Thank you!


-----Original Message-----
From: nginx-devel [mailto:nginx-devel-bounces at nginx.org] On Behalf Of Roman Arutyunyan
Sent: Monday, May 29, 2017 11:40 PM
To: nginx-devel at nginx.org
Subject: [nginx] Fixed background requests with asynchronous operations.

details:   http://hg.nginx.org/nginx/rev/5e05118678af
changeset: 7011:5e05118678af
user:      Roman Arutyunyan <arut at nginx.com>
date:      Mon May 29 23:33:38 2017 +0300
Fixed background requests with asynchronous operations.

If the main request was finalized while a background request performed an asynchronous operation, the main request ended up in ngx_http_writer() and was not finalized until a network event or a timeout.  For example, cache background update with aio enabled made nginx unable to process further client requests or close the connection, keeping it open until client closes it.

Now regular finalization of the main request is not suspended because of an asynchronous operation in another request.

If a background request was terminated while an asynchronous operation was in progress, background request's write event handler was changed to
ngx_http_request_finalizer() and never called again.

Now, whenever a request is terminated while an asynchronous operation is in progress, connection error flag is set to make further finalizations of any request with this connection lead to termination.

These issues appeared in 1aeaae6e9446 (not yet released).


 src/http/ngx_http_request.c |  8 +++-----
 1 files changed, 3 insertions(+), 5 deletions(-)

diffs (32 lines):

diff -r c1524829af3d -r 5e05118678af src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c	Mon May 29 16:48:30 2017 +0300
+++ b/src/http/ngx_http_request.c	Mon May 29 23:33:38 2017 +0300
@@ -2331,10 +2331,6 @@ ngx_http_finalize_request(ngx_http_reque
-        if (r->main->blocked) {
-            r->write_event_handler = ngx_http_request_finalizer;
-        }
         ngx_http_terminate_request(r, rc);
@@ -2449,7 +2445,7 @@ ngx_http_finalize_request(ngx_http_reque
-    if (r->buffered || c->buffered || r->postponed || r->blocked) {
+    if (r->buffered || c->buffered || r->postponed) {
         if (ngx_http_set_write_handler(r) != NGX_OK) {
             ngx_http_terminate_request(r, 0); @@ -2530,6 +2526,8 @@ ngx_http_terminate_request(ngx_http_requ
     if (mr->write_event_handler) {
         if (mr->blocked) {
+            r->connection->error = 1;
+            r->write_event_handler = ngx_http_request_finalizer;
nginx-devel mailing list
nginx-devel at nginx.org

More information about the nginx-devel mailing list