Skip to content

Commit 7eadb1b

Browse files
lundibunditargos
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 ee9e817 commit 7eadb1b

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
@@ -1010,6 +1010,8 @@ function emitClose(self, error) {
10101010
}
10111011

10121012
function finishSessionDestroy(session, error) {
1013+
debugSessionObj(session, 'finishSessionDestroy');
1014+
10131015
const socket = session[kSocket];
10141016
if (!socket.destroyed)
10151017
socket.destroy(error);
@@ -1378,16 +1380,12 @@ class Http2Session extends EventEmitter {
13781380
const handle = this[kHandle];
13791381

13801382
// Destroy the handle if it exists at this point
1381-
if (handle !== undefined)
1383+
if (handle !== undefined) {
1384+
handle.ondone = finishSessionDestroy.bind(null, this, error);
13821385
handle.destroy(code, socket.destroyed);
1383-
1384-
// If the socket is alive, use setImmediate to destroy the session on the
1385-
// next iteration of the event loop in order to give data time to transmit.
1386-
// Otherwise, destroy immediately.
1387-
if (!socket.destroyed)
1388-
setImmediate(finishSessionDestroy, this, error);
1389-
else
1386+
} else {
13901387
finishSessionDestroy(this, error);
1388+
}
13911389
}
13921390

13931391
// Closing the session will:

src/node_http2.cc

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

690690
flags_ |= SESSION_STATE_CLOSED;
691691

692+
// If we are writing we will get to make the callback in OnStreamAfterWrite.
693+
if ((flags_ & SESSION_STATE_WRITE_IN_PROGRESS) == 0) {
694+
Debug(this, "make done session callback");
695+
HandleScope scope(env()->isolate());
696+
MakeCallback(env()->ondone_string(), 0, nullptr);
697+
}
698+
692699
// If there are outstanding pings, those will need to be canceled, do
693700
// so on the next iteration of the event loop to avoid calling out into
694701
// javascript since this may be called during garbage collection.
@@ -1526,6 +1533,12 @@ void Http2Session::OnStreamAfterWrite(WriteWrap* w, int status) {
15261533
stream_->ReadStart();
15271534
}
15281535

1536+
if ((flags_ & SESSION_STATE_CLOSED) != 0) {
1537+
HandleScope scope(env()->isolate());
1538+
MakeCallback(env()->ondone_string(), 0, nullptr);
1539+
return;
1540+
}
1541+
15291542
// If there is more incoming data queued up, consume it.
15301543
if (stream_buf_offset_ > 0) {
15311544
ConsumeHTTP2Data();

0 commit comments

Comments
 (0)