-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Aborting fetch #6484
Aborting fetch #6484
Conversation
fetch/api/abort/general.js
Outdated
|
||
assert_false(request.bodyUsed, "Body has not been used"); | ||
assert_equals(await request.text(), 'foo', "Correct body"); | ||
}, "Request is not 'used' if signal is aborted before fetching"); |
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.
@annevk I felt like the above was the expected behaviour, but seems like it'll be a pain to spec. What do you think?
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.
So the idea is that if the signal is aborted it's a no-op when passed to fetch()
? It seems like that should be fairly easy to specify (just don't pass it along), but I'm not sure it's the model we want.
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.
The signal is used, but the request isn't, as in the body isn't read.
My thinking is that the abort should happen straight away if the signal is already aborted. No request should be made to the server. Therefore, request.body
shouldn't be consumed/locked/disturbed.
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.
Oh, I missed the subtle distinction. I guess the Request
constructor (which is what fetch()
uses) can just bypass a whole bunch of things when passed an aborted signal?
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 is where it gets messy I think. Here are my gut feelings:
const request = new Request(url, { signal: abortedSignal, body });
const anotherRequest = new Request(request);
It feels like request
's body should be disturbed/locked, since the body is being explicitly moved from one request to another.
fetch(invalidURL, { signal: abortedSignal });
The above should reject based on the invalid URL, not aborted.
fetch(request, { signal: abortedSignal, body });
In the above, the body shouldn't be consumed, as it doesn't need to be sent to the server, as the signal is already aborted.
So, from the developer's point of view, the order is:
- Validate the args.
- Abort if already aborted.
- Make the request.
Locking/disturbing the body as part of "Validate the args" doesn't fit well with me, although I realise that's how it's specced now.
I guess fetch could undisturb/unlock the body if it's already aborted?
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.
So you basically want fetch()
to do something else than invoke the constructor. We could do that, but it'll be tricky to ensure it's correct and has no regressions. (I don't like undisturbing/unlocking. I'd rather not do the "damage" to begin with.)
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.
Given that mixed-content and CSP could be considered exiting early before using the body (yet they disturb/lock the body), I think I'm over complicating it by trying to make aborting different.
Happy to replace this with a test that ensures the body is locked.
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.
Yeah, it does seem like a lot of refactoring effort for unclear benefit. If we had other features that needed that kind of refactoring it might be worth looking into, but not sure.
fetch/api/abort/general.js
Outdated
|
||
// I'm hoping this will give the browser enough time to (incorrectly) make the request | ||
// above, if it intends to. | ||
await fetch('../resources/data.json').then(r => r.json()); |
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.
@wanderview Any better ideas than the hack above?
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.
Not really. Doing another operation that takes the same path is what we do other places in wpt to approximate this.
except socket.error: | ||
# This can happen if the socket got closed by the remote end | ||
pass | ||
return False |
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.
@annevk any idea who owns this?
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.
@jgraham should review 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.
@jgraham I'm using this to stop an infinite response, but also to track when/if a browser closes the connection related to an aborted fetch.
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.
@jgraham I want to use this in other tests too, eg to stop generating content when the client is no longer interested.
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.
I think this is fine.
fetch/api/abort/general.js
Outdated
const request = new Request(''); | ||
assert_true(Boolean(request.signal), "Signal member is present & truthy"); | ||
assert_equals(request.signal.constructor, AbortSignal); | ||
}, "Request objects have a signal property"); |
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.
@annevk what do you think about this? I'm undecided.
The idea is that requests will always have a signal. If you pass a signal to the fetch constructor, it'll listen to it & repeat the same action on its own signal.
I figured this would make things easier when it came to replacing this with fetchSignal
later, as you don't have to worry that request.signal
is one of a variety of types (or null).
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.
So instead of forwarding the signal it gets copied? It seems you might still get different types or shapes over time as we add more features, but copying rather than forwarding might be nice if you want to reuse the request... It's not clear if we should take care of that or whether there should be a utility to copy signals.
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 is what I was thinking: https://github.com/whatwg/fetch/pull/523/files#diff-e8c7e042697e1e48e4466c612dda6a3bR5006
Then, we can later change all request.signal
s to FetchSignal
s, and developers can rely on behaviour like "If self.FetchSignal
is supported, all requests will have one as .signal
".
fetch/api/abort/general.js
Outdated
} | ||
}, `Fetch aborted & connection closed when aborted after calling response.${bodyMethod}()`); | ||
} | ||
|
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.
@domenic I don't suppose you have time to check these aborting-streams tests?
fetch/api/abort/cache.https.html
Outdated
await new Promise(resolve => { | ||
signal.addEventListener('abort', resolve); | ||
controller.abort(); | ||
}); |
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.
What's the point of the promise here?
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.
I thought it queued a task, but it doesn't. Will fix.
fetch/api/abort/general.js
Outdated
|
||
// TODO: we may want to discuss this design idea | ||
assert_true(Boolean(request.signal), "Signal member is present & truthy"); | ||
assert_equals(request.signal.constructor, AbortSignal); |
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.
I hope folks won't do these kind of checks when branching...
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.
I could change it to instanceof
?
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.
It's fine as-is, I doubt anyone would copy from the tests.
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.
Though maybe instanceof
is better so we don't have to update this test and it'll keep passing in newer UAs.
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.
Won't we update it to instanceof FetchSignal
later?
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.
Yeah, I guess we might do that anyway. Okay, never mind.
fetch/api/abort/general.js
Outdated
return Promise.all( | ||
keys.map(key => fetch(`../resources/stash-put.py?key=${key}&value=close`)) | ||
); | ||
} |
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.
Move requestKeys and abortRequests done to where they are actually used? I guess I can kinda see they are global, but to me at least I kept wondering when they'd show up and when they did I had forgotten the setup.
I've written all the tests I intended to write. |
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.
In general the same comments everywhere: use promise_rejects instead of your custom assertion function, and consider also testing .closed.
@@ -0,0 +1 @@ | |||
<!DOCTYPE html> |
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.
You should be able to use /common/blank.html for this
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.
That means I'd have to install a service worker at that scope. Is that likely to interfere with other tests?
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.
I don't think tests run concurrently so if you clean up that could work, but it might be clearer to just add a comment as to why you duplicated and keep the scope as-is.
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.
R+ with added newline and the comment.
</head> | ||
<body> | ||
<script> | ||
function assert_abort_error(err) { |
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 promise_rejects(t, "AbortError", promise)
instead of this function.
}, `response.${bodyMethod}() rejects if already aborted`); | ||
} | ||
|
||
promise_test(async t => { |
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.
Apart from the promise_rejects issue, this looks good. You may also want to test reader.closed.
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.
Fixed the promise_rejects
stuff & added reader.closed
tests.
This is a high-level overview of the intent behind the controller, signal, & fetch abort design. Aborting a fetchFetching can involve setting up connections, making an HTTP request, receiving headers, and reading a response body. A single fetch can involve many iterations of these, through preflights and redirects. The aim is to make the process abortable during all of these phases. The spec should be explicit about how to react to an abort signal at various points during the process, including which points cannot be aborted. Abort controllers and abort signalsconst controller = new AbortController();
const signal = controller.signal;
const fetchPromise = fetch(url, {signal}); The feedback we got from developers was to avoid the As a result, we designed signal.addEventListener('abort', () => {
console.log('Abort signalled!');
});
controller.abort(); The controller gives you the ability to say "I would like to signal 'abort'". The signal broadcasts that intent. A single signal can be passed to many APIs – a bunch of fetches can be aborted by a single signal. The signal is just a signal, it doesn't reflect whether anything was actually aborted. For instance, you can signal abort long after the fetch has completed and been fully read. In this case the signal will be in the aborted state, but the fetch will have successfully completed. A signal may be The design here is deliberately generic, as streams and webauthn want this ability. In future,
|
I can't really figure out what Travis is complaining about. |
It's some intermittent issue with the Firefox bot (that also seems to be affecting the Chrome bot, but the Chrome bot doesn't count as blocking since it does that a lot). However, I can't really decipher the log either. I asked @jgraham to weigh in on IRC. |
w3c-test:mirror |
idk why the travis failure isn't being reported here correctly, but @bobholt is working on improved infrastructure there. Anyway the problem in this case is a legit bug in the tests that needs to be fixed. In the log there are lots of lines at the end like
The important point there is |
Thanks! Fixed now. |
} | ||
}); | ||
|
||
assert_true(!!cancelReason, 'Cancel called sync'); |
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 test makes sense. Can we not assert anything more specific about cancelReason than that it's truthy?
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.
Doh, that's supposed to be the next two lines. Copy & paste error.
fetch_tests_from_worker(new Worker("general.js")); | ||
if (window.SharedWorker) { | ||
fetch_tests_from_worker(new SharedWorker("general.js")); | ||
} |
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.
FWIW I found generating multiple tests with a wrapper simpler than contextualTestName: see https://github.com/w3c/web-platform-tests/tree/master/streams readme and https://github.com/w3c/web-platform-tests/blob/master/streams/generate-test-wrappers.js
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.
The portions I have been asked to comment on look good :)
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.
Couple nits, but I think this is good to go. I'm personally happy with this landing before the change to the Fetch Standard, but I'd like to hear from @wanderview and @youennf as well. We'll start implementing this a month from now or so, so there's no particular rush.
addEventListener('fetch', event => { | ||
event.waitUntil(broadcast(event.request.url)); | ||
event.respondWith(fetch(event.request)); | ||
}); |
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.
Add a newline here?
@@ -0,0 +1 @@ | |||
<!DOCTYPE html> |
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.
R+ with added newline and the comment.
}, "Stream errors once aborted."); | ||
</script> | ||
</body> | ||
</html> |
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.
Add newline.
} | ||
</script> | ||
</body> | ||
</html> |
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.
Add newline.
}, "Signals are not stored in the cache API, even if they're already aborted"); | ||
</script> | ||
</body> | ||
</html> |
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.
Add newline.
})(); | ||
</script> | ||
</body> | ||
</html> |
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.
Add newline.
fetch/api/resources/basic.html
Outdated
<!DOCTYPE html> | ||
<!-- | ||
Duplicating this resource to make service worker scoping simpler in |
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.
"Duplicating /common/blank.html..."
Bug for Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1378342. |
Bug for WebKit: https://bugs.webkit.org/show_bug.cgi?id=174980 |
if that's the intent, |
We don't have use cases for that right now, but we can add it later if need be. |
This makes the following changes: * Integrates DOM's `AbortSignal`. * Specifies abort points and how to handle termination. * Replaces termination reason with an aborted flag. Tests: web-platform-tests/wpt#6484. Service worker integration follow-up: w3c/ServiceWorker#1178. Fixes #447. Closes #563.
* Updated the versions in WPT_HEAD in checkout.sh and README.chromium. * All wpt commands now have a single entry point, wpt. Include this dispatch module in WPTWhiteList. * The commands for generating manifest, starting wptserve and running wpt lint are now "wpt manifest", "wpt serve" and "wpt lint". Change webkitpy and PRESUBMIT.py accordingly. * The new wptserve contains a change for fetch/api/abort/general-serviceworker.https.html (web-platform-tests/wpt#6484), so the test is rebaselined. Bug: 749879 Change-Id: I214860078add1c391266f063193aa279db414bb0 Reviewed-on: https://chromium-review.googlesource.com/677557 Reviewed-by: Quinten Yearsley <qyearsley@chromium.org> Commit-Queue: Robert Ma <robertma@chromium.org> Cr-Commit-Position: refs/heads/master@{#504060}
* Updated the versions in WPT_HEAD in checkout.sh and README.chromium. * All wpt commands now have a single entry point, wpt. Include this dispatch module in WPTWhiteList. * The commands for generating manifest, starting wptserve and running wpt lint are now "wpt manifest", "wpt serve" and "wpt lint". Change webkitpy and PRESUBMIT.py accordingly. * The new wptserve contains a change for fetch/api/abort/general-serviceworker.https.html (web-platform-tests/wpt#6484), so the test is rebaselined. Bug: 749879 Change-Id: I214860078add1c391266f063193aa279db414bb0 Reviewed-on: https://chromium-review.googlesource.com/677557 Reviewed-by: Quinten Yearsley <qyearsley@chromium.org> Commit-Queue: Robert Ma <robertma@chromium.org> Cr-Original-Commit-Position: refs/heads/master@{#504060} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 26a8ae1db8e8caa1f7c8b84611d6c72948120320
For whatwg/fetch#523
This change is