Skip to content
This repository has been archived by the owner on Mar 20, 2021. It is now read-only.

Add sequence ID to emit calls to prevent race #32

Merged
merged 2 commits into from
Mar 17, 2014
Merged

Add sequence ID to emit calls to prevent race #32

merged 2 commits into from
Mar 17, 2014

Conversation

kpdecker
Copy link
Contributor

If a page calls emit multiple times it's possible that the pool output may be
preemptively emitted on subsequent requests if they occur within a short
enough period. Adding a unique sequence identified to NOP any subsequent
operations.

@@ -41,7 +42,14 @@ module.exports = exports = function(options) {
emit: function(after) {
function checkComplete() {
if (isComplete()) {
setImmediate(emit);
var associatedRequest = requestId;

Choose a reason for hiding this comment

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

I was thinking that the emit event meant that the page was complete and ready to be transferred to the client. Am I misunderstanding something there? If not, why are we performing any action once the initial emit has been triggered? And, what happens in that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emit has two forms of deferred operations, ajax and events, in addition to the "immediate" mode. Under these modes the actual emit might occur far later than the original call due to pending events. Calling emit multiple times generally shouldn't have any effect other than moving the emitter to the most restrictive mode, i.e. events -> ajax -> "immediate". It's also possible for multiple emit() calls to occur prior to actually outputting data as the actual emit logic is run in a setImmediate call and the event continues executing as well as any queued immediate events.

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: associatedRequestId

@jhudson8
Copy link

outside of my probably stupid question, +1

setImmediate(function() {
// Avoid a race condtion where pooled requests may come in while we still have
// pending emit calls. This will generally only happen for very chatty emit callers.
if (requestId === associatedRequest) {
Copy link
Contributor

Choose a reason for hiding this comment

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

So calling page.navigate() will NOP a pending emit request?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. Navigate should stop everything dead.

If a page calls emit multiple times it's possible that the pool output may be preemptively emitted on subsequent requests if they occur within a short enough period. Adding a unique sequence identified to NOP any subsequent operations.
kpdecker added a commit that referenced this pull request Mar 17, 2014
Add sequence ID to emit calls to prevent race
@kpdecker kpdecker merged commit ad74b44 into master Mar 17, 2014
@kpdecker kpdecker deleted the emit-race branch March 17, 2014 20:04
@kpdecker
Copy link
Contributor Author

Released in 0.3.0

@kpdecker kpdecker modified the milestone: 1.0.0 Feb 13, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants