Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: keep session objects alive during Http2Scope #17863

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 13 additions & 6 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Http2Scope::Http2Scope(Http2Session* session) {
}
session->flags_ |= SESSION_STATE_HAS_SCOPE;
session_ = session;

// Always keep the session object alive for at least as long as
// this scope is active.
session_handle_ = session->object();
CHECK(!session_handle_.IsEmpty());
}

Http2Scope::~Http2Scope() {
Expand Down Expand Up @@ -174,47 +179,47 @@ void Http2Session::Http2Settings::Init() {

if (flags & (1 << IDX_SETTINGS_HEADER_TABLE_SIZE)) {
uint32_t val = buffer[IDX_SETTINGS_HEADER_TABLE_SIZE];
DEBUG_HTTP2SESSION2(session, "setting header table size: %d\n", val);
DEBUG_HTTP2SESSION2(session_, "setting header table size: %d\n", val);
entries_[n].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
entries_[n].value = val;
n++;
}

if (flags & (1 << IDX_SETTINGS_MAX_CONCURRENT_STREAMS)) {
uint32_t val = buffer[IDX_SETTINGS_MAX_CONCURRENT_STREAMS];
DEBUG_HTTP2SESSION2(session, "setting max concurrent streams: %d\n", val);
DEBUG_HTTP2SESSION2(session_, "setting max concurrent streams: %d\n", val);
entries_[n].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
entries_[n].value = val;
n++;
}

if (flags & (1 << IDX_SETTINGS_MAX_FRAME_SIZE)) {
uint32_t val = buffer[IDX_SETTINGS_MAX_FRAME_SIZE];
DEBUG_HTTP2SESSION2(session, "setting max frame size: %d\n", val);
DEBUG_HTTP2SESSION2(session_, "setting max frame size: %d\n", val);
entries_[n].settings_id = NGHTTP2_SETTINGS_MAX_FRAME_SIZE;
entries_[n].value = val;
n++;
}

if (flags & (1 << IDX_SETTINGS_INITIAL_WINDOW_SIZE)) {
uint32_t val = buffer[IDX_SETTINGS_INITIAL_WINDOW_SIZE];
DEBUG_HTTP2SESSION2(session, "setting initial window size: %d\n", val);
DEBUG_HTTP2SESSION2(session_, "setting initial window size: %d\n", val);
entries_[n].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
entries_[n].value = val;
n++;
}

if (flags & (1 << IDX_SETTINGS_MAX_HEADER_LIST_SIZE)) {
uint32_t val = buffer[IDX_SETTINGS_MAX_HEADER_LIST_SIZE];
DEBUG_HTTP2SESSION2(session, "setting max header list size: %d\n", val);
DEBUG_HTTP2SESSION2(session_, "setting max header list size: %d\n", val);
entries_[n].settings_id = NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE;
entries_[n].value = val;
n++;
}

if (flags & (1 << IDX_SETTINGS_ENABLE_PUSH)) {
uint32_t val = buffer[IDX_SETTINGS_ENABLE_PUSH];
DEBUG_HTTP2SESSION2(session, "setting enable push: %d\n", val);
DEBUG_HTTP2SESSION2(session_, "setting enable push: %d\n", val);
entries_[n].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
entries_[n].value = val;
n++;
Expand Down Expand Up @@ -512,6 +517,7 @@ void Http2Session::Unconsume() {
}

Http2Session::~Http2Session() {
CHECK_EQ(flags_ & SESSION_STATE_HAS_SCOPE, 0);
if (!object().IsEmpty())
ClearWrap(object());
persistent().Reset();
Expand Down Expand Up @@ -1365,6 +1371,7 @@ void Http2Session::OnStreamReadImpl(ssize_t nread,
void* ctx) {
Http2Session* session = static_cast<Http2Session*>(ctx);
Http2Scope h2scope(session);
CHECK_NE(session->stream_, nullptr);
DEBUG_HTTP2SESSION2(session, "receiving %d bytes", nread);
if (nread < 0) {
uv_buf_t tmp_buf;
Expand Down
1 change: 1 addition & 0 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ class Http2Scope {

private:
Http2Session* session_ = nullptr;
Local<Object> session_handle_;
};

// The Http2Options class is used to parse the options object passed in to
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-http2-createwritereq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

// Flags: --expose-gc

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
Expand Down Expand Up @@ -62,6 +64,15 @@ server.listen(0, common.mustCall(function() {
}
}));

// Ref: https://github.com/nodejs/node/issues/17840
const origDestroy = req.destroy;
req.destroy = function(...args) {
// Schedule a garbage collection event at the end of the current
// MakeCallback() run.
process.nextTick(global.gc);
return origDestroy.call(this, ...args);
};

req.end();
});
}));