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

http: simplify parser lifetime tracking #18135

Closed
wants to merge 2 commits into from
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
5 changes: 4 additions & 1 deletion lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ var parsers = new FreeList('parsers', 1000, function() {
return parser;
});

function closeParserInstance(parser) { parser.close(); }

// Free the parser and also break any links that it
// might have to any other things.
Expand All @@ -212,7 +213,9 @@ function freeParser(parser, req, socket) {
parser.outgoing = null;
parser[kOnExecute] = null;
if (parsers.free(parser) === false) {
parser.close();
// Make sure the parser's stack has unwound before deleting the
// corresponding C++ object through .close().
setImmediate(closeParserInstance, parser);
} else {
// Since the Parser destructor isn't going to run the destroy() callbacks
// it needs to be triggered manually.
Expand Down
24 changes: 1 addition & 23 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,7 @@ class Parser : public AsyncWrap {
Parser* parser;
ASSIGN_OR_RETURN_UNWRAP(&parser, args.Holder());

if (--parser->refcount_ == 0)
delete parser;
delete parser;
}


Expand Down Expand Up @@ -559,22 +558,6 @@ class Parser : public AsyncWrap {
}

protected:
class ScopedRetainParser {
public:
explicit ScopedRetainParser(Parser* p) : p_(p) {
CHECK_GT(p_->refcount_, 0);
p_->refcount_++;
}

~ScopedRetainParser() {
if (0 == --p_->refcount_)
delete p_;
}

private:
Parser* const p_;
};

static const size_t kAllocBufferSize = 64 * 1024;

static void OnAllocImpl(size_t suggested_size, uv_buf_t* buf, void* ctx) {
Expand Down Expand Up @@ -611,8 +594,6 @@ class Parser : public AsyncWrap {
if (nread == 0)
return;

ScopedRetainParser retain(parser);

parser->current_buffer_.Clear();
Local<Value> ret = parser->Execute(buf->base, nread);

Expand Down Expand Up @@ -750,10 +731,7 @@ class Parser : public AsyncWrap {
char* current_buffer_data_;
StreamResource::Callback<StreamResource::AllocCb> prev_alloc_cb_;
StreamResource::Callback<StreamResource::ReadCb> prev_read_cb_;
int refcount_ = 1;
static const struct http_parser_settings settings;

friend class ScopedRetainParser;
};


Expand Down