From 85c37b473a2ef9bd994ebf6e1ee9f68319e7f850 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Sun, 12 Jun 2022 21:41:01 +0800 Subject: [PATCH] fix: DOQ blocked dns return result because DOQ goroutine leak --- dns/doq.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/dns/doq.go b/dns/doq.go index 265abc24..b9bddf3b 100644 --- a/dns/doq.go +++ b/dns/doq.go @@ -93,7 +93,7 @@ func isActive(s quic.Connection) bool { // getSession - opens or returns an existing quic.Connection // useCached - if true and cached session exists, return it right away // otherwise - forcibly creates a new session -func (dc *quicClient) getSession() (quic.Connection, error) { +func (dc *quicClient) getSession(ctx context.Context) (quic.Connection, error) { var session quic.Connection dc.RLock() session = dc.session @@ -111,14 +111,14 @@ func (dc *quicClient) getSession() (quic.Connection, error) { defer dc.Unlock() var err error - session, err = dc.openSession() + session, err = dc.openSession(ctx) if err != nil { // This does not look too nice, but QUIC (or maybe quic-go) // doesn't seem stable enough. // Maybe retransmissions aren't fully implemented in quic-go? // Anyways, the simple solution is to make a second try when // it fails to open the QUIC session. - session, err = dc.openSession() + session, err = dc.openSession(ctx) if err != nil { return nil, err } @@ -127,7 +127,7 @@ func (dc *quicClient) getSession() (quic.Connection, error) { return session, nil } -func (dc *quicClient) openSession() (quic.Connection, error) { +func (dc *quicClient) openSession(ctx context.Context) (quic.Connection, error) { tlsConfig := &tls.Config{ InsecureSkipVerify: true, NextProtos: []string{ @@ -163,12 +163,12 @@ func (dc *quicClient) openSession() (quic.Connection, error) { udpAddr := net.UDPAddr{IP: ip.AsSlice(), Port: p} if dc.proxyAdapter == "" { - udp, err = dialer.ListenPacket(context.Background(), "udp", "") + udp, err = dialer.ListenPacket(ctx, "udp", "") if err != nil { return nil, err } } else { - conn, err := dialContextExtra(context.Background(), dc.proxyAdapter, "udp", ip, port) + conn, err := dialContextExtra(ctx, dc.proxyAdapter, "udp", ip, port) if err != nil { return nil, err } @@ -181,7 +181,7 @@ func (dc *quicClient) openSession() (quic.Connection, error) { udp = wrapConn } - session, err := quic.Dial(udp, &udpAddr, host, tlsConfig, quicConfig) + session, err := quic.DialContext(ctx, udp, &udpAddr, host, tlsConfig, quicConfig) if err != nil { return nil, fmt.Errorf("failed to open QUIC session: %w", err) } @@ -190,7 +190,7 @@ func (dc *quicClient) openSession() (quic.Connection, error) { } func (dc *quicClient) openStream(ctx context.Context) (quic.Stream, error) { - session, err := dc.getSession() + session, err := dc.getSession(ctx) if err != nil { return nil, err }