nginx-0.0.1-2003-10-30-11:51:06 import
diff --git a/src/os/unix/ngx_aio_read.c b/src/os/unix/ngx_aio_read.c
index cb498eb..28deac6 100644
--- a/src/os/unix/ngx_aio_read.c
+++ b/src/os/unix/ngx_aio_read.c
@@ -24,12 +24,15 @@
 
     rev = c->read;
 
-    if (rev->active) {
+    if (!rev->ready) {
         ngx_log_error(NGX_LOG_ALERT, rev->log, 0, "SECOND AIO POST");
         return NGX_AGAIN;
     }
 
-    if (!rev->aio_complete) {
+    ngx_log_debug(rev->log, "rev->complete: %d" _ rev->complete);
+    ngx_log_debug(rev->log, "aio size: %d" _ size);
+
+    if (!rev->complete) {
         ngx_memzero(&rev->aiocb, sizeof(struct aiocb));
 
         rev->aiocb.aio_fildes = c->fd;
@@ -49,12 +52,13 @@
             return NGX_ERROR;
         }
 
-        ngx_log_debug(rev->log, "aio_read: OK");
+        ngx_log_debug(rev->log, "aio_read: #%d OK" _ c->fd);
 
         rev->active = 1;
+        rev->ready = 0;
     }
 
-    rev->aio_complete = 0;
+    rev->complete = 0;
 
     n = aio_error(&rev->aiocb);
     if (n == -1) {
@@ -65,15 +69,17 @@
 
     if (n != 0) {
         if (n == NGX_EINPROGRESS) {
-            if (!rev->active) {
+            if (rev->ready) {
                 ngx_log_error(NGX_LOG_ALERT, rev->log, n,
                               "aio_read() still in progress");
+                rev->ready = 0;
             }
             return NGX_AGAIN;
         }
 
         ngx_log_error(NGX_LOG_CRIT, rev->log, n, "aio_read() failed");
         rev->error = 1;
+        rev->ready = 0;
         return NGX_ERROR;
     }
 
@@ -83,16 +89,20 @@
                       "aio_return() failed");
 
         rev->error = 1;
+        rev->ready = 0;
         return NGX_ERROR;
     }
 
-    rev->active = 0;
-
-    ngx_log_debug(rev->log, "aio_read: %d" _ n);
+    ngx_log_debug(rev->log, "aio_read: #%d %d" _ c->fd _ n);
 
     if (n == 0) {
         rev->eof = 1;
+        rev->ready = 0;
+    } else {
+        rev->ready = 1;
     }
 
+    rev->active = 0;
+
     return n;
 }
diff --git a/src/os/unix/ngx_aio_read_chain.c b/src/os/unix/ngx_aio_read_chain.c
index 31f7c85..9af48dd 100644
--- a/src/os/unix/ngx_aio_read_chain.c
+++ b/src/os/unix/ngx_aio_read_chain.c
@@ -12,25 +12,30 @@
     size_t        size, total;
     ngx_err_t     err;
 
+    if (c->read->aio_eof) {
+        c->read->ready = 0;
+        return 0;
+    }
+
     total = 0;
 
     while (cl) {
 
         /* we can post the single aio operation only */
 
-        if (c->read->active) {
+        if (!c->read->ready) {
             return total ? total : NGX_AGAIN;
         }
 
-        buf = cl->hunk->pos;
-        prev = buf;
+        buf = cl->hunk->last;
+        prev = cl->hunk->last;
         size = 0;
 
         /* coalesce the neighbouring hunks */
 
-        while (cl && prev == cl->hunk->pos) {
-            size += cl->hunk->last - cl->hunk->pos;
-            prev = cl->hunk->last;
+        while (cl && prev == cl->hunk->last) {
+            size += cl->hunk->end - cl->hunk->last;
+            prev = cl->hunk->end;
             cl = cl->next;
         }
 
@@ -46,6 +51,15 @@
             return NGX_ERROR;
         }
 
+        if (n == 0) {
+            c->read->aio_eof = 1;
+            if (total) {
+                c->read->eof = 0;
+                c->read->ready = 1;
+            }
+            return total;
+        }
+
         if (n > 0) {
             total += n;
         }
diff --git a/src/os/unix/ngx_aio_write.c b/src/os/unix/ngx_aio_write.c
index dcdecc5..dc2c876 100644
--- a/src/os/unix/ngx_aio_write.c
+++ b/src/os/unix/ngx_aio_write.c
@@ -24,13 +24,13 @@
 
     wev = c->write;
 
-    if (wev->active) {
+    if (!wev->ready) {
         return NGX_AGAIN;
     }
 
-ngx_log_debug(wev->log, "aio: wev->aio_complete: %d" _ wev->aio_complete);
+ngx_log_debug(wev->log, "aio: wev->complete: %d" _ wev->complete);
 
-    if (!wev->aio_complete) {
+    if (!wev->complete) {
         ngx_memzero(&wev->aiocb, sizeof(struct aiocb));
 
         wev->aiocb.aio_fildes = c->fd;
@@ -52,9 +52,10 @@
         ngx_log_debug(wev->log, "aio_write: OK");
 
         wev->active = 1;
+        wev->ready = 0;
     }
 
-    wev->aio_complete = 0;
+    wev->complete = 0;
 
     n = aio_error(&wev->aiocb);
     if (n == -1) {
@@ -65,15 +66,28 @@
 
     if (n != 0) {
         if (n == NGX_EINPROGRESS) {
-            if (!wev->active) {
+            if (wev->ready) {
                 ngx_log_error(NGX_LOG_ALERT, wev->log, n,
                               "aio_write() still in progress");
+                wev->ready = 0;
             }
             return NGX_AGAIN;
         }
 
         ngx_log_error(NGX_LOG_CRIT, wev->log, n, "aio_write() failed");
         wev->error = 1;
+        wev->ready = 0;
+
+#if 1
+        n = aio_return(&wev->aiocb);
+        if (n == -1) {
+            ngx_log_error(NGX_LOG_ALERT, wev->log, ngx_errno,
+                          "aio_return() failed");
+        }
+
+        ngx_log_error(NGX_LOG_CRIT, wev->log, n, "aio_return() %d", n);
+#endif
+
         return NGX_ERROR;
     }
 
@@ -83,16 +97,15 @@
                       "aio_return() failed");
 
         wev->error = 1;
+        wev->ready = 0;
         return NGX_ERROR;
     }
 
-    wev->active = 0;
 
     ngx_log_debug(wev->log, "aio_write: %d" _ n);
 
-    if (n == 0) {
-        wev->eof = 1;
-    }
+    wev->active = 0;
+    wev->ready = 1;
 
     return n;
 }
diff --git a/src/os/unix/ngx_aio_write_chain.c b/src/os/unix/ngx_aio_write_chain.c
index ba24d80..7315044 100644
--- a/src/os/unix/ngx_aio_write_chain.c
+++ b/src/os/unix/ngx_aio_write_chain.c
@@ -26,7 +26,7 @@
 
         /* we can post the single aio operation only */
 
-        if (c->write->active) {
+        if (!c->write->ready) {
             return cl;
         }