Skip to content

WebSocket transport: Treat normal close frames as io.EOF - #7236

Merged
RPRX merged 2 commits into
XTLS:mainfrom
thatssoheil:fix/websocket-normal-close-eof
Oct 29, 2026
Merged

RPRX merged 2 commits into
XTLS:mainfrom
thatssoheil:fix/websocket-normal-close-eof

Conversation

@thatssoheil

Copy link
Copy Markdown
Contributor

What this fixes

A WebSocket close frame with a normal status (1000) or no status (1005) ends the byte stream the same way a FIN does, but connection.Read surfaced gorilla's *websocket.CloseError to its callers.

VLESS and Trojan carry payload without framing, so a response copy ended with that error. app/proxyman/outbound.Handler.Dispatch treats every error other than io.EOF as a failure and calls common.Interrupt, which releases the data still buffered in the pipe before the inbound has read it. The client gets a short response that ends with a normal FIN - silent truncation for HTTP/1.0 responses without Content-Length, and a websocket: close 1000 (normal) line for every such connection.

Fix: map websocket.IsCloseError(err, CloseNormalClosure, CloseNoStatusReceived) to io.EOF in connection.Read. Other close codes keep their error semantics. The same wrapper is used by the XHTTP browser dialer client (transport/internet/splithttp/browser_client.go), so that path inherits the fix.

Fixes #7235.

Test record

Environment: 1b1647cd (v26.10.21), linux/amd64. The configs and scripts from the report used verbatim: target answers HTTP/1.0 200 OK + 65536 body bytes without Content-Length, then closes; the client fetches through an http inbound -> VLESS/ws outbound.

scenario base 1b1647cd with fix bba46a28
20 sequential fetches, GOMAXPROCS=1 19/20 short (15 x 0 B, 2 x 32749 B, 1 x 8171 B, 1 x 8173 B); 20 close 1000 log lines 20/20 complete 65536 B; 0 close lines
10 rounds x 20 concurrent fetches ~172/200 short; 200 close 1000 log lines 200/200 complete; 0 close lines
raw TCP control (same scripts) 20/20 complete; 0 close lines 20/20 complete

The byte counts above are from the runs saved for this report; a second base run produced the same shape (18/20 short) with a different byte split.

Unit tests (new connection_test.go, real WebSocket server + the production wrapper): RED on base - close 1000 and 1005 come back as websocket: close 1000 (normal) / close 1005 (no status) instead of io.EOF; GREEN with the fix. A 1001 (going away) control asserts that other close codes stay errors, before and after.

go test ./transport/internet/... - 25 packages pass. go build ./..., gofmt, go vet clean.

Known boundary: a close frame arriving in the middle of a fragmented message still surfaces as a close error, because that payload is genuinely incomplete; only a close after a complete message is remapped, which is the reported mechanism.

A close frame with code 1000 (normal) or 1005 (no status) ends the byte
stream the same way a FIN does, but connection.Read returned gorilla's
*websocket.CloseError to its callers. VLESS and Trojan carry payload
without framing, so a response copy ended with that error; the outbound
dispatch treats it as a failure and interrupts the pipe, dropping the
tail that the inbound had not read yet.

Map normal-closure and no-status close frames to io.EOF. Other close
codes keep their error semantics.

Fixes XTLS#7235
Copilot AI balanced review requested due to automatic review settings October 22, 2026 23:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@RPRX RPRX changed the title WebSocket: Treat normal close frames as io.EOF WebSocket transport: Treat normal close frames as io.EOF Oct 29, 2026
@RPRX
RPRX merged commit ec124ff into XTLS:main Oct 29, 2026
40 checks passed
Romaxa55 pushed a commit to Romaxa55/Xray-core that referenced this pull request Nov 2, 2026
gorilla/websocket answers the peer's close frame with its own and then
fails every write with the sentinel ErrCloseSent. The outbound handler
treats any error but EOF as a broken link and interrupts the pipe,
dropping the response the reader has not taken yet. After the close
handshake the stream is over: report io.EOF, as Read does for a normal
close since XTLS#7236.
Romaxa55 pushed a commit to Romaxa55/Xray-core that referenced this pull request Nov 2, 2026
Romaxa55 pushed a commit to Romaxa55/Xray-core that referenced this pull request Nov 2, 2026
gorilla/websocket answers the peer's close frame with its own and then
fails every write with the sentinel ErrCloseSent. The outbound handler
treats any error but EOF as a broken link and interrupts the pipe,
dropping the response the reader has not taken yet. After the close
handshake the stream is over: report io.EOF, as Read does for a normal
close since XTLS#7236.
black-raison-detre added a commit to black-raison-detre/Xray-core that referenced this pull request Nov 7, 2026
Backport upstream XTLS#7225, XTLS#7327, XTLS#7236, and XTLS#7291 onto the
v26.7.11 maintenance branch with focused regression coverage.

Co-authored-by: Codex <[email protected]>
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.

Withdrawn

4 participants