Skip to content

Commit

Permalink
src: no SetImmediate from destructor in stream_pipe code
Browse files Browse the repository at this point in the history
Guard against running `SetImmediate()` from the destructor.
The object will not be alive or usable in the callback,
so it does not make sense to attempt to schedule the
`SetImmediate()`.

Backport-PR-URL: #32301
PR-URL: #30666
Fixes: #30643
Refs: #30374
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and MylesBorins committed Apr 1, 2020
1 parent 51230f7 commit 5b65348
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/stream_pipe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ StreamPipe::StreamPipe(StreamBase* source,
}

StreamPipe::~StreamPipe() {
Unpipe();
Unpipe(true);
}

StreamBase* StreamPipe::source() {
Expand All @@ -53,7 +53,7 @@ StreamBase* StreamPipe::sink() {
return static_cast<StreamBase*>(writable_listener_.stream());
}

void StreamPipe::Unpipe() {
void StreamPipe::Unpipe(bool is_in_deletion) {
if (is_closed_)
return;

Expand All @@ -69,6 +69,8 @@ void StreamPipe::Unpipe() {
if (pending_writes_ == 0)
sink()->RemoveStreamListener(&writable_listener_);

if (is_in_deletion) return;

// Delay the JS-facing part with SetImmediate, because this might be from
// inside the garbage collector, so we can’t run JS here.
HandleScope handle_scope(env()->isolate());
Expand Down
2 changes: 1 addition & 1 deletion src/stream_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StreamPipe : public AsyncWrap {
StreamPipe(StreamBase* source, StreamBase* sink, v8::Local<v8::Object> obj);
~StreamPipe() override;

void Unpipe();
void Unpipe(bool is_in_deletion = false);

static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down

0 comments on commit 5b65348

Please sign in to comment.