Skip to content

Commit 759829c

Browse files
lundibunditargos
authored andcommitted
http2: wait for session to finish writing before destroy
PR-URL: nodejs#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 af392a1 commit 759829c

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

lib/internal/http2/core.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,8 @@ function emitClose(self, error) {
10171017
}
10181018

10191019
function finishSessionDestroy(session, error) {
1020+
debugSessionObj(session, 'finishSessionDestroy');
1021+
10201022
const socket = session[kSocket];
10211023
if (!socket.destroyed)
10221024
socket.destroy(error);
@@ -1385,16 +1387,12 @@ class Http2Session extends EventEmitter {
13851387
const handle = this[kHandle];
13861388

13871389
// Destroy the handle if it exists at this point
1388-
if (handle !== undefined)
1390+
if (handle !== undefined) {
1391+
handle.ondone = finishSessionDestroy.bind(null, this, error);
13891392
handle.destroy(code, socket.destroyed);
1390-
1391-
// If the socket is alive, use setImmediate to destroy the session on the
1392-
// next iteration of the event loop in order to give data time to transmit.
1393-
// Otherwise, destroy immediately.
1394-
if (!socket.destroyed)
1395-
setImmediate(finishSessionDestroy, this, error);
1396-
else
1393+
} else {
13971394
finishSessionDestroy(this, error);
1395+
}
13981396
}
13991397

14001398
// Closing the session will:

src/node_http2.cc

Lines changed: 13 additions & 0 deletions
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)