[nginx] Copy filter: fixed sendfile aio handlers to set ctx->aio.

Maxim Dounin mdounin at mdounin.ru
Thu Mar 3 19:55:20 UTC 2016


details:   http://hg.nginx.org/nginx/rev/c5f81dcf97a7
branches:  
changeset: 6423:c5f81dcf97a7
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Mar 03 21:14:19 2016 +0300
description:
Copy filter: fixed sendfile aio handlers to set ctx->aio.

Sendfile handlers (aio preload and thread handler) are called within
ctx->output_filter() in ngx_output_chain(), and hence ctx->aio cannot
be set directly in ngx_output_chain().  Meanwhile, it must be set to
make sure loop within ngx_output_chain() will be properly terminated.

There are no known cases that trigger the problem, though in theory
something like aio + sub filter (something that needs body in memory,
and can also free some memory buffers) + sendfile can result in
"task already active" and "second aio post" alerts.

The fix is to set ctx->aio in ngx_http_copy_aio_sendfile_preload()
and ngx_http_copy_thread_handler().

For consistency, ctx->aio is no longer set explicitly in
ngx_output_chain_copy_buf(), as it's now done in
ngx_http_copy_thread_handler().

diffstat:

 src/core/ngx_output_chain.c            |   1 -
 src/http/ngx_http_copy_filter_module.c |  16 ++++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diffs (58 lines):

diff --git a/src/core/ngx_output_chain.c b/src/core/ngx_output_chain.c
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -581,7 +581,6 @@ ngx_output_chain_copy_buf(ngx_output_cha
             n = ngx_thread_read(&ctx->thread_task, src->file, dst->pos,
                                 (size_t) size, src->file_pos, ctx->pool);
             if (n == NGX_AGAIN) {
-                ctx->aio = 1;
                 return NGX_AGAIN;
             }
 
diff --git a/src/http/ngx_http_copy_filter_module.c b/src/http/ngx_http_copy_filter_module.c
--- a/src/http/ngx_http_copy_filter_module.c
+++ b/src/http/ngx_http_copy_filter_module.c
@@ -204,10 +204,11 @@ ngx_http_copy_aio_event_handler(ngx_even
 static ssize_t
 ngx_http_copy_aio_sendfile_preload(ngx_buf_t *file)
 {
-    ssize_t              n;
-    static u_char        buf[1];
-    ngx_event_aio_t     *aio;
-    ngx_http_request_t  *r;
+    ssize_t                  n;
+    static u_char            buf[1];
+    ngx_event_aio_t         *aio;
+    ngx_http_request_t      *r;
+    ngx_output_chain_ctx_t  *ctx;
 
     n = ngx_file_aio_read(file->file, buf, 1, file->file_pos, NULL);
 
@@ -218,6 +219,9 @@ ngx_http_copy_aio_sendfile_preload(ngx_b
         r = aio->data;
         r->main->blocked++;
         r->aio = 1;
+
+        ctx = ngx_http_get_module_ctx(r, ngx_http_copy_filter_module);
+        ctx->aio = 1;
     }
 
     return n;
@@ -252,6 +256,7 @@ ngx_http_copy_thread_handler(ngx_thread_
     ngx_str_t                  name;
     ngx_thread_pool_t         *tp;
     ngx_http_request_t        *r;
+    ngx_output_chain_ctx_t    *ctx;
     ngx_http_core_loc_conf_t  *clcf;
 
     r = file->thread_ctx;
@@ -285,6 +290,9 @@ ngx_http_copy_thread_handler(ngx_thread_
     r->main->blocked++;
     r->aio = 1;
 
+    ctx = ngx_http_get_module_ctx(r, ngx_http_copy_filter_module);
+    ctx->aio = 1;
+
     return NGX_OK;
 }
 



More information about the nginx-devel mailing list