Skip to content

Commit

Permalink
fetch: ensure consistency between response, done-chan, and cache state.
Browse files Browse the repository at this point in the history
  • Loading branch information
gterzian committed Oct 9, 2019
1 parent ec408e9 commit bb1fa88
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions components/net/http_loader.rs
Expand Up @@ -1092,20 +1092,6 @@ fn http_network_or_cache_fetch(
response_from_cache.needs_validation,
),
};
if cached_response.is_none() {
// Ensure the done chan is not set if we're not using the cached response,
// as the cache might have set it to Some if it constructed a pending response.
*done_chan = None;

// Update the cache state, incrementing the pending store count,
// or starting the count.
if let HttpCacheEntryState::PendingStore(i) = *state {
let new = i + 1;
*state = HttpCacheEntryState::PendingStore(new);
} else {
*state = HttpCacheEntryState::PendingStore(1);
}
}
if needs_revalidation {
revalidating_flag = true;
// Substep 5
Expand All @@ -1124,6 +1110,20 @@ fn http_network_or_cache_fetch(
// Substep 6
response = cached_response;
}
if response.is_none() {
// Ensure the done chan is not set if we're not using the cached response,
// as the cache might have set it to Some if it constructed a pending response.
*done_chan = None;

// Update the cache state, incrementing the pending store count,
// or starting the count.
if let HttpCacheEntryState::PendingStore(i) = *state {
let new = i + 1;
*state = HttpCacheEntryState::PendingStore(new);
} else {
*state = HttpCacheEntryState::PendingStore(1);
}
}
}
}
// Notify the next thread waiting in line, if there is any.
Expand Down Expand Up @@ -1223,6 +1223,9 @@ fn http_network_or_cache_fetch(
.map_or(false, |s| s.0 == StatusCode::NOT_MODIFIED)
{
if let Ok(mut http_cache) = context.state.http_cache.write() {
// Ensure done_chan is None,
// since the network response will be replaced by the revalidated stored one.
*done_chan = None;
response = http_cache.refresh(&http_request, forward_response.clone(), done_chan);
wait_for_cached_response(done_chan, &mut response);
}
Expand Down

0 comments on commit bb1fa88

Please sign in to comment.