Automatic no-delay for SSE is skipped when Content-Type carries parameters (text/event-stream; charset=utf-8)

Abierto Apto para principiantes
#3,574 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
80/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
c
Área
networking

Línea de trabajo

Comienza en src/http_ana.c, en el bloque http_find_header(htx, ist("content-type"), ...) mostrado, y sigue cómo se compara ctx.value con isteqi(). Reproduce con el script de Python proporcionado y luego haz que la comprobación de SSE compare solo el tipo de medio antes del primer ;, recortando los espacios en blanco finales. Hecho significa que text/event-stream; charset=utf-8 sigue la misma ruta sin retardo que text/event-stream, idealmente comprobado con el reproductor y con las pruebas existentes de cabeceras HTTP.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Detailed Description of the Problem

HAProxy 3.4.7 enables no-delay sending automatically for server-sent events, but only when the response Content-Type is exactly text/event-stream. When the header carries a parameter, for example text/event-stream; charset=utf-8, the check does not match and small events are held back as before: in the reproduction below the first event after a 120 ms pause reaches the client about 88 ms late, and events are delayed by about 46 ms at the median.

text/event-stream; charset=utf-8 is common in practice. Starlette appends ; charset=utf-8 to every text/* media type, so FastAPI applications and servers built on it (vLLM's OpenAI-compatible server is one) send their SSE streams this way.

Expected Behavior

The automatic SSE no-delay applies whenever the media type is text/event-stream, whatever parameters follow it, so that text/event-stream; charset=utf-8 is delivered like text/event-stream.

Steps to Reproduce the Behavior
  1. Save the configuration under "What is your configuration?" as haproxy.cfg and start HAProxy, for example docker run --rm --network host -v "$PWD/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro" haproxy:lts-alpine.
  2. Run the script below (Python standard library only). It starts an origin on 127.0.0.1:18711 that sends one event, pauses 120 ms, then sends ten small events 15 ms apart, each carrying the time it was written. The client measures the time from that write to the read, directly and through HAProxy, for both content types (20 requests each).
"""SSE delivery delay through HAProxy: Content-Type `text/event-stream` versus `text/event-stream; charset=utf-8`.

Python standard library only. Start HAProxy with the configuration below (for example
`docker run --rm --network host -v "$PWD/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro" haproxy:lts-alpine`),
then run: python3 sse_repro.py

haproxy.cfg:
    defaults
      mode http
      timeout connect 3s
      timeout client 30s
      timeout server 30s
    frontend sse
      bind 127.0.0.1:18700
      default_backend origin
    backend origin
      server origin 127.0.0.1:18711
"""

import http.client
import json
import statistics
import threading
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

ORIGIN, PROXY = 18711, 18700
TYPES = {"/bare": "text/event-stream", "/charset": "text/event-stream; charset=utf-8"}


class Origin(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"

    def log_message(self, *args):
        pass

    def event(self, index):
        data = f"data: {json.dumps({'index': index, 'sent': time.time()})}\n\n".encode()
        self.wfile.write(f"{len(data):x}\r\n".encode() + data + b"\r\n")
        self.wfile.flush()

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-Type", TYPES[self.path])
        self.send_header("Transfer-Encoding", "chunked")
        self.end_headers()
        self.event(0)  # a first event, then a pause, then small events: the shape of a token stream
        time.sleep(0.12)
        for index in range(1, 11):
            self.event(index)
            time.sleep(0.015)
        self.wfile.write(b"0\r\n\r\n")


def delays(port, path):
    """Seconds between each event being written by the origin and read by the client."""
    connection = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
    connection.request("GET", path)
    out = []
    for line in connection.getresponse():
        if line.startswith(b"data:"):
            out.append(time.time() - json.loads(line[5:])["sent"])
    connection.close()
    return out


server = ThreadingHTTPServer(("127.0.0.1", ORIGIN), Origin)
server.handle_error = lambda *args: None  # the proxy resets idle server connections; not part of the measurement
threading.Thread(target=server.serve_forever, daemon=True).start()
for name, port in (("direct", ORIGIN), ("haproxy", PROXY)):
    for path in TYPES:
        delays(port, path)  # warm up
        runs = [delays(port, path) for _ in range(20)]
        assert all(len(run) == 11 for run in runs)
        second = [run[1] * 1000 for run in runs]  # the first event after the pause
        every = [d * 1000 for run in runs for d in run]
        print(f"{name:8} {TYPES[path]:36} first event after the pause: median {statistics.median(second):7.2f} ms, max {max(second):7.2f} ms | all events: median {statistics.median(every):7.2f} ms")

Output with haproxy:lts-alpine (3.4.7), everything on loopback on one Linux host:

direct   text/event-stream                    first event after the pause: median    0.14 ms, max    0.21 ms | all events: median    0.09 ms
direct   text/event-stream; charset=utf-8     first event after the pause: median    0.11 ms, max    0.28 ms | all events: median    0.09 ms
haproxy  text/event-stream                    first event after the pause: median    0.23 ms, max    0.40 ms | all events: median    0.18 ms
haproxy  text/event-stream; charset=utf-8     first event after the pause: median   87.89 ms, max   91.39 ms | all events: median   45.65 ms

With option http-no-delay added to defaults, both content types are delivered immediately:

haproxy  text/event-stream                    first event after the pause: median    0.22 ms, max    0.36 ms | all events: median    0.16 ms
haproxy  text/event-stream; charset=utf-8     first event after the pause: median    0.25 ms, max    0.39 ms | all events: median    0.16 ms
Do you have any idea what may have caused this?

The check in src/http_ana.c compares the whole header value:

/* check if we need to switch to no-delay for SSE */
if (!(s->scf->flags & SC_FL_SND_NEVERWAIT)) {
	struct http_hdr_ctx ctx = { .blk = NULL };

	if (http_find_header(htx, ist("content-type"), &ctx, 0) &&
	    isteqi(ctx.value, ist("text/event-stream"))) {

so a value with parameters after the media type is not recognized.

Do you have an idea how to solve the issue?

Compare only the media type: the part of the value before the first ;, with trailing whitespace removed.

What is your configuration?
defaults
  mode http
  timeout connect 3s
  timeout client 30s
  timeout server 30s
frontend sse
  bind 127.0.0.1:18700
  default_backend origin
backend origin
  server origin 127.0.0.1:18711
Output of haproxy -vv
HAProxy version 3.4.7-8872a26 2026/10/14 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2031.
Known bugs: http://www.haproxy.org/bugs/bugs-3.4.7.html
Build options :
  TARGET  = linux-musl
  CC      = cc
  CFLAGS  = -O2 -g -fwrapv -fvect-cost-model=very-cheap
  OPTIONS = USE_PTHREAD_EMULATION=1 USE_GETADDRINFO=1 USE_OPENSSL=1 USE_LUA=1 USE_QUIC=1 USE_PROMEX=1 USE_PCRE2=1 USE_PCRE2_JIT=1

Official Docker image haproxy:lts-alpine (haproxy@sha256:b9fe316f718d5015c5cfebd8ca019591f3e44656f61d93cb8b59a1028961e530), on x86_64 Linux. haproxy:latest (also 3.4.7) behaves the same, and the comparison is unchanged on master.

Last Outputs and Backtraces

No response

Additional Information

option http-no-delay is a complete workaround, which is what we use now. I am reporting it because the automatic detection looks intended to make that option unnecessary for SSE, and the parameterized form is what most Python ASGI servers emit.

Lenguaje dominante
C
Estrellas
6.9k
Forks
976
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de haproxy/haproxy

Todos los issues de haproxy/haproxy

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.