]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: mux-h2: don't stall on a failed rxbuf alloc without stream
authorOlivier Houchard <ohouchard@haproxy.com>
Fri, 20 Nov 2026 13:29:45 +0000 (14:29 +0100)
committerOlivier Houchard <cognet@ci0.org>
Fri, 20 Nov 2026 14:06:33 +0000 (15:06 +0100)
When h2c_dec_hdrs() fails to allocate the temporary buffer used to
decode the HEADERS frame of a new request, it sets H2_CF_DEM_SALLOC and
the connection is queued in the buffer wait list. But h2_buf_available()
only serves H2_CF_DEM_SALLOC for a stream which has a stream connector,
which is not the case yet, so it never succeeds: the demux remains
blocked and the request is not processed until the connection times
out. This can be reproduced by faking a failed allocation there on the
first request of a connection.

There is no buffer the callback could allocate in this case, so let's
clear the flag and restart the demux, which will retry by itself. Since
b_alloc() refuses to allocate when other entries are queued, the retry
would be queued again and the offered buffer would be lost for
everyone, so a new flag H2_CF_DEM_MAYALLOC is set to let the next
h2_get_buf() allocate with DB_F_NOQUEUE, as mux-h1 does with
H1C_F_IN_MAYALLOC.

This was introduced in 2.0 by commit 5c8cafae3 ("MEDIUM: mux-h2: decode
HEADERS frames before allocating the stream"). It must be backported to
all supported versions. Before 3.0 there is no DB_F_NOQUEUE and the new
flag is not needed, clearing H2_CF_DEM_SALLOC and restarting the demux
is enough.

include/haproxy/mux_h2-t.h
src/mux_h2.c

index fd8f5abd8bbe2d75ed17e24064e4ed7ecdbcefe0..502b3071ef3a3d5780cc816980236fd200d73b8c 100644 (file)
@@ -73,6 +73,7 @@
 
 #define H2_CF_IDL_PING          0x04000000  // timer task scheduled for a PING emission
 #define H2_CF_IDL_PING_SENT     0x08000000  // PING emitted, or will be on next tasklet run, waiting for ACK
+#define H2_CF_DEM_MAYALLOC      0x10000000  // demux may allocate its next buffer despite the queue
 
 /* This function is used to report flags in debugging tools. Please reflect
  * below any single-bit flag addition above in the same order via the
@@ -91,7 +92,7 @@ static forceinline char *h2c_show_flags(char *buf, size_t len, const char *delim
        _(H2_CF_GOAWAY_SENT, _(H2_CF_GOAWAY_FAILED, _(H2_CF_WAIT_FOR_HS, _(H2_CF_IS_BACK,
        _(H2_CF_WINDOW_OPENED, _(H2_CF_RCVD_SHUT, _(H2_CF_END_REACHED,
        _(H2_CF_RCVD_RFC8441, _(H2_CF_SHTS_UPDATED, _(H2_CF_DTSU_EMITTED,
-       _(H2_CF_ERR_PENDING, _(H2_CF_ERROR)))))))))))))))))))))))));
+       _(H2_CF_ERR_PENDING, _(H2_CF_ERROR, _(H2_CF_DEM_MAYALLOC))))))))))))))))))))))))));
        /* epilogue */
        _(~0U);
        return buf;
index 920ee1c13a1cbdc188aaf3222a416f4d20f9f579..8dd296adeddc4e2a1155ae43120fc70ab1b0c8ed 100644 (file)
@@ -1292,6 +1292,15 @@ static int h2_buf_available(void *target)
                progress = 1;
        }
 
+       if ((h2c->flags & H2_CF_DEM_SALLOC) &&
+           !h2s_sc(h2c_st_by_id(h2c, h2c->dsi))) {
+               /* no stream to allocate for, let the demux retry */
+               h2c->flags &= ~H2_CF_DEM_SALLOC;
+               h2c->flags |= H2_CF_DEM_MAYALLOC;
+               h2c_restart_reading(h2c, 1);
+               progress = 1;
+       }
+
        if ((h2c->flags & H2_CF_DEM_SALLOC) &&
            (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s_sc(h2s) &&
            (h2s_rxbuf_tail(h2s) || h2s_get_rxbuf(h2s))) {
@@ -1321,9 +1330,12 @@ static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
        if (b_size(bptr))
                return bptr;
 
-       if (likely(!LIST_INLIST(&h2c->buf_wait.list)) &&
-           unlikely((buf = b_alloc(bptr, DB_MUX_RX)) == NULL)) {
-               b_queue(DB_MUX_RX, &h2c->buf_wait, h2c, h2_buf_available);
+       if (likely(!LIST_INLIST(&h2c->buf_wait.list))) {
+               buf = b_alloc(bptr, DB_MUX_RX |
+                             ((h2c->flags & H2_CF_DEM_MAYALLOC) ? DB_F_NOQUEUE : 0));
+               h2c->flags &= ~H2_CF_DEM_MAYALLOC;
+               if (unlikely(!buf))
+                       b_queue(DB_MUX_RX, &h2c->buf_wait, h2c, h2_buf_available);
        }
        return buf;
 }