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

Return a "controller" instance from fetch #1329

Merged
merged 16 commits into from Mar 14, 2022
Merged

Conversation

noamr
Copy link
Contributor

@noamr noamr commented Oct 12, 2021

Use the controller for external timing reporting and aborts.

This allows removing the 1:1 dependency between timing info and a response, as a response can be shared across fetch
instances (e.g. when restoring a response from cache, or when forwarding a response through a service worker).

See #1208

  • At least two implementers are interested (and none opposed):

    • Should reflect current implementation.
  • Tests are written and can be reviewed and commented upon at:

  • Implementation bugs are filed:

    • Chrome: …
    • Firefox: …
    • Safari: …

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

fetch.bs Outdated Show resolved Hide resolved
@noamr
Copy link
Contributor Author

noamr commented Oct 19, 2021

@annevk i think this is ready for another review, thanks!

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

This good, thanks for working on this! I suggest we wait a bit with landing this until @jakearchibald is back home (next week iirc) as he worked on this last and might have some relevant insights.

fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

\o/ looks even better. I'll file a follow-up on where we should define fetch params and such.

This PR should also update the "Invoking fetch" section with a few words about the return value of fetch.

fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
Copy link
Collaborator

@jakearchibald jakearchibald left a comment

Choose a reason for hiding this comment

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

Just a few nits really.

fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
@@ -2247,14 +2278,14 @@ functionality.

<p>A <a for="fetch group">fetch record</a> has an associated
<dfn export for="fetch record" id=concept-fetch-record-fetch>fetch</dfn> (a
<a for=/>fetch</a> algorithm or null).
<a for=/>fetch controller</a> or null).
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can't see any cases where it's null.

Copy link
Member

Choose a reason for hiding this comment

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

I suggest we tackle this in a follow-up.

Copy link
Member

Choose a reason for hiding this comment

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

Can you file an issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@noamr noamr left a comment

Choose a reason for hiding this comment

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

Fixed the detailed CR comments.

fetch.bs Outdated Show resolved Hide resolved
@annevk
Copy link
Member

annevk commented Nov 3, 2021

As noted on Matrix we need to account for service workers and in particular "handle fetch". Either we make it so that "handle fetch" returns a controller for the service worker fetch event, or we pass the controller into "handle fetch" so the service worker can observe it. The former seems a bit nicer as it reuses the pattern introduced here and could be used for other kinds of information exchange in the future. It would require "handle fetch" to take a callback.

@yutakahirano @jakearchibald @noamr thoughts?

@noamr
Copy link
Contributor Author

noamr commented Nov 3, 2021

As noted on Matrix we need to account for service workers and in particular "handle fetch". Either we make it so that "handle fetch" returns a controller for the service worker fetch event, or we pass the controller into "handle fetch" so the service worker can observe it. The former seems a bit nicer as it reuses the pattern introduced here and could be used for other kinds of information exchange in the future. It would require "handle fetch" to take a callback.

@yutakahirano @jakearchibald @noamr thoughts?

I like the idea of returning a controller from SW's fetch rather than having SW query its caller fetch for "is terminated"...
Does it affect this PR?

@annevk
Copy link
Member

annevk commented Nov 3, 2021

Yeah, I think we need to tackle it as otherwise fetch termination in service workers ends up in UB territory.

@noamr
Copy link
Contributor Author

noamr commented Nov 3, 2021

Yeah, I think we need to tackle it as otherwise fetch termination in service workers ends up in UB territory.

It is currently in undefined behavior territory, this doesn't change much about it. can't we fix this iteratively?

noamr added a commit to noamr/ServiceWorker that referenced this pull request Nov 8, 2021
In conjunction with whatwg/fetch#1329.

When the SW fetch starts, it emits an `onStart` callback with a controller
instance. That controller instance can be used to terminate the service worker
fetch and it's internal fetches.
noamr added a commit to noamr/ServiceWorker that referenced this pull request Nov 8, 2021
In conjunction with whatwg/fetch#1329.

When the SW fetch starts, it emits an `onStart` callback with a controller
instance. That controller instance can be used to terminate the service worker
fetch and it's internal fetches.
@noamr
Copy link
Contributor Author

noamr commented Nov 8, 2021

As noted on Matrix we need to account for service workers and in particular "handle fetch". Either we make it so that "handle fetch" returns a controller for the service worker fetch event, or we pass the controller into "handle fetch" so the service worker can observe it. The former seems a bit nicer as it reuses the pattern introduced here and could be used for other kinds of information exchange in the future. It would require "handle fetch" to take a callback.

@yutakahirano @jakearchibald @noamr thoughts?

See w3c/ServiceWorker#1612 and latest revision of this PR.

  • Instead of returning a controller, we emit it in a new onFetchStart callback
  • Termination of the service worker and of fetches initiated by the service worker are done explicitly

(It currently fails to build because of the dependency on the SW patch)

fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

Are you working on a new service worker PR? I think the Fetch side looks good, though I'd like to do another detailed read once we have the full story. @jakearchibald what do you think?

fetch.bs Outdated Show resolved Hide resolved
@noamr
Copy link
Contributor Author

noamr commented Nov 19, 2021

@annevk I was working on the corresponding SW PR: #1329 and I see that it's closed for some reason, I must have closed it by mistake and I don't seem to be able to re-open. Opened a new one: w3c/ServiceWorker#1620

fetch.bs Outdated Show resolved Hide resolved
@@ -2247,14 +2278,14 @@ functionality.

<p>A <a for="fetch group">fetch record</a> has an associated
<dfn export for="fetch record" id=concept-fetch-record-fetch>fetch</dfn> (a
<a for=/>fetch</a> algorithm or null).
<a for=/>fetch controller</a> or null).
Copy link
Member

Choose a reason for hiding this comment

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

I suggest we tackle this in a follow-up.

@annevk
Copy link
Member

annevk commented Dec 3, 2021

@jakearchibald @yutakahirano could you please review this PR as well as w3c/ServiceWorker#1620?

@noamr
Copy link
Contributor Author

noamr commented Feb 7, 2022

@annevk ping? The corresponding service-workers PR has already been merged.

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

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

Found a couple minor remaining issues. Also pinging @jakearchibald and @yutakahirano again for review.

fetch.bs Outdated Show resolved Hide resolved
@@ -2247,14 +2278,14 @@ functionality.

<p>A <a for="fetch group">fetch record</a> has an associated
<dfn export for="fetch record" id=concept-fetch-record-fetch>fetch</dfn> (a
<a for=/>fetch</a> algorithm or null).
<a for=/>fetch controller</a> or null).
Copy link
Member

Choose a reason for hiding this comment

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

Can you file an issue for this?

fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
fetch.bs Outdated Show resolved Hide resolved
noamr and others added 4 commits February 14, 2022 15:37
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
@noamr
Copy link
Contributor Author

noamr commented Feb 28, 2022

Found a couple minor remaining issues. Also pinging @jakearchibald and @yutakahirano again for review.

@jakearchibald ping?

Copy link
Collaborator

@jakearchibald jakearchibald left a comment

Choose a reason for hiding this comment

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

LGTM. One comment, but it can be a followup


<dl>
<dt><dfn for="fetch controller">state</dfn> (default "<code>ongoing</code>")
<dd>"<code>ongoing</code>", "<code>terminated</code>", or "<code>aborted</code>"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should there be a state that represents a fetch that can no longer be aborted or terminated? I guess that would be "complete".

It seems weird that a fully completed fetch would have a state of "ongoing".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! I will add it when "Finalize and report timing" would move to the fetch controller, otherwise no one would set that value :)

@annevk
Copy link
Member

annevk commented Mar 11, 2022

This looks good to me too. One thing we'll need is a tentative commit message that summarizes the changes and links the various follow-up issues, the resolved issues, and the related service worker PR. Is that something you can put together @noamr? In a new comment would be best.

@noamr
Copy link
Contributor Author

noamr commented Mar 11, 2022

This looks good to me too. One thing we'll need is a tentative commit message that summarizes the changes and links the various follow-up issues, the resolved issues, and the related service worker PR. Is that something you can put together @noamr? In a new comment would be best.

Sure, I will post follow up issues and comment with a commit message.

@noamr noamr mentioned this pull request Mar 13, 2022
4 tasks
@noamr
Copy link
Contributor Author

noamr commented Mar 13, 2022

Commit message:

Return a "controller" instance from the fetch algorithm, which allows clients of fetch to perform certain actions on an ongoing fetch, initially to abort or terminate it, and later on to conclude it.

This will eventually remove the need of using response as the object that retains timing info, as that creates certain ambiguities.

Followup action items:
#1412

@annevk annevk merged commit 4561d5f into whatwg:main Mar 14, 2022
@noamr noamr deleted the controller branch March 14, 2022 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants