Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,7 @@ function setupHandle(socket, type, options) {
if (typeof options.selectPadding === 'function')
this[kSelectPadding] = options.selectPadding;
handle.consume(socket._handle);
handle.onstreamafterwrite = this[kMaybeDestroy].bind(this, null);

this[kHandle] = handle;
if (this[kNativeFields]) {
Expand Down Expand Up @@ -1609,11 +1610,13 @@ class Http2Session extends EventEmitter {
// * session is closed and there are no more pending or open streams
[kMaybeDestroy](error) {
if (error == null) {
const handle = this[kHandle];
const hasPendingData = !!handle && handle.hasPendingData();
const state = this[kState];
// Do not destroy if we're not closed and there are pending/open streams
if (!this.closed ||
state.streams.size > 0 ||
state.pendingStreams.size > 0) {
state.pendingStreams.size > 0 || hasPendingData) {
return;
}
}
Expand Down Expand Up @@ -3300,7 +3303,7 @@ function socketOnClose() {
state.streams.forEach((stream) => stream.close(NGHTTP2_CANCEL));
state.pendingStreams.forEach((stream) => stream.close(NGHTTP2_CANCEL));
session.close();
session[kMaybeDestroy](err);
closeSession(session, NGHTTP2_NO_ERROR, err);
Comment on lines -3303 to +3310
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is done as improvement. This is because when underlying socket is closed there is no need for looking at graceful closure of session by calling [kMaybeDestroy] instead we can immediately call closeSession which will handle all cleanup operation.

}
}

Expand Down
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
V(onsignal_string, "onsignal") \
V(onunpipe_string, "onunpipe") \
V(onwrite_string, "onwrite") \
V(onstreamafterwrite_string, "onstreamafterwrite") \
V(openssl_error_stack, "opensslErrorStack") \
V(options_string, "options") \
V(order_string, "order") \
Expand Down
22 changes: 22 additions & 0 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ void Http2Stream::EmitStatistics() {
});
}

void Http2Session::HasPendingData(const FunctionCallbackInfo<Value>& args) {
Http2Session* session;
ASSIGN_OR_RETURN_UNWRAP(&session, args.Holder());
args.GetReturnValue().Set(session->HasPendingData());
}

bool Http2Session::HasPendingData() const {
nghttp2_session* session = session_.get();
int want_write = nghttp2_session_want_write(session);
int want_read = nghttp2_session_want_read(session);
if (want_write == 0 && want_read == 0) {
return false;
}
return true;
}

void Http2Session::EmitStatistics() {
if (!HasHttp2Observer(env())) [[likely]] {
return;
Expand Down Expand Up @@ -1743,6 +1759,8 @@ void Http2Session::HandleSettingsFrame(const nghttp2_frame* frame) {
void Http2Session::OnStreamAfterWrite(WriteWrap* w, int status) {
Debug(this, "write finished with status %d", status);

HandleScope scope(env()->isolate());
MakeCallback(env()->onstreamafterwrite_string(), 0, nullptr);
CHECK(is_write_in_progress());
set_write_in_progress(false);

Expand Down Expand Up @@ -1965,6 +1983,8 @@ uint8_t Http2Session::SendPendingData() {
if (!res.async) {
set_write_in_progress(false);
ClearOutgoing(res.err);
HandleScope scope(env()->isolate());
MakeCallback(env()->onstreamafterwrite_string(), 0, nullptr);
}

MaybeStopReading();
Expand Down Expand Up @@ -3478,6 +3498,8 @@ void Initialize(Local<Object> target,
SetProtoMethod(isolate, session, "receive", Http2Session::Receive);
SetProtoMethod(isolate, session, "destroy", Http2Session::Destroy);
SetProtoMethod(isolate, session, "goaway", Http2Session::Goaway);
SetProtoMethod(
isolate, session, "hasPendingData", Http2Session::HasPendingData);
SetProtoMethod(isolate, session, "settings", Http2Session::Settings);
SetProtoMethod(isolate, session, "request", Http2Session::Request);
SetProtoMethod(
Expand Down
2 changes: 2 additions & 0 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ class Http2Session : public AsyncWrap,
static void Consume(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Receive(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Destroy(const v8::FunctionCallbackInfo<v8::Value>& args);
static void HasPendingData(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Settings(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Request(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetNextStreamID(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand All @@ -735,6 +736,7 @@ class Http2Session : public AsyncWrap,

BaseObjectPtr<Http2Ping> PopPing();
bool AddPing(const uint8_t* data, v8::Local<v8::Function> callback);
bool HasPendingData() const;

BaseObjectPtr<Http2Settings> PopSettings();
bool AddSettings(v8::Local<v8::Function> callback);
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-http2-session-graceful-close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');

const server = h2.createServer();
let response;

server.on('session', common.mustCall(function(session) {
session.on('stream', common.mustCall(function (stream) {
response.end();
session.close();
}));
session.on('close', common.mustCall(function() {
server.close();
}));
}));

server.listen(0, common.mustCall(function() {
const port = server.address().port;
server.once('request', common.mustCall(function (request, resp) {
response = resp;
}));

const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(function() {
const headers = {
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.on('response', common.mustCall(function(headers) {
assert.strictEqual(headers[':status'], 200);
}, 1));
request.on('end', common.mustCall(function() {
client.close();
}));
request.end();
request.resume();
}));
client.on('goaway', common.mustCallAtLeast(1));
}));