Skip to content
Permalink
Browse files

http2: wait for session to finish writing before destroy

PR-URL: #30854
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
lundibundi authored and BridgeAR committed Dec 7, 2019
1 parent 69d6e97 commit 86f2e869dcc287d44185b64881a1b6e07a55a872
Showing with 19 additions and 8 deletions.
  1. +6 −8 lib/internal/http2/core.js
  2. +13 −0 src/node_http2.cc
@@ -986,6 +986,8 @@ function emitClose(self, error) {
}

function finishSessionDestroy(session, error) {
debugSessionObj(session, 'finishSessionDestroy');

const socket = session[kSocket];
if (!socket.destroyed)
socket.destroy(error);
@@ -1354,16 +1356,12 @@ class Http2Session extends EventEmitter {
const handle = this[kHandle];

// Destroy the handle if it exists at this point
if (handle !== undefined)
if (handle !== undefined) {
handle.ondone = finishSessionDestroy.bind(null, this, error);
handle.destroy(code, socket.destroyed);

// If the socket is alive, use setImmediate to destroy the session on the
// next iteration of the event loop in order to give data time to transmit.
// Otherwise, destroy immediately.
if (!socket.destroyed)
setImmediate(finishSessionDestroy, this, error);
else
} else {
finishSessionDestroy(this, error);
}
}

// Closing the session will:
@@ -683,6 +683,13 @@ void Http2Session::Close(uint32_t code, bool socket_closed) {

flags_ |= SESSION_STATE_CLOSED;

// If we are writing we will get to make the callback in OnStreamAfterWrite.
if ((flags_ & SESSION_STATE_WRITE_IN_PROGRESS) == 0) {
Debug(this, "make done session callback");
HandleScope scope(env()->isolate());
MakeCallback(env()->ondone_string(), 0, nullptr);
}

// If there are outstanding pings, those will need to be canceled, do
// so on the next iteration of the event loop to avoid calling out into
// javascript since this may be called during garbage collection.
@@ -1485,6 +1492,12 @@ void Http2Session::OnStreamAfterWrite(WriteWrap* w, int status) {
stream_->ReadStart();
}

if ((flags_ & SESSION_STATE_CLOSED) != 0) {
HandleScope scope(env()->isolate());
MakeCallback(env()->ondone_string(), 0, nullptr);
return;
}

// If there is more incoming data queued up, consume it.
if (stream_buf_offset_ > 0) {
ConsumeHTTP2Data();

0 comments on commit 86f2e86

Please sign in to comment.
You can’t perform that action at this time.