Skip to content

Commit b263f25

Browse files
kumarakrichardlau
authored andcommitted
http2: on receiving rst_stream with cancel code add it to pending list
PR-URL: #39423 Backport-PR-URL: #39527 Fixes: #38964 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent e11a862 commit b263f25

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/node_http2.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2150,6 +2150,23 @@ int Http2Stream::SubmitPriority(nghttp2_priority_spec* prispec,
21502150
void Http2Stream::SubmitRstStream(const uint32_t code) {
21512151
CHECK(!this->IsDestroyed());
21522152
code_ = code;
2153+
2154+
// If RST_STREAM frame is received and stream is not writable
2155+
// because it is busy reading data, don't try force purging it.
2156+
// Instead add the stream to pending stream list and process
2157+
// the pending data when it is safe to do so. This is to avoid
2158+
// double free error due to unwanted behavior of nghttp2.
2159+
// Ref:https://github.com/nodejs/node/issues/38964
2160+
2161+
// Add stream to the pending list if it is received with scope
2162+
// below in the stack. The pending list may not get processed
2163+
// if RST_STREAM received is not in scope and added to the list
2164+
// causing endpoint to hang.
2165+
if (session_->is_in_scope() && IsReading()) {
2166+
session_->AddPendingRstStream(id_);
2167+
return;
2168+
}
2169+
21532170
// If possible, force a purge of any currently pending data here to make sure
21542171
// it is sent before closing the stream. If it returns non-zero then we need
21552172
// to wait until the current write finishes and try again to avoid nghttp2

src/node_http2.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,22 @@ class Http2Session : public AsyncWrap,
764764
return (flags_ & SESSION_STATE_CLOSED) || session_ == nullptr;
765765
}
766766

767+
768+
// The changes are backported and exposes APIs to check the
769+
// status flag of `Http2Session`
770+
#define IS_FLAG(name, flag) \
771+
bool is_##name() const { return flags_ & flag; }
772+
773+
IS_FLAG(in_scope, SESSION_STATE_HAS_SCOPE)
774+
IS_FLAG(write_scheduled, SESSION_STATE_WRITE_SCHEDULED)
775+
IS_FLAG(closing, SESSION_STATE_CLOSING)
776+
IS_FLAG(sending, SESSION_STATE_SENDING)
777+
IS_FLAG(write_in_progress, SESSION_STATE_WRITE_IN_PROGRESS)
778+
IS_FLAG(reading_stopped, SESSION_STATE_READING_STOPPED)
779+
IS_FLAG(receive_paused, SESSION_STATE_NGHTTP2_RECV_PAUSED)
780+
781+
#undef IS_FLAG
782+
767783
// Schedule a write if nghttp2 indicates it wants to write to the socket.
768784
void MaybeScheduleWrite();
769785

0 commit comments

Comments
 (0)