Skip to content

Commit

Permalink
transport: don't add connection to multiplexer if init fails (#3931)
Browse files Browse the repository at this point in the history
* Remove conn from multiplexer when (*Transport).init fails

* Transport: AddConn to multiplexer directly before start listening

* Update transport_test.go

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
  • Loading branch information
kelmenhorst and marten-seemann committed Jun 29, 2023
1 parent 435444a commit 0c54d41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 1 addition & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ func (t *Transport) DialEarly(ctx context.Context, addr net.Addr, tlsConf *tls.C

func (t *Transport) init(isServer bool) error {
t.initOnce.Do(func() {
getMultiplexer().AddConn(t.Conn)

var conn rawConn
if c, ok := t.Conn.(rawConn); ok {
conn = c
Expand Down Expand Up @@ -212,6 +210,7 @@ func (t *Transport) init(isServer bool) error {
t.connIDGenerator = &protocol.DefaultConnectionIDGenerator{ConnLen: t.connIDLen}
}

getMultiplexer().AddConn(t.Conn)
go t.listen(conn)
go t.runSendQueue()
})
Expand Down
24 changes: 24 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"errors"
"net"
"syscall"
"time"

mocklogging "github.com/quic-go/quic-go/internal/mocks/logging"
Expand Down Expand Up @@ -307,4 +308,27 @@ var _ = Describe("Transport", func() {
pconn.EXPECT().Close()
Expect(tr.Close()).To(Succeed())
})

It("doesn't add the PacketConn to the multiplexer if (*Transport).init fails", func() {
packetChan := make(chan packetToRead)
pconn := newMockPacketConn(packetChan)
syscallconn := &mockSyscallConn{pconn}

tr := &Transport{
Conn: syscallconn,
}

err := tr.init(false)
Expect(err).To(HaveOccurred())
conns := getMultiplexer().(*connMultiplexer).conns
Expect(len(conns)).To(BeZero())
})
})

type mockSyscallConn struct {
net.PacketConn
}

func (c *mockSyscallConn) SyscallConn() (syscall.RawConn, error) {
return nil, errors.New("mocked")
}

0 comments on commit 0c54d41

Please sign in to comment.