From d1843ec3a79030157dd205331947c66199ba30fc Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 21 Dec 2016 16:05:37 -0700 Subject: [PATCH] async_wrap: clear destroy_ids vector After processing all the callbacks in the destroy_ids vector make sure to clear() it otherwise the DestroyIdsCb() won't run again. PR-URL: https://github.com/nodejs/node/pull/10400 Fixes: b49b496 "async_wrap: call destroy() callback in uv_idle_t" Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen --- src/async-wrap.cc | 6 +++++- test/parallel/test-async-wrap-uid.js | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/async-wrap.cc b/src/async-wrap.cc index 42463bd22b31f4..a0780566db72d8 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -199,7 +199,9 @@ void AsyncWrap::DestroyIdsCb(uv_idle_t* handle) { TryCatch try_catch(env->isolate()); - for (auto current_id : *env->destroy_ids_list()) { + std::vector destroy_ids_list; + destroy_ids_list.swap(*env->destroy_ids_list()); + for (auto current_id : destroy_ids_list) { // Want each callback to be cleaned up after itself, instead of cleaning // them all up after the while() loop completes. HandleScope scope(env->isolate()); @@ -212,6 +214,8 @@ void AsyncWrap::DestroyIdsCb(uv_idle_t* handle) { FatalException(env->isolate(), try_catch); } } + + env->destroy_ids_list()->clear(); } diff --git a/test/parallel/test-async-wrap-uid.js b/test/parallel/test-async-wrap-uid.js index 3497c3b0768ddd..19cb01cfa83c7a 100644 --- a/test/parallel/test-async-wrap-uid.js +++ b/test/parallel/test-async-wrap-uid.js @@ -5,8 +5,14 @@ const fs = require('fs'); const assert = require('assert'); const async_wrap = process.binding('async_wrap'); +// Give the event loop time to clear out the final uv_close(). +var si_cntr = 3; +process.on('beforeExit', () => { + if (--si_cntr > 0) setImmediate(() => {}); +}); + const storage = new Map(); -async_wrap.setupHooks({ init, pre, post }); +async_wrap.setupHooks({ init, pre, post, destroy }); async_wrap.enable(); function init(uid) { @@ -14,6 +20,7 @@ function init(uid) { init: true, pre: false, post: false, + destroy: false, }); } @@ -25,6 +32,10 @@ function post(uid) { storage.get(uid).post = true; } +function destroy(uid) { + storage.get(uid).destroy = true; +} + fs.access(__filename, function(err) { assert.ifError(err); }); @@ -46,6 +57,7 @@ process.once('exit', function() { init: true, pre: true, post: true, + destroy: true, }); } });