[nginx] HTTP/2: fixed connection finalization.

Valentin Bartenev vbart at nginx.com
Wed Mar 29 17:24:30 UTC 2017


details:   http://hg.nginx.org/nginx/rev/83bae3d354ab
branches:  
changeset: 6957:83bae3d354ab
user:      Valentin Bartenev <vbart at nginx.com>
date:      Wed Mar 29 20:21:01 2017 +0300
description:
HTTP/2: fixed connection finalization.

All streams in connection must be finalized before the connection
itself can be finalized and all related memory is freed.  That's
not always possible on the current event loop iteration.

Thus when the last stream is finalized, it sets the special read
event handler ngx_http_v2_handle_connection_handler() and posts
the event.

Previously, this handler didn't check the connection state and
could call the regular event handler on a connection that was
already in finalization stage.  In the worst case that could
lead to a segmentation fault, since some data structures aren't
supposed to be used during connection finalization.  Particularly,
the waiting queue can contain already freed streams, so the
WINDOW_UPDATE frame received by that moment could trigger
accessing to these freed streams.

Now, the connection error flag is explicitly checked in
ngx_http_v2_handle_connection_handler().

diffstat:

 src/http/v2/ngx_http_v2.c |  11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)

diffs (28 lines):

diff -r 9b5f31fdb850 -r 83bae3d354ab src/http/v2/ngx_http_v2.c
--- a/src/http/v2/ngx_http_v2.c	Wed Mar 29 20:16:23 2017 +0300
+++ b/src/http/v2/ngx_http_v2.c	Wed Mar 29 20:21:01 2017 +0300
@@ -4134,6 +4134,14 @@ ngx_http_v2_handle_connection_handler(ng
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                    "http2 handle connection handler");
 
+    c = rev->data;
+    h2c = c->data;
+
+    if (c->error) {
+        ngx_http_v2_finalize_connection(h2c, 0);
+        return;
+    }
+
     rev->handler = ngx_http_v2_read_handler;
 
     if (rev->ready) {
@@ -4141,9 +4149,6 @@ ngx_http_v2_handle_connection_handler(ng
         return;
     }
 
-    c = rev->data;
-    h2c = c->data;
-
     if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
         ngx_http_v2_finalize_connection(h2c, 0);
         return;


More information about the nginx-devel mailing list