From 43e5478e2f51343b66f4d243bb4800d8e422f764 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 19 Jun 2019 19:24:14 -0700 Subject: domain: use strong reference to domain while active When an uncaught exception is thrown inside a domain, the domain is removed from the stack as of 43a51708589ac789ce08beaeb49d6d778dfbdc49. This means that it might not be kept alive as an object anymore, and may be garbage collected before the `after()` hook can run, which tries to exit it as well. Resolve that by making references to the domain strong while it is active. Fixes: https://github.com/nodejs/node/issues/28275 PR-URL: https://github.com/nodejs/node/pull/28313 Reviewed-By: Ben Noordhuis Reviewed-By: Vladimir de Turckheim Reviewed-By: Rich Trott --- lib/domain.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/domain.js') diff --git a/lib/domain.js b/lib/domain.js index b2ffed2741..3dc56c179a 100644 --- a/lib/domain.js +++ b/lib/domain.js @@ -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) { -- cgit v1.2.3