Skip to content

Commit

Permalink
HTTP/2: fixed posted streams handling.
Browse files Browse the repository at this point in the history
A bug was introduced by 82efcedb310b that could lead to timing out of
responses or segmentation fault, when accept_mutex was enabled.

The output queue in HTTP/2 can contain frames from different streams.
When the queue is sent, all related write handlers need to be called.
In order to do so, the streams were added to the h2c->posted queue
after handling sent frames.  Then this queue was processed in
ngx_http_v2_write_handler().

If accept_mutex is enabled, the event's "ready" flag is set but its
handler is not called immediately.  Instead, the event is added to
the ngx_posted_events queue.  At the same time in this queue can be
events from upstream connections.  Such events can result in sending
output queue before ngx_http_v2_write_handler() is triggered.  And
at the time ngx_http_v2_write_handler() is called, the output queue
can be already empty with some streams added to h2c->posted.

But after 82efcedb310b, these streams weren't processed if all frames
have already been sent and the output queue was empty.  This might lead
to a situation when a number of streams were get stuck in h2c->posted
queue for a long time.  Eventually these streams might get closed by
the send timeout.

In the worst case this might also lead to a segmentation fault, if
already freed stream was left in the h2c->posted queue.  This could
happen if one of the streams was terminated but wasn't closed, due to
the HEADERS frame or a partially sent DATA frame left in the output
queue.  If this happened the ngx_http_v2_filter_cleanup() handler
removed the stream from the h2c->waiting or h2c->posted queue on
termination stage, before the frame has been sent, and the stream
was again added to the h2c->posted queue after the frame was sent.

In order to fix all these problems and simplify the code, write
events of fake stream connections are now added to ngx_posted_events
instead of using a custom h2c->posted queue.
  • Loading branch information
VBart committed Nov 28, 2016
1 parent 72ace36 commit 5d496d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
27 changes: 2 additions & 25 deletions src/http/v2/ngx_http_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ ngx_http_v2_init(ngx_event_t *rev)
: ngx_http_v2_state_preface;

ngx_queue_init(&h2c->waiting);
ngx_queue_init(&h2c->posted);
ngx_queue_init(&h2c->dependencies);
ngx_queue_init(&h2c->closed);

Expand Down Expand Up @@ -420,9 +419,7 @@ static void
ngx_http_v2_write_handler(ngx_event_t *wev)
{
ngx_int_t rc;
ngx_queue_t *q;
ngx_connection_t *c;
ngx_http_v2_stream_t *stream;
ngx_http_v2_connection_t *h2c;

c = wev->data;
Expand Down Expand Up @@ -457,26 +454,6 @@ ngx_http_v2_write_handler(ngx_event_t *wev)
return;
}

while (!ngx_queue_empty(&h2c->posted)) {
q = ngx_queue_head(&h2c->posted);

ngx_queue_remove(q);

stream = ngx_queue_data(q, ngx_http_v2_stream_t, queue);

stream->handled = 0;

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"run http2 stream %ui", stream->node->id);

wev = stream->request->connection->write;

wev->active = 0;
wev->ready = 1;

wev->handler(wev);
}

h2c->blocked = 0;

if (rc == NGX_AGAIN) {
Expand Down Expand Up @@ -2254,7 +2231,7 @@ ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos,

stream = ngx_queue_data(q, ngx_http_v2_stream_t, queue);

stream->handled = 0;
stream->waiting = 0;

wev = stream->request->connection->write;

Expand Down Expand Up @@ -4274,7 +4251,7 @@ ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
continue;
}

stream->handled = 0;
stream->waiting = 0;

r = stream->request;
fc = r->connection;
Expand Down
3 changes: 1 addition & 2 deletions src/http/v2/ngx_http_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ struct ngx_http_v2_connection_s {

ngx_http_v2_out_frame_t *last_out;

ngx_queue_t posted;
ngx_queue_t dependencies;
ngx_queue_t closed;

Expand Down Expand Up @@ -192,7 +191,7 @@ struct ngx_http_v2_stream_s {

ngx_pool_t *pool;

unsigned handled:1;
unsigned waiting:1;
unsigned blocked:1;
unsigned exhausted:1;
unsigned in_closed:1;
Expand Down
50 changes: 39 additions & 11 deletions src/http/v2/ngx_http_v2_filter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,11 +1104,11 @@ ngx_http_v2_waiting_queue(ngx_http_v2_connection_t *h2c,
ngx_queue_t *q;
ngx_http_v2_stream_t *s;

if (stream->handled) {
if (stream->waiting) {
return;
}

stream->handled = 1;
stream->waiting = 1;

for (q = ngx_queue_last(&h2c->waiting);
q != ngx_queue_sentinel(&h2c->waiting);
Expand Down Expand Up @@ -1298,20 +1298,29 @@ static ngx_inline void
ngx_http_v2_handle_stream(ngx_http_v2_connection_t *h2c,
ngx_http_v2_stream_t *stream)
{
ngx_event_t *wev;
ngx_connection_t *fc;

if (stream->handled || stream->blocked) {
if (stream->waiting || stream->blocked) {
return;
}

fc = stream->request->connection;

if (!fc->error && (stream->exhausted || fc->write->delayed)) {
if (!fc->error && stream->exhausted) {
return;
}

stream->handled = 1;
ngx_queue_insert_tail(&h2c->posted, &stream->queue);
wev = fc->write;

wev->active = 0;
wev->ready = 1;

if (!fc->error && wev->delayed) {
return;
}

ngx_post_event(wev, &ngx_posted_events);
}


Expand All @@ -1321,11 +1330,13 @@ ngx_http_v2_filter_cleanup(void *data)
ngx_http_v2_stream_t *stream = data;

size_t window;
ngx_event_t *wev;
ngx_queue_t *q;
ngx_http_v2_out_frame_t *frame, **fn;
ngx_http_v2_connection_t *h2c;

if (stream->handled) {
stream->handled = 0;
if (stream->waiting) {
stream->waiting = 0;
ngx_queue_remove(&stream->queue);
}

Expand Down Expand Up @@ -1359,9 +1370,26 @@ ngx_http_v2_filter_cleanup(void *data)
fn = &frame->next;
}

if (h2c->send_window == 0 && window && !ngx_queue_empty(&h2c->waiting)) {
ngx_queue_add(&h2c->posted, &h2c->waiting);
ngx_queue_init(&h2c->waiting);
if (h2c->send_window == 0 && window) {

while (!ngx_queue_empty(&h2c->waiting)) {
q = ngx_queue_head(&h2c->waiting);

ngx_queue_remove(q);

stream = ngx_queue_data(q, ngx_http_v2_stream_t, queue);

stream->waiting = 0;

wev = stream->request->connection->write;

wev->active = 0;
wev->ready = 1;

if (!wev->delayed) {
ngx_post_event(wev, &ngx_posted_events);
}
}
}

h2c->send_window += window;
Expand Down

0 comments on commit 5d496d4

Please sign in to comment.