Skip to content

Concurrent requests for the same resource can result in cache misses #24166

Closed

Description

@asajeffrey

If two GET requests are processed in quick succession, then the second one can end up being a cache miss, since the code to check whether the cache has been hit uses a read lock at

if let Ok(http_cache) = context.state.http_cache.read() {
whereas the code to insert into the cache uses a write lock at
if let Ok(mut http_cache) = context.state.http_cache.write() {

Activity

  1. added
    I-race
    on Sep 10, 2019
  2. asajeffrey commented on Sep 10, 2019

    @asajeffrey
    ContributorAuthor

    This came up in the context of #24148 which makes concurrent GETs more likely.

  3. asajeffrey commented on Sep 10, 2019

    @asajeffrey
    ContributorAuthor

    The matching spec issue: whatwg/fetch#932

  4. gterzian commented on Sep 10, 2019

    @gterzian
    Member

    That's interesting.

    it might be the point that our simple "lock around a hashmap" approach to the cache shows its limitations.

    We could handle this quite easily if the cache was message-passing based, where the cache ran in a worker thread, and each fetch thread would send a message to ask for a cached entry, and block on the reply. The cache could then simply let the fetch block if it knows there is another one doing the fetch at the same time.

    I guess it could be done with locks(and condvars?) without an additional cache worker thread, but that might be much more complicated...

  5. jdm commented on Sep 10, 2019

    @jdm
    Member

    I don't think a message-passing-based approach would solve this issue - the spec describes two separate operations (check the cache & store the response) which happen at different times, so unless we add a marker entry to the cache which means "something else has requested this URL and there is no response yet" we're going to always run into this problem.

  6. gterzian commented on Sep 10, 2019

    @gterzian
    Member

    so unless we add a marker entry to the cache which means "something else has requested this URL and there is no response yet" we're going to always run into this problem

    I agree. What I mean is that it would be easier to add such a marker, as it would any other "state" in addition to the cached entries, if the cache where to run in it's own thread and handle messages, versus adding more shared-state to the current structure.

  7. gterzian commented on Sep 10, 2019

    @gterzian
    Member

    Ok so I think one idea from https://github.com/servo/servo/pull/23553/files could be useful, which is to have fetches operate on entries, versus the cache as a whole.

    Because a problem with the current structure is that we're operation on a RwLock<HttpCache>, which means it's going to be pretty hard to hide any sort of condvar based logic inside of it, since you'd need to acquire the outer lock first, and to tie that one to a condvar would require changing the whole structure of HttpState.

    Instead, if fetches operate on entries, we can put a condvar inside of it, tie it to a new mutex around a "pending fetch" flag, and just have every other fetch using the same entry block on it, without affecting ALL fetches.

    (And we don't need all the fine-grained locking from that PR, just the entries concept).

  8. gterzian commented on Sep 11, 2019

    @gterzian
    Member

    I will take a look at using entries, and a condvar.

  9. added 7 commits that reference this issue on Sep 12, 2019
    2742af8
    faa97eb
    0bde330
    382faa3
    02ee933
    e7fca8d
    bb81703
  10. added 3 commits that reference this issue on Sep 29, 2019
    cd5b3cc
    30e695f
    c1f701b
  11. added a commit that references this issue on Oct 8, 2019
    c1401f5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-networkI-race

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions