]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: cache: free the blocks of aborted stores at once
authorOlivier Houchard <ohouchard@haproxy.com>
Wed, 25 Nov 2026 17:18:23 +0000 (18:18 +0100)
committerOlivier Houchard <cognet@ci0.org>
Wed, 25 Nov 2026 17:06:58 +0000 (18:06 +0100)
When a store is aborted, because the object turns out to be larger than
max-object-size or because the stream ends before the end of the
response, its row is reattached to the avail list still holding its
data, but it is in no LRU. With "early-hints on", once such a row
reaches the head of the list, cache_make_room() evicts a valid entry
instead, and the row stays there. These rows pile up and the cache
shrinks until no valid entry is left. Any client can cause this by
requesting such objects or by aborting.

Let's truncate these rows when they are reattached, so that their
blocks are free again immediately. This was introduced by commit
77c4de1ec ("MEDIUM: cache: early hints-aware eviction code") in
3.5-dev3, no backport is needed.

src/cache.c

index e47199702f24db016f5a3b072a8576b3720ce518..13e51b3e50de56d71639d541f77f4d6a088d863e 100644 (file)
@@ -658,6 +658,18 @@ static inline void cache_row_reattach(struct cache *cache, struct shared_block *
        shctx_row_reattach(shctx_ptr(cache), first);
 }
 
+/* Reattach the row of an aborted store and free its blocks, as it is in no
+ * LRU. Must be called under shctx wrlock.
+ */
+static inline void cache_row_abort(struct cache *cache, struct shared_block *first)
+{
+       struct cache_entry *entry = (struct cache_entry *)first->data;
+
+       cache_row_reattach(cache, first);
+       if (!first->refcount && !entry->eb.key)
+               shctx_row_truncate(shctx_ptr(cache), first, 0);
+}
+
 static int
 cache_store_init(struct proxy *px, struct flt_conf *fconf)
 {
@@ -803,7 +815,9 @@ cache_store_strm_deinit(struct stream *s, struct filter *filter)
         * there too, in case of errors */
        if (st && st->first_block) {
                struct cache_entry *object = (struct cache_entry *)st->first_block->data;
-               if (!(object->flags & CACHE_EF_COMPLETE)) {
+               int aborted = !(object->flags & CACHE_EF_COMPLETE);
+
+               if (aborted) {
                        /* The stream was closed but the 'complete' flag was not
                         * set which means that cache_store_http_end was not
                         * called. The stream must have been closed before we
@@ -812,7 +826,10 @@ cache_store_strm_deinit(struct stream *s, struct filter *filter)
                        release_entry_unlocked(&cache->trees[object->eb.key % CACHE_TREE_NUM], object);
                }
                shctx_wrlock(shctx);
-               cache_row_reattach(cache, st->first_block);
+               if (aborted)
+                       cache_row_abort(cache, st->first_block);
+               else
+                       cache_row_reattach(cache, st->first_block);
                shctx_wrunlock(shctx);
        }
        if (st) {
@@ -870,7 +887,7 @@ static inline void disable_cache_entry(struct cache_st *st,
        filter->ctx = NULL; /* disable cache  */
        release_entry_unlocked(&cache->trees[object->eb.key % CACHE_TREE_NUM], object);
        shctx_wrlock(shctx);
-       cache_row_reattach(cache, st->first_block);
+       cache_row_abort(cache, st->first_block);
        shctx_wrunlock(shctx);
        pool_free(pool_head_cache_st, st);
 }