Skip to content

fix(xhttp): skip expired entries when picking reusable connections - #3479

Merged
wwqgtxx merged 1 commit into
MetaCubeX:Alphafrom
echoowall:fix/xhttp-reuse-skip-expired
Nov 18, 2026
Merged

wwqgtxx merged 1 commit into
MetaCubeX:Alphafrom
echoowall:fix/xhttp-reuse-skip-expired

Conversation

@echoowall

@echoowall echoowall commented Nov 17, 2026 •

Copy link
Copy Markdown

Summary

With reuse-settings, a connection that has passed h-max-reusable-secs keeps receiving new proxy requests as long as at least one request is still open on it. The documentation says the opposite:

After a TCP/QUIC connection lasts this long, it will no longer be assigned new HTTP requests and will be closed after the last internal HTTP request finishes.

— https://github.com/MetaCubeX/Meta-Docs/blob/0ec4dad4496d905b42db3794c49b2ce690a13a5b/docs/config/proxies/transport.en.md?plain=1#L504-L506

Since every new request reuses the expired connection, its open-request count rarely reaches zero, so on a busy client the connection is not rotated at all and h-max-reusable-secs has no effect. Only h-max-request-times, if set, still bounds it. This is most visible when a long-lived connection goes bad (for example, its path starts dropping packets): new requests keep landing on it until the client is restarted or the proxy is reselected. Idle connections are not affected; they are already closed on expiry.

Root cause

pickLocked() skips entries that are closed, out of leftRequests, past maxReuseTimes or at maxConcurrency, but does not look at unreusableAt —

func (m *ReuseManager) pickLocked() *reuseEntry {
var best *reuseEntry
for _, entry := range m.entries {
if entry.isClosed() {
continue
}
if entry.leftRequests.Load() <= 0 {
continue
}
if entry.maxReuseTimes > 0 && entry.reuseCount.Load() >= entry.maxReuseTimes {
continue
}
if m.maxConcurrency > 0 && int(entry.openUsage.Load()) >= m.maxConcurrency {
continue
}
if best == nil || entry.openUsage.Load() < best.openUsage.Load() {
best = entry
}
}
return best
}

Expiry is only checked in cleanupLocked() and release(), and both act only when openUsage == 0 —

func (m *ReuseManager) cleanupLocked(now time.Time) {
kept := m.entries[:0]
for _, entry := range m.entries {
if entry.isClosed() {
continue
}
if entry.leftRequests.Load() <= 0 && entry.openUsage.Load() == 0 {
entry.close()
continue
}
if !entry.unreusableAt.IsZero() && now.After(entry.unreusableAt) && entry.openUsage.Load() == 0 {
entry.close()
continue
}
kept = append(kept, entry)
}
m.entries = kept
}
func (entry *reuseEntry) release() {
if entry == nil {
return
}
remaining := entry.openUsage.Add(-1)
if remaining < 0 {
entry.openUsage.Store(0)
remaining = 0
}
if remaining == 0 {
now := time.Now()
if entry.leftRequests.Load() <= 0 ||
(entry.maxReuseTimes > 0 && entry.reuseCount.Load() >= entry.maxReuseTimes) ||
(!entry.unreusableAt.IsZero() && now.After(entry.unreusableAt)) {
entry.close()
}
}
}

With max-connections set, the expired entry also still counts toward len(m.entries), so shouldCreateLocked() returns false and the expired entry is picked again —

func (m *ReuseManager) shouldCreateLocked() bool {
if len(m.entries) == 0 {
return true
}
if m.maxConnections > 0 {
return len(m.entries) < m.maxConnections
}
return false
}

Xray-core removes clients past UnreusableAt from the pool before picking, regardless of how many requests are running on them, and closes them once those requests finish — https://github.com/XTLS/Xray-core/blob/f781c68715cda8a896dde7a19966980a15e53071/transport/internet/splithttp/mux.go#L89-L105

Change

  • pickLocked() takes the now already computed in GetTransport() and skips entries past unreusableAt, next to the existing leftRequests and maxReuseTimes checks.

What stays the same:

  • Requests already running on an expired entry are not interrupted. Each Dial gets one ReuseTransport from getTransport() and uses it for the whole session (all POSTs and the GET), so an entry expiring mid-session does not split a session across connections —
    func (c *Client) getTransport() (uploadTransport http.RoundTripper, downloadTransport http.RoundTripper, err error) {
    uploadTransport = c.makeTransport()
    downloadTransport = onlyRoundTripper{uploadTransport}
    if c.makeDownloadTransport != nil {
    downloadTransport = c.makeDownloadTransport()
    }
    return
  • The expired entry is still closed by release() when its last request finishes, and removed by cleanupLocked().
  • Upload and download managers (download-settings) expire independently, as before.
  • Nothing changes when h-max-reusable-secs is unset or 0, or when reuse-settings is not used.

Tests

TestManagerRotateOnReusableSecs closes the first transport before asking for the second one, so it only covers the idle path. The new TestManagerSkipExpiredBusyEntry keeps the first transport open across expiry and checks that the next GetTransport() returns a different connection, with max-concurrency: 16 and with max-connections: 1.

  • Alpha 3c0d10a: both subtests fail (expected new transport after reusable timeout while busy, got same id 1).
  • This branch: both pass.

go test ./transport/xhttp/, go test -race ./transport/xhttp/, go vet ./transport/xhttp/ and gofmt -l are clean (Go 1.27, darwin/arm64).

I also ran the XHTTP tests in listener/inbound (-run XHTTP). On this machine some Concurrent subtests fail intermittently with connection reset by peer / broken pipe on loopback, both on Alpha (54 of 1759) and on this branch (43 of 1759), with a different set each run and including subtests that do not use reuse-settings. I did not see a difference attributable to this change.

Limitations

While an expired entry is draining it still counts toward max-connections, so during that time fewer than max-connections fresh connections may be opened. Xray-core drops it from the count immediately. This does not affect correctness and I left it out to keep the change small.

@wwqgtxx
wwqgtxx merged commit 2be2f08 into MetaCubeX:Alpha Nov 18, 2026
135 checks passed
@echoowall
echoowall deleted the fix/xhttp-reuse-skip-expired branch November 18, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants