Skip to content

Cache returns stale response body data on when ETag is the same (Windows only) #5761

@bitwiseman

Description

@bitwiseman

Environment

OkHttp 4.2.2
Only happens on Windows. On linux and MacOS, the correct (expected) behavior occurs.

Description

On Windows, the OkHttp Cache does not update response body data for a request when a later response has the same ETag as a previous response.

Steps

  1. Request a URL /a/b
    • Response Header: 404, no ETag
    • Response Body: '{ "message": "Not Found" }'
    • Cache Updated
  2. Request a URL /a/b with "If-Modified-Since" header
    • Response Header: 304, ETag "C"
    • Cache automatically converts - client sees:
      • Response Header: 404, ETag "C"
      • Response Body: '{ "message": "Not Found" }'
  3. Request a URL /a/b with "Cache-Control: no-cache" to force retrieval of new data
    • Response Header: 200, ETag "C"
    • Response Body: '{ data: "..." }'
    • NOTE: Yes, that is the same ETag with different data - not good, but cache should still work
    • Cache Updated? (I think this is where the problem starts).
  4. Request a URL /a/b with "If-None-Match" equal to ETag "C"

Expected Result

  • Response Header: 304, ETag "C"
  • Cache automatically converts - client sees:
    • Response Header: 200, ETag "C"
    • Response Body: '{ data: "..." }'

Actual Result

  • Response Header: 304, ETag "C"
  • Cache automatically converts - client sees:
    • Response Header: 200, ETag "C"
    • Response Body: '{ "message": "Not Found" }'

Automated Test

I know it is not great to point to a test on an external project but the test fully automated and will repro the issue every time:
https://github.com/github-api/github-api/blob/967512629821b706e8d06ef9b3e57061f397d073/src/test/java/org/kohsuke/github/extras/okhttp3/GitHubCachingTest.java#L59

To run automated repro:

  1. clone the repository on Windows with JDK 8+
  2. remove the assumeFalse() call
  3. run mvn install

Notes

This issue was found and can be reproduced using https://github.com/github-api/github-api .
It is exposed due to a bug in GitHub server responses (see hub4j/github-api#669) but it is still a bug in this OkHttp Cache behavior on Windows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug in existing code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions