Skip to content

Commit 8bc930c

Browse files
committed
http2: fix endless loop when writing empty string
PR-URL: #18924 Fixes: #18169 Refs: #18673 Refs: https://github.com/nodejs/node/blob/v9.5.0/src/node_http2.cc#L1481-L1484 Refs: https://github.com/nodejs/node/blob/v9.5.0/lib/_http_outgoing.js#L659-L661 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent aa0fca9 commit 8bc930c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/node_http2.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -2192,6 +2192,17 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle,
21922192

21932193
size_t amount = 0; // amount of data being sent in this data frame.
21942194

2195+
// Remove all empty chunks from the head of the queue.
2196+
// This is done here so that .write('', cb) is still a meaningful way to
2197+
// find out when the HTTP2 stream wants to consume data, and because the
2198+
// StreamBase API allows empty input chunks.
2199+
while (!stream->queue_.empty() && stream->queue_.front().buf.len == 0) {
2200+
WriteWrap* finished = stream->queue_.front().req_wrap;
2201+
stream->queue_.pop();
2202+
if (finished != nullptr)
2203+
finished->Done(0);
2204+
}
2205+
21952206
if (!stream->queue_.empty()) {
21962207
DEBUG_HTTP2SESSION2(session, "stream %d has pending outbound data", id);
21972208
amount = std::min(stream->available_outbound_length_, length);
@@ -2205,7 +2216,8 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle,
22052216
}
22062217
}
22072218

2208-
if (amount == 0 && stream->IsWritable() && stream->queue_.empty()) {
2219+
if (amount == 0 && stream->IsWritable()) {
2220+
CHECK(stream->queue_.empty());
22092221
DEBUG_HTTP2SESSION2(session, "deferring stream %d", id);
22102222
return NGHTTP2_ERR_DEFERRED;
22112223
}

0 commit comments

Comments
 (0)