From ce75fab19b0c2de89342f9f929dd221b4e7d235f Mon Sep 17 00:00:00 2001 From: roach Date: Thu, 22 Oct 2026 15:26:08 -0500 Subject: [PATCH] Match QUICConn's NextEvent and Close in UQUICConn Close canceled the handshake but never read signalc, so a handshake blocked waiting for peer data could not observe the cancellation and Close hung forever. NextEvent never reported a failed handshake as QUICErrorEvent. Apply Go's QUICConn versions of both: Close unblocks the handshake goroutine before waiting for it, and NextEvent reports the handshake error once. From MetaCubeX/utls 8853e10 ("keep in sync of UQUICConn and QUICConn"). --- u_quic.go | 12 ++++++- u_quic_conn_test.go | 86 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 u_quic_conn_test.go diff --git a/u_quic.go b/u_quic.go index 6493ddbdb3c..4d2aeefaf19 100644 --- a/u_quic.go +++ b/u_quic.go @@ -73,6 +73,15 @@ func (q *UQUICConn) NextEvent() QUICEvent { <-qs.signalc <-qs.blockedc } + if err := q.conn.handshakeErr; err != nil { + if qs.errorReturned { + return QUICEvent{Kind: QUICNoEvent} + } + qs.errorReturned = true + qs.events = nil + qs.nextEvent = 0 + return QUICEvent{Kind: QUICErrorEvent, Err: q.conn.handshakeErr} + } if qs.nextEvent >= len(qs.events) { qs.events = qs.events[:0] qs.nextEvent = 0 @@ -86,10 +95,11 @@ func (q *UQUICConn) NextEvent() QUICEvent { // Close closes the connection and stops any in-progress handshake. func (q *UQUICConn) Close() error { - if q.conn.quic.cancel == nil { + if q.conn.quic.ctx == nil { return nil // never started } q.conn.quic.cancel() + <-q.conn.quic.signalc for range q.conn.quic.blockedc { // Wait for the handshake goroutine to return. } diff --git a/u_quic_conn_test.go b/u_quic_conn_test.go new file mode 100644 index 00000000000..5dcf490c992 --- /dev/null +++ b/u_quic_conn_test.go @@ -0,0 +1,86 @@ +package tls + +import ( + "context" + "errors" + "testing" + "time" +) + +// Close must stop a handshake that is blocked waiting for peer data, as +// QUICConn.Close does, rather than wait for it forever. +func TestUQUICCloseWhileWaitingForData(t *testing.T) { + config := &QUICConfig{TLSConfig: testConfigClient.Clone()} + config.TLSConfig.MinVersion = VersionTLS13 + client := UQUICClient(config, HelloGolang) + client.SetTransportParameters(nil) + if err := client.Start(context.Background()); err != nil { + t.Fatal(err) + } + for client.NextEvent().Kind != QUICNoEvent { + } + + closed := make(chan error, 1) + go func() { closed <- client.Close() }() + select { + case err := <-closed: + if !errors.Is(err, alertCloseNotify) { + t.Errorf("Close() = %v, want alertCloseNotify", err) + } + case <-time.After(10 * time.Second): + t.Fatal("Close() did not return while the handshake waited for data") + } +} + +// A failed handshake is reported once through NextEvent as a QUICErrorEvent, +// as QUICConn does, so a QUIC stack can close the connection with its alert. +func TestUQUICHandshakeErrorEvent(t *testing.T) { + clientConfig := &QUICConfig{TLSConfig: testConfigClient.Clone()} + clientConfig.TLSConfig.MinVersion = VersionTLS13 + clientConfig.TLSConfig.ServerName = "name" // not in the server certificate + client := UQUICClient(clientConfig, HelloGolang) + defer client.Close() + client.SetTransportParameters(nil) + + serverConfig := &QUICConfig{TLSConfig: testConfigServer.Clone()} + serverConfig.TLSConfig.MinVersion = VersionTLS13 + server := QUICServer(serverConfig) + defer server.Close() + server.SetTransportParameters(nil) + + if err := client.Start(t.Context()); err != nil { + t.Fatal(err) + } + if err := server.Start(t.Context()); err != nil { + t.Fatal(err) + } + + var handshakeErr error + for handshakeErr == nil { + e := client.NextEvent() + if e.Kind == QUICNoEvent { + if e = server.NextEvent(); e.Kind == QUICNoEvent { + t.Fatal("handshake stalled without an error") + } + if e.Kind == QUICWriteData { + handshakeErr = client.HandleData(e.Level, e.Data) + } + continue + } + if e.Kind == QUICWriteData { + if err := server.HandleData(e.Level, e.Data); err != nil { + t.Fatalf("server: %v", err) + } + } + } + if _, ok := errors.AsType[*CertificateVerificationError](handshakeErr); !ok { + t.Fatalf("handshake error %v, want a CertificateVerificationError", handshakeErr) + } + + if e := client.NextEvent(); e.Kind != QUICErrorEvent || e.Err != handshakeErr { + t.Fatalf("NextEvent() = %v (%v), want QUICErrorEvent with the handshake error", e.Kind, e.Err) + } + if e := client.NextEvent(); e.Kind != QUICNoEvent { + t.Fatalf("NextEvent() after the error = %v, want QUICNoEvent", e.Kind) + } +}