nginx-0.0.1-2003-10-30-11:51:06 import
diff --git a/src/event/modules/ngx_kqueue_module.c b/src/event/modules/ngx_kqueue_module.c
index 6f6ed62..26e109b 100644
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -437,8 +437,8 @@
             break;
 
         case EVFILT_AIO:
-            ev->aio_complete = 1;
-            ev->active = 0;
+            ev->complete = 1;
+            ev->ready = 1;
 
             ev->event_handler(ev);
 
diff --git a/src/event/ngx_event.h b/src/event/ngx_event.h
index 1e5a63a..629e83d 100644
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -61,18 +61,15 @@
 
     /*
      * the event was passed or would be passed to a kernel;
-     * aio mode: 1 - the posted aio operation,
-     *           0 - the complete aio operation or no aio operation.
+     * in aio mode - operation was posted.
      */
     unsigned         active:1;
 
-    /*
-     * the ready event;
-     * in aio mode "ready" is always set - it makes things simple
-     * to learn whether the aio operation complete use aio_complete flag
-     */
+    /* the ready event; in aio mode 0 means that no operation can be posted */
     unsigned         ready:1;
-    unsigned         aio_complete:1;
+
+    /* aio operation is complete */
+    unsigned         complete:1;
 
     unsigned         eof:1;
     unsigned         error:1;
@@ -89,12 +86,20 @@
 
     unsigned         deferred_accept:1;
 
+    /* TODO: aio_eof and kq_eof can be the single pending_eof */
+    /* the pending eof in aio chain operation */
+    unsigned         aio_eof:1;
+
+    /* the pending eof reported by kqueue */
+    unsigned         kq_eof:1;
+
 #if (WIN32)
+    /* setsockopt(SO_UPDATE_ACCEPT_CONTEXT) was succesfull */
     unsigned         accept_context_updated:1;
 #endif
 
 #if (HAVE_KQUEUE)
-    unsigned         kq_eof:1;
+    /* the pending errno reported by kqueue */
     int              kq_errno;
 #endif
 
diff --git a/src/event/ngx_event_acceptex.c b/src/event/ngx_event_acceptex.c
index 8fd978e..c737434 100644
--- a/src/event/ngx_event_acceptex.c
+++ b/src/event/ngx_event_acceptex.c
@@ -120,6 +120,9 @@
         wev->write = 1;
         rev->event_handler = ngx_event_acceptex;
 
+        rev->ready = 1;
+        wev->ready = 1;
+
         ngx_test_null(c->pool,
                       ngx_create_pool(ls->pool_size, ls->log),
                       NGX_ERROR);
