Skip to content

Commit bb6a565

Browse files
committed
Fix missing handshake timeout
1 parent 1cbd1ab commit bb6a565

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

client.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (c *Client) openStream(ctx context.Context) (net.Conn, error) {
113113
if err != nil {
114114
continue
115115
}
116-
stream, err = session.Open()
116+
stream, err = session.OpenContext(ctx)
117117
if err != nil {
118118
continue
119119
}
@@ -168,6 +168,8 @@ func (c *Client) offer(ctx context.Context) (abstractSession, error) {
168168
}
169169

170170
func (c *Client) offerNew(ctx context.Context) (abstractSession, error) {
171+
ctx, cancel := context.WithTimeout(ctx, TCPTimeout)
172+
defer cancel()
171173
conn, err := c.dialer.DialContext(ctx, N.NetworkTCP, Destination)
172174
if err != nil {
173175
return nil, err
@@ -192,7 +194,7 @@ func (c *Client) offerNew(ctx context.Context) (abstractSession, error) {
192194
return nil, err
193195
}
194196
if c.brutal.Enabled {
195-
err = c.brutalExchange(conn, session)
197+
err = c.brutalExchange(ctx, conn, session)
196198
if err != nil {
197199
conn.Close()
198200
session.Close()
@@ -203,8 +205,8 @@ func (c *Client) offerNew(ctx context.Context) (abstractSession, error) {
203205
return session, nil
204206
}
205207

206-
func (c *Client) brutalExchange(sessionConn net.Conn, session abstractSession) error {
207-
stream, err := session.Open()
208+
func (c *Client) brutalExchange(ctx context.Context, sessionConn net.Conn, session abstractSession) error {
209+
stream, err := session.OpenContext(ctx)
208210
if err != nil {
209211
return err
210212
}

h2mux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (s *h2MuxServerSession) ServeHTTP(writer http.ResponseWriter, request *http
6464
}
6565
}
6666

67-
func (s *h2MuxServerSession) Open() (net.Conn, error) {
67+
func (s *h2MuxServerSession) OpenContext(ctx context.Context) (net.Conn, error) {
6868
return nil, os.ErrInvalid
6969
}
7070

@@ -197,13 +197,14 @@ func (s *h2MuxClientSession) MarkDead(conn *http2.ClientConn) {
197197
s.Close()
198198
}
199199

200-
func (s *h2MuxClientSession) Open() (net.Conn, error) {
200+
func (s *h2MuxClientSession) OpenContext(ctx context.Context) (net.Conn, error) {
201201
pipeInReader, pipeInWriter := io.Pipe()
202202
request := &http.Request{
203203
Method: http.MethodConnect,
204204
Body: pipeInReader,
205205
URL: &url.URL{Scheme: "https", Host: "localhost"},
206206
}
207+
request = request.WithContext(ctx)
207208
conn := newLateHTTPConn(pipeInWriter)
208209
go func() {
209210
response, err := s.transport.RoundTrip(request)

session.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mux
22

33
import (
4+
"context"
45
"io"
56
"net"
67
"reflect"
@@ -12,7 +13,7 @@ import (
1213
)
1314

1415
type abstractSession interface {
15-
Open() (net.Conn, error)
16+
OpenContext(ctx context.Context) (net.Conn, error)
1617
Accept() (net.Conn, error)
1718
NumStreams() int
1819
Close() error
@@ -80,7 +81,7 @@ type smuxSession struct {
8081
*smux.Session
8182
}
8283

83-
func (s *smuxSession) Open() (net.Conn, error) {
84+
func (s *smuxSession) OpenContext(context.Context) (net.Conn, error) {
8485
return s.OpenStream()
8586
}
8687

@@ -96,6 +97,10 @@ type yamuxSession struct {
9697
*yamux.Session
9798
}
9899

100+
func (y *yamuxSession) OpenContext(context.Context) (net.Conn, error) {
101+
return y.OpenStream()
102+
}
103+
99104
func (y *yamuxSession) CanTakeNewRequest() bool {
100105
return true
101106
}

0 commit comments

Comments
 (0)