Repository navigation
Description
Summary
The hysteria transport keeps its QUIC clients in a process-global pool that is not tied to the Xray instance. When an instance is closed (e.g. core.Instance.Close() in an embedding app such as a mobile client that restarts the core on config change, or runs short-lived probe instances for latency tests), its hysteria QUIC connections are not closed. If finalmask.quicParams.keepAlivePeriod is set, those connections stay alive (sending keepalives) until the process exits.
Version
v26.9.9 (same code in v26.9.30). File: transport/internet/hysteria/dialer.go.
Details
dialer.go:289-292— the pool is a package-level singleton (var manager *clientManager,initmanager sync.Once), created on firstDial(:303) and never reset.dialer.go:268-271, 326— the map key isdialerConf{net.Destination, *internet.MemoryStreamConfig}. Every new instance has its own*MemoryStreamConfigpointer, so every instance creates a newclientfor the same destination; entries from previous instances are never looked up again.dialer.go:260-266, 278-287— the cleanup goroutine (idleCleanupInterval= 1s) only closes a client whoseconn.Context()is already done (StatusInactive). It never closes a live connection, and it never deletes entries from the map.dialer.go:87-88—MaxIdleTimeout/KeepAlivePeriodcome fromquicParams. WithkeepAlivePeriod: 20, maxIdleTimeout: 60, the idle timeout is never reached, so the orphaned connection lives forever.
Nothing in the instance shutdown path touches manager, so there is no way for an embedder to release these connections short of killing the process.
Impact
Observed in production on Android (gomobile/libv2ray, one long-lived process):
- Each latency-test instance against a hysteria server leaves one QUIC connection alive after the instance is closed. After ~1 hour of normal use, conntrack on the server side showed 57 live UDP flows from a single phone, the oldest ~50 minutes old, all sending keepalives.
- Wasted battery and mobile data; the server keeps per-connection state.
- On some mobile networks many parallel QUIC flows to the same destination appear to trigger operator-side rate limiting, which then breaks new handshakes.
- The map also grows without bound (one entry per instance × destination), retaining old
MemoryStreamConfigobjects.
Steps to reproduce
- Embed Xray, configure a hysteria outbound with
finalmask.quicParams.keepAlivePeriod: 20. - Start an instance, send one request through the outbound, then
Close()the instance. - Repeat N times in the same process.
- On the server (or with
ss -uap/ tcpdump on the client) observe N live QUIC connections that keep sending keepalive packets indefinitely.
Expected
Closing an instance closes all hysteria QUIC connections it created (as other transports' connections are closed when the instance's outbounds are closed).
Suggested fix
Either scope the client pool to the instance (e.g. hold it in a feature/object registered on the instance and close it from Close()), or track which clients belong to an instance and close + delete them on shutdown. At minimum, the cleanup loop should delete closed entries from the map.
Activity
fix(hysteria): remove clients owned by stopped instances