Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

stream_wrap: reference handle before uv_write2 #4663

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 11 additions & 0 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static Persistent<String> bytes_sym;
static Persistent<String> write_queue_size_sym;
static Persistent<String> onread_sym;
static Persistent<String> oncomplete_sym;
static Persistent<String> handle_sym;
static SlabAllocator* slab_allocator;
static bool initialized;

Expand Down Expand Up @@ -411,6 +412,13 @@ Handle<Value> StreamWrap::WriteStringImpl(const Arguments& args) {
StreamWrap* send_stream_wrap = static_cast<StreamWrap*>(
send_stream_obj->GetAlignedPointerFromInternalField(0));
send_stream = send_stream_wrap->GetStream();

// Reference StreamWrap instance to prevent it from being garbage
// collected before `AfterWrite` will be called.
Copy link
Member

Choose a reason for hiding this comment

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

s/will be/is/

if (handle_sym.IsEmpty()) {
handle_sym = NODE_PSYMBOL("handle");
}
req_wrap->object_->Set(handle_sym, send_stream_obj);
}

r = uv_write2(&req_wrap->req_,
Expand Down Expand Up @@ -468,6 +476,9 @@ void StreamWrap::AfterWrite(uv_write_t* req, int status) {
assert(req_wrap->object_.IsEmpty() == false);
assert(wrap->object_.IsEmpty() == false);

// Unref handle propery
Copy link
Member

Choose a reason for hiding this comment

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

s/propery/properly/ (or 'property')?

req_wrap->object_->Delete(handle_sym);

if (status) {
SetErrno(uv_last_error(uv_default_loop()));
}
Expand Down