-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Event loop execution order changes based on code part that didn't executed #27747
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
Comments
So: https://nodejs.org/api/process.html#process_a_note_on_process_i_o says:
IMHO, this is not a bug, its probably the sync/async discrepancy that's causing this. If you can provide a minimal reproachable example, we can investigate this further. |
What exactly is the windows result? You don't seem to have posted it? Nothing there seems unusual to me so far. |
@Fishrock123 Please read my initial message carefully. Providing code execution results in screenshots. Pay attention at 23 code line and |
Do you mean that the issue can be connected with
With joy, but I do not understand what |
Why are you uncommenting the Immediate? The Immediate should always run before timers. I am still unsure of what windows difference there is since there does not appear to be any other than what you modified between runs? |
Pretty sure they meant |
@Fishrock123 Without the Immediate, execution of the seemingly-unrelated Timeout is nearly a full second slower. Without scheduling the Immediate, the 1000ms Timeout reports firing after 2003ms. When the Immediate is scheduled, it fires after only 1007ms, much closer to the expected time, but why would scheduling an unrelated Immediate cause that? IIUC, @bzoz is suggesting that it might have to do with async vs sync i/o, which certainly seems plausible (although surprising to me in terms of the magnitude of its impact). I'm unable to replicate this on macOS using the sample code provided in the description. So it would seem to be Windows-specific (although confirmation from another Windows user that they see the same results with the sample code would be helpful). @davNazaryan If you change the Immediate to do nothing at all (rather than invoking |
Oh... did I really not see that... well, at least that seems wrong. |
Thanks to @Trott for a clear explanation, and sorry for my English.
There are 2 versions of Node, Windows and POSIX. I can't reproduce the issue on Ubuntu. So, Yes, you're right, it's Windows-specific issue.
Commenting I provided 3 versions of the fully reproducible code with their results. All you need to get the same results is
const fs = require('fs');
function someAsyncOperation(callback) {
fs.readFile('/path/to/file', callback);
}
const timeoutScheduled = Date.now();
setTimeout(() => {
const delay = Date.now() - timeoutScheduled;
console.log(`${delay}ms have passed since I was scheduled`);
}, 1000);
someAsyncOperation(() => {
console.log('BEFORE WHILE');
const startCallback = Date.now();
/*let i = 0;*/
while (Date.now() - startCallback < 1000) {
/*console.log(i++);*/
}
/*setImmediate(() => { console.log('IMMEDIATE'); });*/
console.log('AFTER WHILE');
});
const fs = require('fs');
function someAsyncOperation(callback) {
fs.readFile('/path/to/file', callback);
}
const timeoutScheduled = Date.now();
setTimeout(() => {
const delay = Date.now() - timeoutScheduled;
console.log(`${delay}ms have passed since I was scheduled`);
}, 1000);
someAsyncOperation(() => {
console.log('BEFORE WHILE');
const startCallback = Date.now();
/*let i = 0;*/
while (Date.now() - startCallback < 1000) {
/*console.log(i++);*/
}
setImmediate(() => { console.log('IMMEDIATE'); });
console.log('AFTER WHILE');
});
const fs = require('fs');
function someAsyncOperation(callback) {
fs.readFile('/path/to/file', callback);
}
const timeoutScheduled = Date.now();
setTimeout(() => {
const delay = Date.now() - timeoutScheduled;
console.log(`${delay}ms have passed since I was scheduled`);
}, 1000);
someAsyncOperation(() => {
console.log('BEFORE WHILE');
const startCallback = Date.now();
/*let i = 0;*/
while (Date.now() - startCallback < 1000) {
/*console.log(i++);*/
}
setImmediate(() => { /*console.log('IMMEDIATE');*/ });
console.log('AFTER WHILE');
}); |
I can reproduce on Windows with Node.js 10.15.3. |
on node v10.15.3 , win10 |
While I don't believe this was ever actually resolved, there will not be another 10.x release before it hits end of life in a few days. Closing the issue as it appears to only be relevant to 10.x |
No need to init new project. Only the built-in
fs
module used there.I have a code that works as designed in Unix based OS, but in windows, it gives the "strange" result.
Code
The output is
The output is the same independently of how many timers are present. ( it's always 2000+ )
Which is weird. I guess that such a result is possible if the
setTimeout
( I mean thesetTimeout
itself, not thesetTimeout
's callback ) registered AFTERwhile
loop work.Pay attention at
// setImmediate(() => { console.log('IMMEDIATE'); });
line.If I uncomment it the output will be
In Unix based system the output always {1000+}ms, and doesn't depend on
setImmediate
existence.Can someone explain why it happens, and give event loop execution order?
It's maybe a BUG, but I'm not sure.
Thanks in advance.
Update: This message is copied from nodejs/help#1912
The text was updated successfully, but these errors were encountered: