]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MEDIUM: backend: read the cached ALPN only once in connect_server()
authorOlivier Houchard <ohouchard@haproxy.com>
Thu, 26 Nov 2026 17:52:41 +0000 (18:52 +0100)
committerOlivier Houchard <cognet@ci0.org>
Fri, 27 Nov 2026 12:47:09 +0000 (13:47 +0100)
When connect_server() wants to start the mux before the handshake, it
checks under the path parameters lock that an ALPN is cached for this
connection's hash, but the mux is then chosen by conn_install_mux_be(),
which reads the cache again, without the lock. If another thread
completed a handshake for a different hash in between, the hash no
longer matches, no ALPN is found and the default mux, H1, is installed.
Once the handshake negotiates H2, the request is sent in H1 to an H2
server, and the client receives a 502.

Let's copy the cached ALPN while holding the lock, pick the mux from
that copy, and give it to conn_install_mux_be(). This is a regression
from commit de3f245df ("BUG/MEDIUM: servers: Store the connection hash
with the parameter cache") in 3.4-dev13. It can be reproduced with a
server using "ssl alpn h2,http/1.1 allow-0rtt", several threads, and
requests using a few different Host header values.

This should help with github issue #3473, and must be backported to 3.3.

src/backend.c

index 4232be31eba635d0f2dd0e8d505feae5ab167010..22a1721e44f78262239dff83cc58c4913409e4af 100644 (file)
@@ -1820,6 +1820,7 @@ int connect_server(struct stream *s)
        int reuse __maybe_unused = 0;
        int may_use_early_data __maybe_unused = 1; // are we allowed to use early data ?
        int may_start_mux_now = 1; // are we allowed to start the mux now ?
+       char cached_alpn[MAX_ALPN_SIZE];
        int err;
        struct sockaddr_storage *bind_addr = NULL;
        int64_t hash = 0;
@@ -1829,6 +1830,8 @@ int connect_server(struct stream *s)
        srv = objt_server(s->target);
        reuse_mode = be_reuse_mode(s->be, srv);
 
+       cached_alpn[0] = 0;
+
        err = alloc_dst_address(&s->scb->dst, srv, s);
        if (err != SRV_STATUS_OK)
                return SF_ERR_INTERNAL;
@@ -2086,6 +2089,8 @@ int connect_server(struct stream *s)
                                        HA_RWLOCK_RDLOCK(SERVER_LOCK, &srv->path_params.param_lock);
                                        if (srv->path_params.srv_hash != hash || srv->path_params.nego_alpn[0] == 0)
                                                may_start_mux_now = 0;
+                                       else
+                                               strlcpy2(cached_alpn, srv->path_params.nego_alpn, sizeof(cached_alpn));
                                        HA_RWLOCK_RDUNLOCK(SERVER_LOCK, &srv->path_params.param_lock);
                                }
                        }
@@ -2138,7 +2143,12 @@ int connect_server(struct stream *s)
 
                if (may_start_mux_now) {
                        /* Delay MUX init if an XPRT handshake is required prior. */
-                       mux_proto = conn_select_mux_be(srv_conn);
+                       if (cached_alpn[0] && !srv->mux_proto)
+                               mux_proto = conn_get_best_mux_entry(IST_NULL, ist(cached_alpn), PROTO_SIDE_BE,
+                                                                   proto_is_quic(srv_conn->ctrl),
+                                                                   conn_pr_mode_to_proto_mode(s->be->mode));
+                       else
+                               mux_proto = conn_select_mux_be(srv_conn);
                        if (mux_proto && mux_proto->init_xprt)
                                may_start_mux_now = 0;
                }
@@ -2231,6 +2241,9 @@ int connect_server(struct stream *s)
        if (may_start_mux_now) {
                const struct mux_ops *alt_mux =
                  likely(!(s->flags & SF_WEBSOCKET) || !srv) ? NULL : srv_get_ws_proto(srv);
+
+               if (!alt_mux && cached_alpn[0] && mux_proto)
+                       alt_mux = mux_proto->mux;
                if (conn_install_mux_be(srv_conn, s->scb, s->sess, alt_mux) < 0) {
                        conn_full_close(srv_conn);
                        return SF_ERR_INTERNAL;