From aa2304b8d5c53ba2d9710d8b8ed4482f3cbd1192 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 7 Jun 2018 22:07:02 +0200 Subject: worker,src: display remaining handles if `uv_loop_close` fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Right now, we crash the process if there are handles remaining on the event loop when we exit (except for the main thread). This does not provide a lot of information about causes, though; in particular, we don’t show which handles are pending and who own them. This patch adds debug output to these cases to help with the situation. PR-URL: https://github.com/nodejs/node/pull/21238 Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: James M Snell Reviewed-By: Gus Caplan Reviewed-By: Ben Noordhuis Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig --- test/addons/uv-handle-leak/binding.cc | 48 ++++++++++++++++++++++++++++++++++ test/addons/uv-handle-leak/binding.gyp | 9 +++++++ test/addons/uv-handle-leak/test.js | 23 ++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 test/addons/uv-handle-leak/binding.cc create mode 100644 test/addons/uv-handle-leak/binding.gyp create mode 100644 test/addons/uv-handle-leak/test.js (limited to 'test/addons') diff --git a/test/addons/uv-handle-leak/binding.cc b/test/addons/uv-handle-leak/binding.cc new file mode 100644 index 0000000000..c2e5f0bf27 --- /dev/null +++ b/test/addons/uv-handle-leak/binding.cc @@ -0,0 +1,48 @@ +#include +#include +#include + +using v8::Context; +using v8::FunctionCallbackInfo; +using v8::Isolate; +using v8::Local; +using v8::Object; +using v8::Value; + +// Give these things names in the public namespace so that we can see +// them show up in symbol dumps. +void CloseCallback(uv_handle_t* handle) {} + +class ExampleOwnerClass { + public: + virtual ~ExampleOwnerClass() {} +}; + +ExampleOwnerClass example_instance; + +void LeakHandle(const FunctionCallbackInfo& args) { + Isolate* isolate = args.GetIsolate(); + Local context = isolate->GetCurrentContext(); + uv_loop_t* loop = node::GetCurrentEventLoop(isolate); + assert(loop != nullptr); + + uv_timer_t* leaked_timer = new uv_timer_t; + leaked_timer->close_cb = CloseCallback; + + if (args[0]->IsNumber()) { + leaked_timer->data = + reinterpret_cast(args[0]->IntegerValue(context).FromJust()); + } else { + leaked_timer->data = &example_instance; + } + + uv_timer_init(loop, leaked_timer); + uv_timer_start(leaked_timer, [](uv_timer_t*){}, 1000, 1000); + uv_unref(reinterpret_cast(leaked_timer)); +} + +void Initialize(v8::Local exports) { + NODE_SET_METHOD(exports, "leakHandle", LeakHandle); +} + +NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) diff --git a/test/addons/uv-handle-leak/binding.gyp b/test/addons/uv-handle-leak/binding.gyp new file mode 100644 index 0000000000..7ede63d94a --- /dev/null +++ b/test/addons/uv-handle-leak/binding.gyp @@ -0,0 +1,9 @@ +{ + 'targets': [ + { + 'target_name': 'binding', + 'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], + 'sources': [ 'binding.cc' ] + } + ] +} diff --git a/test/addons/uv-handle-leak/test.js b/test/addons/uv-handle-leak/test.js new file mode 100644 index 0000000000..73d40ca799 --- /dev/null +++ b/test/addons/uv-handle-leak/test.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../../common'); +const bindingPath = require.resolve(`./build/${common.buildType}/binding`); +const binding = require(bindingPath); + +// This tests checks that addons may leak libuv handles until process exit. +// It’s really not a good idea to do so, but it tests existing behaviour +// that likely can never be removed for backwards compatibility. + +// This has a sibling test in test/abort/ which checks output for failures +// from workers. + +try { + // We don’t want to run this in Workers because they do actually enforce + // a clean-exit policy. + const { isMainThread } = require('worker_threads'); + if (!isMainThread) + common.skip('Cannot run test in environment with clean-exit policy'); +} catch {} + +binding.leakHandle(); +binding.leakHandle(0); +binding.leakHandle(1); -- cgit v1.2.3