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

Changed cid-impl to handle a racy condition. #2923

Merged
merged 1 commit into from Apr 15, 2016

Conversation

avimehta
Copy link
Contributor

When the cid is requested twice in a row and no cookie is present, the
cid in the first call is different from the seconed one.

// The initial CID generation is inherently racy. First one that gets
// consent wins.
const relookup = getCookie(win, scope);
if (!relookup) {
setCidCookie(win, scope, newCookie);
}
return Promise.resolve(relookup || newCookie);
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to .resolve, just retuning will do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ack. Letting the tests run to make sure nothing is broken. Will fix it after that.

@avimehta
Copy link
Contributor Author

/cc @cramforce

@@ -182,15 +182,15 @@ function getOrCreateCookie(cid, getCidStruct, persistenceConsent) {
const newCookie = 'amp-' + cid.sha384Base64_(getEntropy(win));

// Store it as a cookie based on the persistence consent.
persistenceConsent.then(() => {
return persistenceConsent.then(() => {
Copy link
Member

Choose a reason for hiding this comment

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

This will not work. These promises have a cyclic dep.

The real fix is to use the baseCid_ instance var to short circuit the second entry into this function without reading cookies.

Copy link
Member

Choose a reason for hiding this comment

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

Can we add a line before
this.baseCid_ = newCookie

and the same for localStorage.

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.

@avimehta
Copy link
Contributor Author

ptal. I also added the test cases for the bug.

/**
* Cache to store external cids. Scope is used as the key and cookie value
* is the value.
* @private{Object.<string, string>}
Copy link
Member

Choose a reason for hiding this comment

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

Space before {

Copy link
Member

Choose a reason for hiding this comment

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

also add bang !

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.

@cramforce
Copy link
Member

LGTM

* is the value.
* @private{Object.<string, string>}
*/
this.externalCidCache_ = {};
Copy link
Member

Choose a reason for hiding this comment

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

use Object.create(null) instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

didn't know about this. Thanks!

When the cid is requested twice in a row and no cookie is present, the
cid in the first call is different from the seconed one.
@avimehta avimehta merged commit bd40073 into ampproject:master Apr 15, 2016
@avimehta avimehta deleted the cid1 branch April 15, 2016 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants