Skip to content

Metrics: Close HTTP connections when stopping Xray Instance - #7361

Merged
RPRX merged 2 commits into
XTLS:mainfrom
Eljees:n/fix-metrics-connection-shutdown
Nov 11, 2026
Merged

RPRX merged 2 commits into
XTLS:mainfrom
Eljees:n/fix-metrics-connection-shutdown

Conversation

@Eljees

@Eljees Eljees commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

When an application embeds Xray and restarts an Instance, an HTTP collector can keep using its existing metrics keep-alive connection after Instance.Close(). The old handler remains available instead of the collector reconnecting to the new instance. This affects both direct metrics.listen and connections routed to metrics.tag.

http.Serve creates an HTTP server that the metrics handler does not retain. Closing its listeners stops new accepts, but leaves accepted connections open. Retain one http.Server for both listeners and close it with the metrics handler, while keeping the existing explicit listener cleanup. Capture the server before starting either serving goroutine so an immediate close is also handled.

The regression starts actual core instances, makes a successful HTTP/1.1 request, calls Instance.Close, then checks that the same connection can no longer receive any HTTP response. It covers listen, tag, and both together; all three fail against the original production code. Fixtures use only a loopback listener and in-memory connections.

Related: #6349, which supports restarting metrics-enabled instances in the same process.

Validation:

  • Windows Go 1.27.3: all 14 tests in ./app/metrics ./app/stats pass; scoped go vet and main build pass.
  • Linux Go 1.27.1 / CGO: the same 14 tests pass with -race; scoped go vet and main build pass.
  • Repository format check passes on the exact Git source archive.
  • The full repository test suite and macOS runtime were not run locally.

Implemented and tested with Codex assistance.

Eljees and others added 2 commits November 10, 2026 12:06
Retain the HTTP server serving both metrics listeners and close its accepted
connections together with the core, so persistent collectors cannot keep
using a stopped instance after an embedded application restarts it.

Cover direct, tagged and combined listeners with keep-alive regressions.

Assisted-by: Codex
Signed-off-by: Eljees <[email protected]>

@Fangliding Fangliding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chore

@RPRX RPRX changed the title Metrics: Close HTTP connections when stopping the core Metrics: Close HTTP connections when stopping Xray Instance Nov 11, 2026
@RPRX
RPRX merged commit e184d77 into XTLS:main Nov 11, 2026
39 checks passed
patterniha added a commit to patterniha/Xray-core that referenced this pull request Nov 12, 2026
Brings in XTLS/Xray-core up to c456141 (still v26.11.9):
- Build: build list optimization and build-tag clean-up (XTLS#5824)
- Hysteria transport: proper Brutal behavior; bump quic-go to
  v0.63.1-...a10df75c (XTLS#7356)
- Metrics: close HTTP connections when stopping the Instance (XTLS#7361)
- XDNS finalmask: TCP resolver worker (XTLS#7354)
- VLESS mux.cool: maxConcurrency 128 only for XDRIVE (XTLS#7375); replace
  MutexWriter with buf.BufferedWriter (XTLS#7359)

No conflicts: upstream and the fork changed disjoint files; go.mod/go.sum
are taken from upstream unchanged. The fork's core/core.go version
(26.11.12) is kept.
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.

3 participants