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

Fixed some details related to the event loop #1603

Merged
merged 1 commit into from
Apr 25, 2018
Merged

Conversation

dmcghan
Copy link
Contributor

@dmcghan dmcghan commented Mar 26, 2018

No description provided.

@dmcghan dmcghan mentioned this pull request Mar 26, 2018
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

LGTM

@vsemozhetbyt
Copy link
Contributor

cc @nodejs/documentation and maybe @nodejs/libuv.

@vsemozhetbyt vsemozhetbyt added the content Issues/pr concerning content label Mar 26, 2018
@gibfahn
Copy link
Member

gibfahn commented Mar 27, 2018

cc/ @Fishrock123

@@ -76,12 +76,14 @@ actually uses - are those above._

* **timers**: this phase executes callbacks scheduled by `setTimeout()`
and `setInterval()`.
* **I/O callbacks**: executes almost all callbacks with the exception of
close callbacks, the ones scheduled by timers, and `setImmediate()`.
* **pending callbacks**: executes I/O callbacks deferred to the next loop
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, I/O is more appropriate than pending here, 'cause close callbacks wouldn't be executed here, so I/O makes more sense in this phase.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marswong Please keep in mind that I'm not a C programmer, so most of my claims are based on libuv's event loop overview. Perhaps you could point to source code where Node.js has deviated or where the libuv doc is off.

I/O alone seems to indicate this is where all or most I/O callbacks are executed. However, this is the responsibility of the poll phase. Only some callbacks that were deliberately delayed will be executed in this phase (hence "pending").

Originally, I suggested renaming this phase to pending I/O callbacks, but that seemed a little long (especially for the ascii art), so I shortened it to pending callbacks and used the brief description to elaborate. As a reference, libuv calls this phase call pending callbacks.

Copy link
Contributor

Choose a reason for hiding this comment

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

good point :)

Choose a reason for hiding this comment

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

More clear, thx.

* **check**: `setImmediate()` callbacks are invoked here.
* **close callbacks**: e.g. `socket.on('close', ...)`.
* **close callbacks**: some close callbacks, e.g. `socket.on('close', ...)`.
Copy link
Contributor

Choose a reason for hiding this comment

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

with or without "some close callbacks" are both ok :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My worry is that someone would never read the detail and assume that all close callbacks are executed in this phase. Here's what the detail says:

If a socket or handle is closed abruptly (e.g. socket.destroy()), the 'close' event will be emitted in this phase. Otherwise it will be emitted via process.nextTick().

So it's actually via process.nextTick() (or after any phase) where most close callbacks will be executed. Adding "some close callbacks" should lead folks to read the details if they want to know more.


### poll

The **poll** phase has two main functions:

1. Executing scripts for timers whose threshold has elapsed, then
1. Calculating how long it should block and poll for I/O, then
Copy link
Contributor

Choose a reason for hiding this comment

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

Calculating doesn't cover the meaning of Executing here, and the main purpose of poll phase is to execute callbacks. So it'd be better to remain the same here or just modify Executing scripts to be Executing callbacks :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calculating doesn't cover the meaning of Executing here

Agreed, I believe it was a mistake to put "executing" here. As far as I know, it's the responsibility of the timers and check phases to execute timer callbacks - depending on whether the timer was scheduled with setTimeout(), setInterval(), or setImmediate().

Please see #7 and #8 of the libuv overview for more detail. Perhaps this is an area where Node.js deviates from what libuv does by default?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, Node.js eventloop might differ a little from libuv's I/O loop, we set a UV_RUN_ONCE mode to make sure at least one callback must be invoked when it returns. Maybe @tniessen could give a more detailed explanation, thx :)

@fhemberger
Copy link
Contributor

Is something still missing or are we good to go? Would like to merge this PR.

@gibfahn
Copy link
Member

gibfahn commented Apr 15, 2018

Is something still missing or are we good to go? Would like to merge this PR.

I don't think anything is missing. It would be good to have some more reviews from people who have worked on this (ping @bnoordhuis and @Fishrock123, not sure who else), but otherwise I think this can land.

@fhemberger
Copy link
Contributor

Ok, as we didn't get any further reviews, I'm merging this now. If there are still any additions, we can handle them in another PR.

@dmcghan Thank you very much for this update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content Issues/pr concerning content
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants