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
Conversation
// 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
/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(() => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
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>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space before {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add bang !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
LGTM |
* is the value. | ||
* @private{Object.<string, string>} | ||
*/ | ||
this.externalCidCache_ = {}; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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.