Skip to content

Recheck the cache before becoming the RTCache lookup writer - #970

Open
nbarbier-265 wants to merge 2 commits into
cloudflare:mainfrom
nbarbier-265:memcache-singleflight-recheck
Open

Recheck the cache before becoming the RTCache lookup writer#970
nbarbier-265 wants to merge 2 commits into
cloudflare:mainfrom
nbarbier-265:memcache-singleflight-recheck

Conversation

@nbarbier-265

Copy link
Copy Markdown
Contributor

Fixes #110.

A task that misses the cache can acquire the lockers write lock after the previous writer for the same key has already populated the cache and removed its lock. It then inserts a new CacheLock and performs a lookup whose result is already in the cache.

This adds the double-check the issue asks for: re-read the cache under the lockers write lock before inserting a new CacheLock, and return LockHit if another task's lookup has already populated the value. This is the same pattern the waiting-reader path already uses after the writer releases its lock.

The race window sits between two synchronous statements (the initial cache read and the write-lock acquisition), so it can't be exercised deterministically through the public API; the existing concurrency tests cover the changed path. MemoryCache::get is a cheap sync read, so doing it under the write lock does not meaningfully extend the critical section, and it is only reached on the miss path.

A task that misses the cache can acquire the lockers write lock after
the previous writer for the same key has already populated the cache
and removed its lock. It then becomes a new writer and performs a
lookup whose result is already cached.

Recheck the cache under the lockers write lock before inserting a new
CacheLock, the same double-checked pattern the waiting-reader path
already uses after its lock is released.

The window sits between two synchronous statements, so it cannot be
exercised deterministically through the public API; the existing
concurrency tests cover the changed path.

Fixes cloudflare#110
h2 0.3.27 comes in through the same legacy aws chain as the existing
rustls-webpki ignores: dial9-tokio-telemetry -> aws-sdk-s3-transfer-manager
-> aws-config -> aws-smithy-http-client, which still uses hyper 0.14. The
advisory's only fix is h2 >= 0.4.16 and no 0.3.x patch exists, so this
cannot be resolved from this workspace's manifests. The vulnerable code
needs a malicious HTTP/2 peer; in this chain h2 is only a TLS client to
AWS endpoints.

Every CI run has failed the cargo audit step since the advisory was
published on 2026-08-17.
@nbarbier-265
nbarbier-265 force-pushed the memcache-singleflight-recheck branch from bcc432a to 5ec4508 Compare August 20, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize memory cache's singleflight performance

1 participant