Skip to content

🐛Fix variable service race.#22375

Merged
calebcordry merged 1 commit into
ampproject:masterfrom
calebcordry:var-async-bug
May 21, 2019
Merged

🐛Fix variable service race.#22375
calebcordry merged 1 commit into
ampproject:masterfrom
calebcordry:var-async-bug

Conversation

@calebcordry
Copy link
Copy Markdown
Member

@calebcordry calebcordry commented May 18, 2019

Introduced another bug in #22300. When loading new amp docs into shadow shell shell service registration is asynchronous, but I was trying to retrieve the service synchronously. This changes amp-analytics to wait for a promise of this service.

All other analytics files can continue to use the sync method as their creating analytics element will have waited for promise to resolve.

@calebcordry calebcordry marked this pull request as ready for review May 20, 2019 16:15
@calebcordry
Copy link
Copy Markdown
Member Author

cc/ @mattwomple I want to make sure you have a change to test this time.

@mdmower
Copy link
Copy Markdown
Contributor

mdmower commented May 20, 2019

Thanks @calebcordry. Sorry I couldn't review over the weekend, but I expect to be able to complete a review before 12:00 PM PDT if that works for you. If not, I'll still leave a review after merge.

Edit: Ah, my phone is logged into my personal GitHub account. This is @mattwomple.

@calebcordry
Copy link
Copy Markdown
Member Author

@mattwomple no worries, I wasn't expecting you to work on weekend. 12pm is fine, I would just like to get this in sometime today.

Copy link
Copy Markdown
Contributor

@lannka lannka left a comment

Choose a reason for hiding this comment

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

this is something new to me.

@choumx is this issue only from doc level services registered in extension? Or does it affect all doc leve services? I do see many sync getters out there all over our code base.

* @param {!Element|!ShadowRoot|!../../../src/service/ampdoc-impl.AmpDoc} elementOrAmpDoc
* @return {!Promise<!VariableService>}
*/
export function variableServicePromiseForDoc(elementOrAmpDoc) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we still need the other sync method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The other classes created by amp-analytics (linker, cookie-writer, requests etc) are still using the sync method. This is ok because they get created after amp-analytics resolves the promise. I thought about changing them all but I think it would make the code harder to follow in these other files.

@mattwomple
Copy link
Copy Markdown
Member

@calebcordry This commit appears to resolve the issues I've seen with moving amp-analytics variables to the document-level. I largely tested this from a functionality and regression standpoint with the following page that incorporates amp-bind and amp-analytics in a PWA: bind-analytics-pwa.html (and associated AMP pages -amp1.html and -amp2.html). These files can be dropped into the examples/ folder for local testing. It may be worth converting them to tests in the future. Thanks for your work on this.

LGTM

@calebcordry
Copy link
Copy Markdown
Member Author

Not sure why the tests are stalled. Rebasing to re-trigger.

@zhouyx
Copy link
Copy Markdown
Contributor

zhouyx commented May 21, 2019

I found the the comment for a similar case

/**
* It's important to resolve instrumentation asynchronously in elements that
* depends on it in multi-doc scope. Otherwise an element life-cycle could
* resolve way before we have the service available.

Looks like the immediate fix is to asynchronously resolve doc level services that are registered upon element registration. If that's the case should we apply the same to Activity service as well?

AMP.extension(TAG, '0.1', AMP => {
// Register doc-service factory.
AMP.registerServiceForDoc(
'amp-analytics-instrumentation',
InstrumentationService
);
AMP.registerServiceForDoc('activity', Activity);
installLinkerReaderService(AMP.win);
AMP.registerServiceForDoc('amp-analytics-variables', VariableService);
// Register the element.
AMP.registerElement(TAG, AmpAnalytics);
});

What about other components? Do you think this is a race condition that should be fixed by the shadow doc code instead? It would be great if you could point me to where services are registered for individual doc. Thank you.

@calebcordry
Copy link
Copy Markdown
Member Author

calebcordry commented May 21, 2019

Looks like the immediate fix is to asynchronously resolve doc level services that are registered upon element registration. If that's the case should we apply the same to Activity service as well?

The only getter I see for the activity service is here https://github.com/ampproject/amphtml/blob/master/src/services.js#L120 and it looks like its already returning a promise. Let me know if you see another that is potentially problematic.

What about other components? Do you think this is a race condition that should be fixed by the shadow doc code instead?

Maybe, I was chatting with @jridgewell earlier today about this, and I think he is looking into it. @jridgewell could you provide @zhouyx with a code pointer here?

@calebcordry
Copy link
Copy Markdown
Member Author

@zhouyx merging this to make canary cut. Let me know if you want me to open an issue around shadow behavior and we can continue discussion there.

@calebcordry calebcordry merged commit aa327ef into ampproject:master May 21, 2019
@calebcordry calebcordry deleted the var-async-bug branch May 21, 2019 14:58
@zhouyx
Copy link
Copy Markdown
Contributor

zhouyx commented May 21, 2019

That'd be great. Thanks. My concern is that I expect it's common to encounter this race condition and there could be existing bugs with other components already.

@calebcordry
Copy link
Copy Markdown
Member Author

@zhouyx I agree. Opened #22414

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants