Skip to content
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

async_wrap: clear destroy_ids vector #10400

Merged
merged 1 commit into from
Dec 22, 2016
Merged
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
6 changes: 5 additions & 1 deletion src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t> 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());
Expand All @@ -212,6 +214,8 @@ void AsyncWrap::DestroyIdsCb(uv_idle_t* handle) {
FatalException(env->isolate(), try_catch);
}
}

env->destroy_ids_list()->clear();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I suggest you swap before iteration? I.e.:

std::vector<int64_t> destroy_ids_list;
destroy_ids_list.swap(*env->destroy_ids_list());
for (auto current_id : destroy_ids_list) {
  // ...
}

That way the list is both cleared and immune to concurrent modification by the callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}


Expand Down
14 changes: 13 additions & 1 deletion test/parallel/test-async-wrap-uid.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ 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) {
storage.set(uid, {
init: true,
pre: false,
post: false,
destroy: false,
});
}

Expand All @@ -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);
});
Expand All @@ -46,6 +57,7 @@ process.once('exit', function() {
init: true,
pre: true,
post: true,
destroy: true,
});
}
});