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

src: return early if nextTickQueue is empty #10274

Merged
merged 1 commit into from
Dec 21, 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
1 change: 1 addition & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ Local<Value> MakeCallback(Environment* env,

if (tick_info->length() == 0) {
tick_info->set_index(0);
return ret;
}

if (env->tick_callback_function()->Call(process, 0, nullptr).IsEmpty()) {
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-no-enter-tickcallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const common = require('../common');
const assert = require('assert');
var allsGood = false;
var cntr = 0;

process.on('exit', () => {
assert.ok(cntr > 0, '_tickDomainCallback was never called');
});
Copy link
Member

Choose a reason for hiding this comment

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

If you think being stricter is okay, we could just wrap the fake _tickDomainCallback in common.mustCall(·, 2) (You can probably judge whether the number of calls is likely to change).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@addaleax Though on my machine it would be common.mustCall(., 3). Because we can't actually process the nextTickQueue when overriding _tickDomainCallback() we can only guarantee it will be called at least once. I don't feel that being any stricter than that is possible w/o making some shaky assumptions on how the internals work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Likewise, adding mustCall() to process.on('exit' is pointless since mustCall callbacks are checked during 'exit'.

Copy link
Member

Choose a reason for hiding this comment

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

Though on my machine it would be common.mustCall(., 3)

I think that settles it, thanks for checking!


/**
* This test relies upon the following internals to work as specified:
* - require('domain') causes node::Environment::set_tick_callback_function()
* to use process._tickDomainCallback() to process the nextTickQueue;
* replacing process._tickCallback().
* - setImmediate() uses node::MakeCallback() instead of
* node::AsyncWrap::MakeCallback(). Otherwise the test will always pass.
* Have not found a way to verify that node::MakeCallback() is used.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Could these conditions be checked by adding a test for the reverse situation? I.e. making sure that if there are ticks pending, the _tickDomainCallback is actually executed?

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like a good idea. @trevnorris?

Copy link
Member

Choose a reason for hiding this comment

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

@Fishrock123 In response to my suggestion @trevnorris added the second (nested) setImmediate call below (which seems sufficient to me)

process._tickDomainCallback = function _tickDomainCallback() {
assert.ok(allsGood, '_tickDomainCallback should not have been called');
cntr++;
};

setImmediate(common.mustCall(() => {
require('domain');
setImmediate(common.mustCall(() => setImmediate(common.mustCall(() => {
allsGood = true;
process.nextTick(() => {});
}))));
}));