From b045893ea8d1539dbdb05d8c0aeb3639a657612b Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 17 Jun 2022 14:03:06 +0530 Subject: [PATCH] src: register StreamBase while registering LibuvStreamWrap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This resolves this TODO - https://github.com/nodejs/node/blob/71071f896aa9696b5d36bb3bec1c7217e7420509/src/stream_wrap.cc#L111-L112. Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/43321 Reviewed-By: Joyee Cheung Reviewed-By: Tobias Nießen Reviewed-By: Santiago Gimeno Reviewed-By: James M Snell --- src/stream_base.cc | 5 +++++ src/stream_wrap.cc | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/stream_base.cc b/src/stream_base.cc index a9ea347ca6bd47..783e12a36c8e52 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -452,6 +452,10 @@ void StreamBase::AddMethods(Environment* env, Local t) { void StreamBase::RegisterExternalReferences( ExternalReferenceRegistry* registry) { + // This function is called by a single thread during start up, so it is safe + // to use a local static variable here. + static bool is_registered = false; + if (is_registered) return; registry->Register(GetFD); registry->Register(GetExternal); registry->Register(GetBytesRead); @@ -471,6 +475,7 @@ void StreamBase::RegisterExternalReferences( registry->Register( BaseObject::InternalFieldSet); + is_registered = true; } void StreamBase::GetFD(const FunctionCallbackInfo& args) { diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index ff3df6bc5db7bd..ef85ba681f8588 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -108,8 +108,7 @@ void LibuvStreamWrap::RegisterExternalReferences( registry->Register(IsConstructCallCallback); registry->Register(GetWriteQueueSize); registry->Register(SetBlocking); - // TODO(joyee): StreamBase::RegisterExternalReferences() is called somewhere - // else but we may want to do it here too and guard it with a static flag. + StreamBase::RegisterExternalReferences(registry); } LibuvStreamWrap::LibuvStreamWrap(Environment* env,