Skip to content

Commit c3d8efb

Browse files
committed
src: improve checked uv loop close output
Addon developers may run into this when not closing libuv handles inside Workers. Previously, output may have included unhelpful statements such as `uv loop at ... has 0 active handles`, which may sound like everything’s supposed to be fine actually. So, instead of printing the active handle count, print the total handle count and mark active handles individually. Also, fix the test for this to work properly and make sure that parsing finishes properly.
1 parent 3a4d916 commit c3d8efb

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/debug_utils.cc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,21 @@ void PrintLibuvHandleInformation(uv_loop_t* loop, FILE* stream) {
292292
struct Info {
293293
std::unique_ptr<NativeSymbolDebuggingContext> ctx;
294294
FILE* stream;
295+
size_t num_handles;
295296
};
296297

297-
Info info { NativeSymbolDebuggingContext::New(), stream };
298+
Info info { NativeSymbolDebuggingContext::New(), stream, 0 };
298299

299-
fprintf(stream, "uv loop at [%p] has %d active handles\n",
300-
loop, loop->active_handles);
300+
fprintf(stream, "uv loop at [%p] has open handles:\n", loop);
301301

302302
uv_walk(loop, [](uv_handle_t* handle, void* arg) {
303303
Info* info = static_cast<Info*>(arg);
304304
NativeSymbolDebuggingContext* sym_ctx = info->ctx.get();
305305
FILE* stream = info->stream;
306+
info->num_handles++;
306307

307-
fprintf(stream, "[%p] %s\n", handle, uv_handle_type_name(handle->type));
308+
fprintf(stream, "[%p] %s%s\n", handle, uv_handle_type_name(handle->type),
309+
uv_is_active(handle) ? " (active)" : "");
308310

309311
void* close_cb = reinterpret_cast<void*>(handle->close_cb);
310312
fprintf(stream, "\tClose callback: %p %s\n",
@@ -328,6 +330,9 @@ void PrintLibuvHandleInformation(uv_loop_t* loop, FILE* stream) {
328330
first_field, sym_ctx->LookupSymbol(first_field).Display().c_str());
329331
}
330332
}, &info);
333+
334+
fprintf(stream, "uv loop at [%p] has %zu open handles in total\n",
335+
loop, info.num_handles);
331336
}
332337

333338
std::vector<std::string> NativeSymbolDebuggingContext::GetLoadedLibraries() {

test/abort/test-addon-uv-handle-leak.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ if (process.argv[2] === 'child') {
4747

4848
// Parse output that is formatted like this:
4949

50-
// uv loop at [0x559b65ed5770] has active handles
51-
// [0x7f2de0018430] timer
50+
// uv loop at [0x559b65ed5770] has open handles:
51+
// [0x7f2de0018430] timer (active)
5252
// Close callback: 0x7f2df31de220 CloseCallback(uv_handle_s*) [...]
5353
// Data: 0x7f2df33df140 example_instance [...]
5454
// (First field): 0x7f2df33dedc0 vtable for ExampleOwnerClass [...]
@@ -89,15 +89,15 @@ if (process.argv[2] === 'child') {
8989

9090
switch (state) {
9191
case 'initial':
92-
assert(/^uv loop at \[.+\] has \d+ active handles$/.test(line), line);
92+
assert(/^uv loop at \[.+\] has open handles:$/.test(line), line);
9393
state = 'handle-start';
9494
break;
9595
case 'handle-start':
96-
if (/Assertion .+ failed/.test(line)) {
97-
state = 'done';
96+
if (/^uv loop at \[.+\] has \d+ open handles in total$/.test(line)) {
97+
state = 'assertion-failure';
9898
break;
9999
}
100-
assert(/^\[.+\] timer$/.test(line), line);
100+
assert(/^\[.+\] timer( \(active\))?$/.test(line), line);
101101
state = 'close-callback';
102102
break;
103103
case 'close-callback':
@@ -109,15 +109,19 @@ if (process.argv[2] === 'child') {
109109
state = 'maybe-first-field';
110110
break;
111111
case 'maybe-first-field':
112-
if (/^\(First field\)$/.test(line)) {
112+
if (!/^\(First field\)/.test(line)) {
113113
lines.unshift(line);
114-
state = 'handle-start';
115-
break;
116114
}
117-
state = 'maybe-first-field';
115+
state = 'handle-start';
116+
break;
117+
case 'assertion-failure':
118+
assert(/Assertion .+ failed/.test(line), line);
119+
state = 'done';
118120
break;
119121
case 'done':
120122
break;
121123
}
122124
}
125+
126+
assert.strictEqual(state, 'done');
123127
}

0 commit comments

Comments
 (0)