@@ -127,8 +130,7 @@
         ngx_test_null(c->buffer,
                       ngx_create_temp_hunk(c->pool,
                                            ls->post_accept_buffer_size
-                                           + 2 * (c->listening->socklen + 16),
-                                           0, 0),
+                                           + 2 * (c->listening->socklen + 16)),
                       NGX_ERROR);
 
         ngx_test_null(c->local_sockaddr, ngx_palloc(c->pool, ls->socklen),
diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c
index d47c542..c697c1b 100644
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -204,12 +204,13 @@
 
     if (ngx_event_flags & NGX_USE_AIO_EVENT) {
         /* aio, iocp */
-        rev->ready = 1;
  
 #if 1
         /* TODO: NGX_EINPROGRESS */
 
+        rev->ready = 1;
         wev->ready = 1;
+
         return NGX_OK;
 #endif
     }
diff --git a/src/event/ngx_event_pipe.c b/src/event/ngx_event_pipe.c
index d9f9abc..387d8b8 100644
--- a/src/event/ngx_event_pipe.c
+++ b/src/event/ngx_event_pipe.c
@@ -101,71 +101,52 @@
 
         } else {
 
-#if (HAVE_KQUEUE)
-
             /*
              * kqueue notifies about the end of file or a pending error.
              * This test allows not to allocate a hunk on these conditions
              * and not to call ngx_recv_chain().
              */
 
-            if (ngx_event_flags == NGX_HAVE_KQUEUE_EVENT) {
+            if (p->upstream->read->available == 0
+                && (p->upstream->read->kq_eof || p->upstream->read->aio_eof))
+            {
+                p->upstream->read->ready = 0;
+                p->upstream->read->eof = 0;
+                p->upstream_eof = 1;
+                p->read = 1;
 
-                if (p->upstream->read->available == 0) {
-                    if (p->upstream->read->kq_eof) {
-                        p->upstream->read->ready = 0;
-                        p->upstream->read->eof = 0;
-                        p->upstream_eof = 1;
-                        p->read = 1;
-
-                        if (p->upstream->read->kq_errno) {
-                            p->upstream->read->error = 1;
-                            p->upstream_error = 1;
-                            p->upstream_eof = 0;
-
-                            ngx_log_error(NGX_LOG_ERR, p->log,
-                                          p->upstream->read->kq_errno,
-                                          "readv() failed");
-                        }
-
-                        break;
-                    }
-                }
-
-#if 0
+#if (HAVE_KQUEUE)
                 if (p->upstream->read->kq_errno) {
+                    p->upstream->read->error = 1;
+                    p->upstream_error = 1;
+                    p->upstream_eof = 0;
+
                     ngx_log_error(NGX_LOG_ERR, p->log,
                                   p->upstream->read->kq_errno,
                                   "readv() failed");
-                    p->upstream_error = 1;
-
-                    break;
-
-                } else if (p->upstream->read->kq_eof
-                           && p->upstream->read->available == 0) {
-                    p->upstream_eof = 1;
-                    p->read = 1;
-
-                    break;
                 }
 #endif
 
+                break;
             }
-#endif
 
             if (p->free_raw_hunks) {
 
                 /* use the free hunks if they exist */
 
                 chain = p->free_raw_hunks;
-                p->free_raw_hunks = NULL;
+                if (p->single_buf) {
+                    p->free_raw_hunks = p->free_raw_hunks->next;
+                    chain->next = NULL;
+                } else {
+                    p->free_raw_hunks = NULL;
+                }
 
             } else if (p->hunks < p->bufs.num) {
 
                 /* allocate a new hunk if it's still allowed */
 
-                ngx_test_null(h, ngx_create_temp_hunk(p->pool,
-                                                      p->bufs.size, 0, 0),
+                ngx_test_null(h, ngx_create_temp_hunk(p->pool, p->bufs.size),
                               NGX_ABORT);
                 p->hunks++;
 
@@ -214,7 +195,12 @@
                 }
 
                 chain = p->free_raw_hunks;
-                p->free_raw_hunks = NULL;
+                if (p->single_buf) {
+                    p->free_raw_hunks = p->free_raw_hunks->next;
+                    chain->next = NULL;
+                } else {
+                    p->free_raw_hunks = NULL;
+                }
 
             } else {
 
@@ -229,6 +215,9 @@
 
             ngx_log_debug(p->log, "recv_chain: %d" _ n);
 
+            if (p->free_raw_hunks) {
+                chain->next = p->free_raw_hunks;
+            }
             p->free_raw_hunks = chain;
 
             if (n == NGX_ERROR) {
@@ -237,6 +226,10 @@
             }
 
             if (n == NGX_AGAIN) {
+                if (p->single_buf) {
+                    ngx_event_pipe_remove_shadow_links(chain->hunk);
+                }
+
                 break;
             }
 
@@ -283,8 +276,11 @@
             return NGX_ABORT;
         }
 
-        /* TODO: p->free_raw_hunk->next can be free()ed */
         p->free_raw_hunks = p->free_raw_hunks->next;
+
+        for (cl = p->free_raw_hunks; cl; cl = cl->next) {
+            ngx_pfree(p->pool, cl->hunk->start); 
+        }
     }
 
     if (p->cachable && p->in) {
diff --git a/src/event/ngx_event_pipe.h b/src/event/ngx_event_pipe.h
index f59b60e..fc68cc6 100644
--- a/src/event/ngx_event_pipe.h
+++ b/src/event/ngx_event_pipe.h
@@ -38,6 +38,7 @@
 
     unsigned           read:1;
     unsigned           cachable:1;
+    unsigned           single_buf:1;
     unsigned           upstream_done:1;
     unsigned           upstream_error:1;
     unsigned           upstream_eof:1;