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

Fix nasty race condition in AMP video #27740

Merged
merged 7 commits into from
Apr 15, 2020
Merged

Conversation

cramforce
Copy link
Member

@cramforce cramforce commented Apr 14, 2020

Under this scenario a video might not fall back to the origin on pre-render.
Possible fix for #27109

Reproduces by navigating (locally) to page with a video on page one in prerender mode,
waiting for the prerender load to fail, and then executing AMP.viewer.receiveMessage('visibilitychange', {'state': 'visible'}) in the console.

If this if not a fix for #27109, then it might be the same race, but elsewhere. The problem is that you cannot wait for loadPromise on a video element that has failed a source and has just had a new source provided, because currentSrc hasn't been updated yet.

There might be a more elegant fix. Lots of fixes that I thought should have worked, didn't :)

Other changes

  • Preconnects to all sources of a video to improve performance.
  • Documents that that is safe.

Under this scenario a video might not fall back to the origin on pre-render.
Possible fix for ampproject#27109

Reproduces by navigating (locally to)
http://localhost:8000/proxy/s/stories.nonblocking.io/waffles/?usqp=mq331AQFKAGwASA%3D&amp_js_v=0.1#referrer=https%3A%2F%2Fwww.google.com&visibilityState=prerender&cap=swipe&ampshare=https%3A%2F%2Funumbox.com%2Fwebsite-development-trends-2020-amp-story%2F
waiting for the pre-render load to fail, and then executing `AMP.viewer.receiveMessage('visibilitychange', {'state': 'visible'})` in the console.

If this if not a fix for ampproject#27109, then it might be the same race, but elsewhere. The problem is that you cannot wait for `loadPromise` on a video element that has failed a source and has just had a new source provided.
Copy link
Contributor

@gmajoulet gmajoulet left a comment

Choose a reason for hiding this comment

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

Wow, great catch! (#27109 is yet another different story specific bug 🤦)

If I understand correctly:

  • If the load from the cache succeeds, layoutCallback resolves right away
  • If the load from the cache fails, it'll wait for the ampdoc to get visible, and layoutCallback will resolve/reject based on these new sources

Did you consider the alternative of never resolving the layoutCallback during prerender (even if the cached source loads), but always wait for the non cached sources to be applied before getting the loadPromise?
I don't know if it's better, but there's value in keeping the layoutCallback behavior consistent across cached and non cached prerendering, and that'd allow us to get rid of the timer.

},
(reason) => {
if (pendingOriginPromise) {
this.video_[MEDIA_LOAD_FAILURE_SRC_PROPERTY] = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to keep this property set, in case the origin source also 404'd.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

.whenFirstVisible()
.then(() => {
this.propagateLayoutChildren_();
// We need to yield to the event queue before listing for loadPromise
// because this element may still be in error state from the pre-render
// load.
Copy link
Contributor

Choose a reason for hiding this comment

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

My kingdom for a way to know for sure when the media element ran its media selection process...

- Change preload to also preload non-cache sources
- Clarify preconnect docs about the security invariants.
@cramforce
Copy link
Member Author

While some simplification might be possible the general order of layoutCallback has to stay the way it is. You cannot call loadPromise on a failed <video> in the same micro-task as when new sources have been added. Thus we continue to need the timer.

So, while I was fixing the tests, I noticed we has special logic to only preconnect to the cache. However, that is not needed. We never preconnect to anything in prerender phase. I changed it such that we preconnect to all sources which should make fallbacks faster.

While I was at this, I also clarified our docs about these security invariants.

Copy link
Contributor

@gmajoulet gmajoulet left a comment

Choose a reason for hiding this comment

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

LGTM, pending the VideoEvents.LOAD event comment

},
(reason) => {
if (pendingOriginPromise) {
return pendingOriginPromise;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this one succeeds, we still want to dispatch this.element.dispatchCustomEvent(VideoEvents.LOAD);

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Moved the success handler one further down the chain.

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.

4 participants