Skip to content

domain: use strong reference to domain while active #28313

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

Closed
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,18 @@ const asyncHook = createHook({
if (current !== undefined) { // Enter domain for this cb
// We will get the domain through current.get(), because the resource
// object's .domain property makes sure it is not garbage collected.
// However, we do need to make the reference to the domain non-weak,
// so that it cannot be garbage collected before the after() hook.
current.incRef();
current.get().enter();
}
},
after(asyncId) {
const current = pairing.get(asyncId);
if (current !== undefined) { // Exit domain for this cb
current.get().exit();
const domain = current.get();
current.decRef();
domain.exit();
}
},
destroy(asyncId) {
Expand Down
16 changes: 16 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,26 @@ class WeakReference : public BaseObject {
args.GetReturnValue().Set(weak_ref->target_.Get(isolate));
}

static void IncRef(const FunctionCallbackInfo<Value>& args) {
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
if (weak_ref->reference_count_ == 0) weak_ref->target_.ClearWeak();
weak_ref->reference_count_++;
}

static void DecRef(const FunctionCallbackInfo<Value>& args) {
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
CHECK_GE(weak_ref->reference_count_, 1);
weak_ref->reference_count_--;
if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
}

SET_MEMORY_INFO_NAME(WeakReference)
SET_SELF_SIZE(WeakReference)
SET_NO_MEMORY_INFO()

private:
Global<Object> target_;
uint64_t reference_count_ = 0;
};

static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
Expand Down Expand Up @@ -294,6 +308,8 @@ void Initialize(Local<Object> target,
weak_ref->InstanceTemplate()->SetInternalFieldCount(1);
weak_ref->SetClassName(weak_ref_string);
env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
env->SetProtoMethod(weak_ref, "incRef", WeakReference::IncRef);
env->SetProtoMethod(weak_ref, "decRef", WeakReference::DecRef);
target->Set(context, weak_ref_string,
weak_ref->GetFunction(context).ToLocalChecked()).Check();

Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-domain-error-types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --gc-interval=100 --stress-compaction
'use strict';

const common = require('../common');
Expand All @@ -6,6 +7,8 @@ const domain = require('domain');

// This test is similar to test-domain-multiple-errors, but uses a new domain
// for each errors.
// The test flags are not essential, but serve as a way to verify that
// https://github.com/nodejs/node/issues/28275 is fixed in debug mode.

for (const something of [
42, null, undefined, false, () => {}, 'string', Symbol('foo')
Expand Down