Skip to content

Commit e17d314

Browse files
addaleaxMylesBorins
authored andcommitted
src: remove keep alive option from SetImmediate()
This is no longer necessary now that the copyable `BaseObjectPtr` is available (as opposed to the only-movable `v8::Global`). Backport-PR-URL: #32301 PR-URL: #30374 Refs: nodejs/quic#141 Refs: nodejs/quic#149 Refs: nodejs/quic#141 Refs: nodejs/quic#165 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com>
1 parent 6db84d3 commit e17d314

File tree

6 files changed

+39
-43
lines changed

6 files changed

+39
-43
lines changed

src/cares_wrap.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,6 @@ class QueryWrap : public AsyncWrap {
627627
} else {
628628
Parse(response_data_->host.get());
629629
}
630-
631-
delete this;
632630
}
633631

634632
void* MakeCallbackPointer() {
@@ -686,9 +684,13 @@ class QueryWrap : public AsyncWrap {
686684
}
687685

688686
void QueueResponseCallback(int status) {
689-
env()->SetImmediate([this](Environment*) {
687+
BaseObjectPtr<QueryWrap> strong_ref{this};
688+
env()->SetImmediate([this, strong_ref](Environment*) {
690689
AfterResponse();
691-
}, object());
690+
691+
// Delete once strong_ref goes out of scope.
692+
Detach();
693+
});
692694

693695
channel_->set_query_last_ok(status != ARES_ECONNREFUSED);
694696
channel_->ModifyActivityQueryCount(-1);

src/env-inl.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,9 @@ inline void IsolateData::set_options(
733733
}
734734

735735
template <typename Fn>
736-
void Environment::CreateImmediate(Fn&& cb,
737-
v8::Local<v8::Object> keep_alive,
738-
bool ref) {
736+
void Environment::CreateImmediate(Fn&& cb, bool ref) {
739737
auto callback = std::make_unique<NativeImmediateCallbackImpl<Fn>>(
740-
std::move(cb),
741-
v8::Global<v8::Object>(isolate(), keep_alive),
742-
ref);
738+
std::move(cb), ref);
743739
NativeImmediateCallback* prev_tail = native_immediate_callbacks_tail_;
744740

745741
native_immediate_callbacks_tail_ = callback.get();
@@ -752,17 +748,17 @@ void Environment::CreateImmediate(Fn&& cb,
752748
}
753749

754750
template <typename Fn>
755-
void Environment::SetImmediate(Fn&& cb, v8::Local<v8::Object> keep_alive) {
756-
CreateImmediate(std::move(cb), keep_alive, true);
751+
void Environment::SetImmediate(Fn&& cb) {
752+
CreateImmediate(std::move(cb), true);
757753

758754
if (immediate_info()->ref_count() == 0)
759755
ToggleImmediateRef(true);
760756
immediate_info()->ref_count_inc(1);
761757
}
762758

763759
template <typename Fn>
764-
void Environment::SetUnrefImmediate(Fn&& cb, v8::Local<v8::Object> keep_alive) {
765-
CreateImmediate(std::move(cb), keep_alive, false);
760+
void Environment::SetUnrefImmediate(Fn&& cb) {
761+
CreateImmediate(std::move(cb), false);
766762
}
767763

768764
Environment::NativeImmediateCallback::NativeImmediateCallback(bool refed)
@@ -784,10 +780,9 @@ void Environment::NativeImmediateCallback::set_next(
784780

785781
template <typename Fn>
786782
Environment::NativeImmediateCallbackImpl<Fn>::NativeImmediateCallbackImpl(
787-
Fn&& callback, v8::Global<v8::Object>&& keep_alive, bool refed)
783+
Fn&& callback, bool refed)
788784
: NativeImmediateCallback(refed),
789-
callback_(std::move(callback)),
790-
keep_alive_(std::move(keep_alive)) {}
785+
callback_(std::move(callback)) {}
791786

792787
template <typename Fn>
793788
void Environment::NativeImmediateCallbackImpl<Fn>::Call(Environment* env) {

src/env.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,13 +1191,9 @@ class Environment : public MemoryRetainer {
11911191
// cb will be called as cb(env) on the next event loop iteration.
11921192
// keep_alive will be kept alive between now and after the callback has run.
11931193
template <typename Fn>
1194-
inline void SetImmediate(Fn&& cb,
1195-
v8::Local<v8::Object> keep_alive =
1196-
v8::Local<v8::Object>());
1194+
inline void SetImmediate(Fn&& cb);
11971195
template <typename Fn>
1198-
inline void SetUnrefImmediate(Fn&& cb,
1199-
v8::Local<v8::Object> keep_alive =
1200-
v8::Local<v8::Object>());
1196+
inline void SetUnrefImmediate(Fn&& cb);
12011197
// This needs to be available for the JS-land setImmediate().
12021198
void ToggleImmediateRef(bool ref);
12031199

@@ -1274,9 +1270,7 @@ class Environment : public MemoryRetainer {
12741270

12751271
private:
12761272
template <typename Fn>
1277-
inline void CreateImmediate(Fn&& cb,
1278-
v8::Local<v8::Object> keep_alive,
1279-
bool ref);
1273+
inline void CreateImmediate(Fn&& cb, bool ref);
12801274

12811275
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
12821276
const char* errmsg);
@@ -1424,14 +1418,11 @@ class Environment : public MemoryRetainer {
14241418
template <typename Fn>
14251419
class NativeImmediateCallbackImpl final : public NativeImmediateCallback {
14261420
public:
1427-
NativeImmediateCallbackImpl(Fn&& callback,
1428-
v8::Global<v8::Object>&& keep_alive,
1429-
bool refed);
1421+
NativeImmediateCallbackImpl(Fn&& callback, bool refed);
14301422
void Call(Environment* env) override;
14311423

14321424
private:
14331425
Fn callback_;
1434-
v8::Global<v8::Object> keep_alive_;
14351426
};
14361427

14371428
std::unique_ptr<NativeImmediateCallback> native_immediate_callbacks_head_;

src/node_http2.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,8 @@ void Http2Session::MaybeScheduleWrite() {
15501550
HandleScope handle_scope(env()->isolate());
15511551
Debug(this, "scheduling write");
15521552
flags_ |= SESSION_STATE_WRITE_SCHEDULED;
1553-
env()->SetImmediate([this](Environment* env) {
1553+
BaseObjectPtr<Http2Session> strong_ref{this};
1554+
env()->SetImmediate([this, strong_ref](Environment* env) {
15541555
if (session_ == nullptr || !(flags_ & SESSION_STATE_WRITE_SCHEDULED)) {
15551556
// This can happen e.g. when a stream was reset before this turn
15561557
// of the event loop, in which case SendPendingData() is called early,
@@ -1563,7 +1564,7 @@ void Http2Session::MaybeScheduleWrite() {
15631564
HandleScope handle_scope(env->isolate());
15641565
InternalCallbackScope callback_scope(this);
15651566
SendPendingData();
1566-
}, object());
1567+
});
15671568
}
15681569
}
15691570

@@ -2018,7 +2019,8 @@ void Http2Stream::Destroy() {
20182019

20192020
// Wait until the start of the next loop to delete because there
20202021
// may still be some pending operations queued for this stream.
2021-
env()->SetImmediate([this](Environment* env) {
2022+
BaseObjectPtr<Http2Stream> strong_ref{this};
2023+
env()->SetImmediate([this, strong_ref](Environment* env) {
20222024
// Free any remaining outgoing data chunks here. This should be done
20232025
// here because it's possible for destroy to have been called while
20242026
// we still have queued outbound writes.
@@ -2032,9 +2034,11 @@ void Http2Stream::Destroy() {
20322034
// We can destroy the stream now if there are no writes for it
20332035
// already on the socket. Otherwise, we'll wait for the garbage collector
20342036
// to take care of cleaning up.
2035-
if (session() == nullptr || !session()->HasWritesOnSocketForStream(this))
2036-
delete this;
2037-
}, object());
2037+
if (session() == nullptr || !session()->HasWritesOnSocketForStream(this)) {
2038+
// Delete once strong_ref goes out of scope.
2039+
Detach();
2040+
}
2041+
});
20382042

20392043
statistics_.end_time = uv_hrtime();
20402044
session_->statistics_.stream_average_duration =

src/stream_pipe.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ void StreamPipe::Unpipe() {
7272
// Delay the JS-facing part with SetImmediate, because this might be from
7373
// inside the garbage collector, so we can’t run JS here.
7474
HandleScope handle_scope(env()->isolate());
75+
BaseObjectPtr<StreamPipe> strong_ref{this};
7576
env()->SetImmediate([this](Environment* env) {
7677
HandleScope handle_scope(env->isolate());
7778
Context::Scope context_scope(env->context());
@@ -106,7 +107,7 @@ void StreamPipe::Unpipe() {
106107
.IsNothing()) {
107108
return;
108109
}
109-
}, object());
110+
});
110111
}
111112

112113
uv_buf_t StreamPipe::ReadableListener::OnStreamAlloc(size_t suggested_size) {

src/tls_wrap.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,10 @@ void TLSWrap::EncOut() {
320320
// its not clear if it is always correct. Not calling Done() could block
321321
// data flow, so for now continue to call Done(), just do it in the next
322322
// tick.
323-
env()->SetImmediate([this](Environment* env) {
323+
BaseObjectPtr<TLSWrap> strong_ref{this};
324+
env()->SetImmediate([this, strong_ref](Environment* env) {
324325
InvokeQueued(0);
325-
}, object());
326+
});
326327
}
327328
}
328329
return;
@@ -353,9 +354,10 @@ void TLSWrap::EncOut() {
353354
HandleScope handle_scope(env()->isolate());
354355

355356
// Simulate asynchronous finishing, TLS cannot handle this at the moment.
356-
env()->SetImmediate([this](Environment* env) {
357+
BaseObjectPtr<TLSWrap> strong_ref{this};
358+
env()->SetImmediate([this, strong_ref](Environment* env) {
357359
OnStreamAfterWrite(nullptr, 0);
358-
}, object());
360+
});
359361
}
360362
}
361363

@@ -730,9 +732,10 @@ int TLSWrap::DoWrite(WriteWrap* w,
730732
StreamWriteResult res =
731733
underlying_stream()->Write(bufs, count, send_handle);
732734
if (!res.async) {
733-
env()->SetImmediate([this](Environment* env) {
735+
BaseObjectPtr<TLSWrap> strong_ref{this};
736+
env()->SetImmediate([this, strong_ref](Environment* env) {
734737
OnStreamAfterWrite(current_empty_write_, 0);
735-
}, object());
738+
});
736739
}
737740
return 0;
738741
}

0 commit comments

Comments
 (0)