Skip to content

Commit a8c2c66

Browse files
lundibundiBridgeAR
authored andcommitted
http2: wait for session to finish writing before destroy
PR-URL: #30854 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 3987901 commit a8c2c66

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/internal/http2/core.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ function emitClose(self, error) {
986986
}
987987

988988
function finishSessionDestroy(session, error) {
989+
debugSessionObj(session, 'finishSessionDestroy');
990+
989991
const socket = session[kSocket];
990992
if (!socket.destroyed)
991993
socket.destroy(error);
@@ -1354,16 +1356,12 @@ class Http2Session extends EventEmitter {
13541356
const handle = this[kHandle];
13551357

13561358
// Destroy the handle if it exists at this point
1357-
if (handle !== undefined)
1359+
if (handle !== undefined) {
1360+
handle.ondone = finishSessionDestroy.bind(null, this, error);
13581361
handle.destroy(code, socket.destroyed);
1359-
1360-
// If the socket is alive, use setImmediate to destroy the session on the
1361-
// next iteration of the event loop in order to give data time to transmit.
1362-
// Otherwise, destroy immediately.
1363-
if (!socket.destroyed)
1364-
setImmediate(finishSessionDestroy, this, error);
1365-
else
1362+
} else {
13661363
finishSessionDestroy(this, error);
1364+
}
13671365
}
13681366

13691367
// Closing the session will:

src/node_http2.cc

+13
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,13 @@ void Http2Session::Close(uint32_t code, bool socket_closed) {
700700

701701
flags_ |= SESSION_STATE_CLOSED;
702702

703+
// If we are writing we will get to make the callback in OnStreamAfterWrite.
704+
if ((flags_ & SESSION_STATE_WRITE_IN_PROGRESS) == 0) {
705+
Debug(this, "make done session callback");
706+
HandleScope scope(env()->isolate());
707+
MakeCallback(env()->ondone_string(), 0, nullptr);
708+
}
709+
703710
// If there are outstanding pings, those will need to be canceled, do
704711
// so on the next iteration of the event loop to avoid calling out into
705712
// javascript since this may be called during garbage collection.
@@ -1502,6 +1509,12 @@ void Http2Session::OnStreamAfterWrite(WriteWrap* w, int status) {
15021509
stream_->ReadStart();
15031510
}
15041511

1512+
if ((flags_ & SESSION_STATE_CLOSED) != 0) {
1513+
HandleScope scope(env()->isolate());
1514+
MakeCallback(env()->ondone_string(), 0, nullptr);
1515+
return;
1516+
}
1517+
15051518
// If there is more incoming data queued up, consume it.
15061519
if (stream_buf_offset_ > 0) {
15071520
ConsumeHTTP2Data();

0 commit comments

Comments
 (0)