Skip to content

Commit

Permalink
fix(tcpreuse): handle connection that failed to be sampled
Browse files Browse the repository at this point in the history
  • Loading branch information
aschmahmann committed Nov 12, 2024
1 parent 10045d1 commit 6eae9ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 9 additions & 9 deletions p2p/transport/tcpreuse/demultiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,35 @@ func (t DemultiplexedConnType) IsKnown() bool {

// identifyConnType attempts to identify the connection type by peeking at the
// first few bytes.
// It Callers must not use the passed in Conn after this
// function returns. if an error is returned, the connection will be closed.
// Its Callers must not use the passed in Conn after this function returns.
// If an error is returned, the connection will be closed.
func identifyConnType(c manet.Conn) (DemultiplexedConnType, manet.Conn, error) {
if err := c.SetReadDeadline(time.Now().Add(identifyConnTimeout)); err != nil {
closeErr := c.Close()
return 0, nil, errors.Join(err, closeErr)
}

s, c, err := sampledconn.PeekBytes(c)
s, peekableConn, err := sampledconn.PeekBytes(c)
if err != nil {
closeErr := c.Close()
return 0, nil, errors.Join(err, closeErr)
}

if err := c.SetReadDeadline(time.Time{}); err != nil {
closeErr := c.Close()
if err := peekableConn.SetReadDeadline(time.Time{}); err != nil {
closeErr := peekableConn.Close()
return 0, nil, errors.Join(err, closeErr)
}

if IsMultistreamSelect(s) {
return DemultiplexedConnType_MultistreamSelect, c, nil
return DemultiplexedConnType_MultistreamSelect, peekableConn, nil
}
if IsTLS(s) {
return DemultiplexedConnType_TLS, c, nil
return DemultiplexedConnType_TLS, peekableConn, nil
}
if IsHTTP(s) {
return DemultiplexedConnType_HTTP, c, nil
return DemultiplexedConnType_HTTP, peekableConn, nil
}
return DemultiplexedConnType_Unknown, c, nil
return DemultiplexedConnType_Unknown, peekableConn, nil
}

// Matchers are implemented here instead of in the transports so we can easily fuzz them together.
Expand Down
2 changes: 0 additions & 2 deletions p2p/transport/tcpreuse/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ func (m *multiplexedListener) run() error {
t, c, err := identifyConnType(c)
if err != nil {
connScope.Done()
closeErr := c.Close()
err = errors.Join(err, closeErr)
log.Debugf("error demultiplexing connection: %s", err.Error())
return
}
Expand Down

0 comments on commit 6eae9ab

Please sign in to comment.