[Bug]: WebSocket early-data delayDialConn races with Close and panics on pre-dial deadlines

Open
#7,524 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Fangliding is already working on this.

Since Nov 30, 2026.

  • #7527 by @HamedSanaei — closed without merging
  • #7535 by @Fangliding — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
47/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go

Research direction

Read transport/internet/websocket/dialer.go, especially the delayDialConn methods around lines 160–232, then run the package tests with the race detector. Add regression coverage for concurrent Read/Close and deadline methods before the first Write, including handshake cancellation and connection cleanup. Done means the lifecycle is synchronized, pre-dial deadlines do not panic, and the regression tests pass under go test -race; full package integration validation remains outstanding.

Written by the indexing model from the issue text.

Description

Description

WebSocket client early-data connections returned by Dial when Ed > 0 wrap a nil net.Conn until the first Write performs the handshake. The wrapper still violates the net.Conn lifecycle contract in two related ways: concurrent Read and Close access the same ordinary bool without synchronization, and deadline calls before the first Write are promoted to the nil embedded net.Conn and panic.

These are source-confirmed defects in the deferred connection wrapper, not a claim of a reproduced production memory leak.

Versions and source

Inspected main ded8443 and the installed production version Xray 26.10.21, 1b1647c. The relevant deferred-connection fields and methods match in both versions.

  • dialer.go:21-39: returns delayDialConn as net.Conn without dialing when Ed > 0.
  • dialer.go:160-232: embedded net.Conn, plain closed bool, deferred handshake, Read/Close and no explicit deadline methods.
  • #6544 fixed nil promoted address methods, not deadline methods. Closed, unmerged audit PR #7422 already identified the closed flag race; this narrower issue includes a reduced reproducer and the separate deadline panic. No dedicated issue or merged fix found in searches.
flowchart TD
 A[Dial with WebSocket early data] --> B[Deferred wrapper; embedded Conn nil]
 B --> C[Read]
 B --> D[Close]
 C --> E[Read closed bool]
 D --> F[Write closed bool]
 E --> G[Unsynchronized state]
 F --> G
 B --> H[SetDeadline before first Write]
 H --> I[Promoted method on nil interface]
 I --> J[Panic]

Minimal reproducible test

Executed on Windows with Go 1.27.0 and race detector. Full package test was attempted on a temporary copy of the installed revision, but offline module resolution failed for several uncached dependencies. The following standalone reduced reproducer copies the relevant fields and Read/Close logic unchanged; unused dial configuration fields are omitted. It requires only the Go standard library and no real network, credentials or production configuration. It is not a successful full-package integration test.

Save as audit_test.go in a temporary module, then run go test -race -count=1 -timeout=15s -v .

package audit

import (
 "context"
 "sync"
 "testing"
 "time"
 "io"
 "net"
)

type delayDialConn struct {
 net.Conn
 closed bool
 dialed chan bool
 cancel context.CancelFunc
 ctx context.Context
}

func (d *delayDialConn) Read(b []byte) (int, error) {
 if d.closed { return 0, io.ErrClosedPipe }
 if d.Conn == nil {
  select {
  case <-d.ctx.Done(): return 0, io.ErrUnexpectedEOF
  case <-d.dialed:
  }
 }
 return d.Conn.Read(b)
}
func (d *delayDialConn) Close() error {
 d.closed = true
 d.cancel()
 if d.Conn == nil { return nil }
 return d.Conn.Close()
}

func TestConcurrentReadClose(t *testing.T) {
 for i := 0; i < 100; i++ {
  ctx,cancel := context.WithCancel(context.Background())
  d := &delayDialConn{ctx:ctx,cancel:cancel,dialed:make(chan bool,1)}
  var wg sync.WaitGroup
  wg.Add(2)
  go func(){defer wg.Done();_,_=d.Read(make([]byte,1))}()
  go func(){defer wg.Done();_=d.Close()}()
  wg.Wait()
 }
}

func TestDeadlineBeforeWrite(t *testing.T) {
 ctx,cancel := context.WithCancel(context.Background())
 defer cancel()
 d := &delayDialConn{ctx:ctx,cancel:cancel,dialed:make(chan bool,1)}
 defer func(){
  if r:=recover();r!=nil{t.Fatalf("SetDeadline before first Write panicked: %v",r)}
 }()
 _=d.SetDeadline(time.Now().Add(time.Second))
}

Observed output (local filesystem paths omitted):

=== RUN   TestConcurrentReadClose
WARNING: DATA RACE
Write ... (*delayDialConn).Close()
Previous read ... (*delayDialConn).Read()
--- FAIL: TestConcurrentReadClose
=== RUN   TestDeadlineBeforeWrite
SetDeadline before first Write panicked: runtime error: invalid memory address or nil pointer dereference
--- FAIL: TestDeadlineBeforeWrite
FAIL

The deadline defect follows directly from the embedded interface and also affects SetReadDeadline / SetWriteDeadline before the underlying connection is initialized.

Expected behavior / proposed correction

Provide explicit synchronization for the connection state, not just the closed bool. Ensure the handshake is initiated at most once; publish the underlying connection with a well-defined happens-before relationship; signal completion to all waiters (e.g. a closed ready channel); coordinate cancellation/Close with a concurrently completing handshake so a late-created connection cannot be left open. Avoid holding locks across blocking network operations.

Implement all deadline methods explicitly before dial. Preserve pending deadlines and apply them when a real connection is published, or provide another documented non-panicking implementation respecting the net.Conn contract. Close must cancel blocked reads/handshakes, and must be idempotent.

flowchart TD
 A[Deferred connection] --> B[Serialized lifecycle state]
 B --> C[Single handshake]
 B --> D[Store pending deadlines]
 C --> E{Already closed?}
 E -- Yes --> F[Close late connection]
 E -- No --> G[Publish Conn and apply deadlines]
 G --> H[Notify waiters]
 B --> I[Close cancels and wakes waiters]

Regression tests

Add package-level tests in transport/internet/websocket: concurrent Read/Close, Close during handshake success/failure, pre-dial deadline methods, expired read/write deadlines, concurrent address reads during dial, repeated Close, and no orphan connection after cancellation. Use a deterministic fake dial or loopback server, go test -race, and verify early data is still sent only once.

Production context (not causal evidence)

A separately monitored busy Xray server using VLESS+WebSocket/TLS inbounds showed WebSocket/TLS read stacks scaling with connection counts. That is normal for active connections and does not establish that this outbound early-data bug occurred in production. This report is based on the reproducible deferred-wrapper defect, not the production heap or an assertion of a goroutine leak. No production configuration or private logs are needed for the reduced test. Full latest-release integration validation remains to be done.

Dominant language
Go
Stars
43.3k
Forks
6.2k
Avg merge
2d 11h
Merged PRs (30d)
55

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from XTLS/Xray-core

All issues in XTLS/Xray-core

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.