Skip to content

Commit 299a63f

Browse files
committed
fix: quic underlay packetConn maybe not closed in doh3/doq
1 parent acc4f62 commit 299a63f

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

dns/doh.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,11 @@ func (doh *dnsOverHTTPS) dialQuic(ctx context.Context, addr string, tlsCfg *tls.
554554
IP: net.ParseIP(ip),
555555
Port: portInt,
556556
}
557-
conn, err := doh.dialer.ListenPacket(ctx, "udp", addr)
557+
packetConn, err := doh.dialer.ListenPacket(ctx, "udp", addr)
558558
if err != nil {
559559
return nil, err
560560
}
561-
transport := quic.Transport{Conn: conn}
561+
transport := quic.Transport{Conn: packetConn}
562562
transport.SetCreatedConn(true) // auto close conn
563563
transport.SetSingleUse(true) // auto close transport
564564
tlsCfg = tlsCfg.Clone()
@@ -568,7 +568,12 @@ func (doh *dnsOverHTTPS) dialQuic(ctx context.Context, addr string, tlsCfg *tls.
568568
// It's ok if net.SplitHostPort returns an error - it could be a hostname/IP address without a port.
569569
tlsCfg.ServerName = doh.url.Host
570570
}
571-
return transport.DialEarly(ctx, &udpAddr, tlsCfg, cfg)
571+
quicConn, err := transport.DialEarly(ctx, &udpAddr, tlsCfg, cfg)
572+
if err != nil {
573+
_ = packetConn.Close()
574+
return nil, err
575+
}
576+
return quicConn, nil
572577
}
573578

574579
// probeH3 runs a test to check whether QUIC is faster than TLS for this

dns/doq.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (doq *dnsOverQUIC) openStream(ctx context.Context, conn *quic.Conn) (*quic.
279279
}
280280

281281
// openConnection opens a new QUIC connection.
282-
func (doq *dnsOverQUIC) openConnection(ctx context.Context) (conn *quic.Conn, err error) {
282+
func (doq *dnsOverQUIC) openConnection(ctx context.Context) (quicConn *quic.Conn, err error) {
283283
// we're using bootstrapped address instead of what's passed to the function
284284
// it does not create an actual connection, but it helps us determine
285285
// what IP is actually reachable (when there're v4/v6 addresses).
@@ -298,7 +298,7 @@ func (doq *dnsOverQUIC) openConnection(ctx context.Context) (conn *quic.Conn, er
298298

299299
p, err := strconv.Atoi(port)
300300
udpAddr := net.UDPAddr{IP: net.ParseIP(ip), Port: p}
301-
udp, err := doq.dialer.ListenPacket(ctx, "udp", addr)
301+
packetConn, err := doq.dialer.ListenPacket(ctx, "udp", addr)
302302
if err != nil {
303303
return nil, err
304304
}
@@ -322,15 +322,16 @@ func (doq *dnsOverQUIC) openConnection(ctx context.Context) (conn *quic.Conn, er
322322
return nil, err
323323
}
324324

325-
transport := quic.Transport{Conn: udp}
325+
transport := quic.Transport{Conn: packetConn}
326326
transport.SetCreatedConn(true) // auto close conn
327327
transport.SetSingleUse(true) // auto close transport
328-
conn, err = transport.Dial(ctx, &udpAddr, tlsConfig, doq.getQUICConfig())
328+
quicConn, err = transport.Dial(ctx, &udpAddr, tlsConfig, doq.getQUICConfig())
329329
if err != nil {
330+
_ = packetConn.Close()
330331
return nil, fmt.Errorf("opening quic connection to %s: %w", doq.addr, err)
331332
}
332333

333-
return conn, nil
334+
return quicConn, nil
334335
}
335336

336337
// closeConnWithError closes the active connection with error to make sure that

0 commit comments

Comments
 (0)