Skip to content

Commit dc3f6c0

Browse files
addaleaxMylesBorins
authored andcommitted
http2: fix endless loop when writing empty string
Backport-PR-URL: #20456 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 81c08a1 commit dc3f6c0

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
@@ -2209,6 +2209,17 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle,
22092209

22102210
size_t amount = 0; // amount of data being sent in this data frame.
22112211

2212+
// Remove all empty chunks from the head of the queue.
2213+
// This is done here so that .write('', cb) is still a meaningful way to
2214+
// find out when the HTTP2 stream wants to consume data, and because the
2215+
// StreamBase API allows empty input chunks.
2216+
while (!stream->queue_.empty() && stream->queue_.front().buf.len == 0) {
2217+
WriteWrap* finished = stream->queue_.front().req_wrap;
2218+
stream->queue_.pop();
2219+
if (finished != nullptr)
2220+
finished->Done(0);
2221+
}
2222+
22122223
if (!stream->queue_.empty()) {
22132224
DEBUG_HTTP2SESSION2(session, "stream %d has pending outbound data", id);
22142225
amount = std::min(stream->available_outbound_length_, length);
@@ -2222,7 +2233,8 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle,
22222233
}
22232234
}
22242235

2225-
if (amount == 0 && stream->IsWritable() && stream->queue_.empty()) {
2236+
if (amount == 0 && stream->IsWritable()) {
2237+
CHECK(stream->queue_.empty());
22262238
DEBUG_HTTP2SESSION2(session, "deferring stream %d", id);
22272239
return NGHTTP2_ERR_DEFERRED;
22282240
}

0 commit comments

Comments
 (0)