[nginx] Fixed sendfile in threads (or with aio preload) and subrequests.

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


details:   http://hg.nginx.org/nginx/rev/768e287a6f36
branches:  
changeset: 6422:768e287a6f36
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Mar 03 21:14:12 2016 +0300
description:
Fixed sendfile in threads (or with aio preload) and subrequests.

If sendfile in threads is used, it is possible that multiple
subrequests will trigger multiple ngx_linux_sendfile_thread() calls,
as operations are only serialized in output chain based on r->aio,
that is, on subrequest level.

This resulted in "task #N already active" alerts, in particular, when
running proxy_store.t with "aio threads; sendfile on;".

Fix is to tolerate duplicate calls, with an additional safety check
that the file is the same as previously used.

The same problem also affects "aio on; sendfile on;" on FreeBSD
(previously known as "aio sendfile;"), where aio->preload_handler()
could be called multiple times due to similar reasons, resulting in
"second aio post" alerts.  Fix is the same as well.

It is also believed that similar problems can arise if a filter
calls the next body filter multiple times for some reason.  These are
mostly theoretical though.

diffstat:

 src/os/unix/ngx_freebsd_sendfile_chain.c |  13 +++++++++++++
 src/os/unix/ngx_linux_sendfile_chain.c   |  11 +++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

diffs (44 lines):

diff --git a/src/os/unix/ngx_freebsd_sendfile_chain.c b/src/os/unix/ngx_freebsd_sendfile_chain.c
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c
@@ -247,6 +247,19 @@ ngx_freebsd_sendfile_chain(ngx_connectio
 #if (NGX_HAVE_AIO_SENDFILE)
 
         if (ebusy) {
+            if (aio->event.active) {
+                /*
+                 * tolerate duplicate calls; they can happen due to subrequests
+                 * or multiple calls of the next body filter from a filter
+                 */
+
+                if (sent) {
+                    c->busy_count = 0;
+                }
+
+                return in;
+            }
+
             if (sent == 0) {
                 c->busy_count++;
 
diff --git a/src/os/unix/ngx_linux_sendfile_chain.c b/src/os/unix/ngx_linux_sendfile_chain.c
--- a/src/os/unix/ngx_linux_sendfile_chain.c
+++ b/src/os/unix/ngx_linux_sendfile_chain.c
@@ -354,6 +354,17 @@ ngx_linux_sendfile_thread(ngx_connection
         return (ctx->sent == ctx->size) ? NGX_DONE : NGX_AGAIN;
     }
 
+    if (task->event.active && ctx->file == file) {
+        /*
+         * tolerate duplicate calls; they can happen due to subrequests
+         * or multiple calls of the next body filter from a filter
+         */
+
+        *sent = 0;
+
+        return NGX_OK;
+    }
+
     ctx->file = file;
     ctx->socket = c->fd;
     ctx->size = size;



More information about the nginx-devel mailing list