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 race condition between caching and aborting #8472

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function makeFetchRequest(requestParameters: RequestParameters, callback: Respon
signal: controller.signal
});
let complete = false;
let aborted = false;

const cacheIgnoringSearch = hasCacheDefeatingSku(request.url);

Expand All @@ -110,6 +111,8 @@ function makeFetchRequest(requestParameters: RequestParameters, callback: Respon
}

const validateOrFetch = (err, cachedResponse, responseIsFresh) => {
if (aborted) return;

if (err) {
// Do fetch in case of cache error.
// HTTP pages in Edge trigger a security error that can be ignored.
Expand Down Expand Up @@ -152,6 +155,7 @@ function makeFetchRequest(requestParameters: RequestParameters, callback: Respon
requestParameters.type === 'json' ? response.json() :
response.text()
).then(result => {
if (aborted) return;
if (cacheableResponse && requestTime) {
// The response needs to be inserted into the cache after it has completely loaded.
// Until it is fully loaded there is a chance it will be aborted. Aborting while
Expand All @@ -172,6 +176,7 @@ function makeFetchRequest(requestParameters: RequestParameters, callback: Respon
}

return { cancel: () => {
aborted = true;
if (!complete) controller.abort();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't the controller's abort signal be used directly to reject or escape from validateOrFetch early instead of using an additional aborted variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from discussion in chat: let's leave this for now

}};
}
Expand Down