Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 53e7c33

Browse files
authored
fix: Close conn if context deadline is exceeded (#395)
* fix: Close conn if context deadline is exceeded Previously the error occurred but never propogated due to blocking fromm the read loop in negotiate. Now, connection timeouts can properly occur. * Fixes
1 parent 3e78729 commit 53e7c33

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

wsnet/dial.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,16 @@ func (d *Dialer) negotiate(ctx context.Context) (err error) {
147147
// so it's better to buffer and process than fail.
148148
pendingCandidates = []webrtc.ICECandidateInit{}
149149
)
150-
151150
go func() {
152151
defer close(errCh)
153152
defer func() {
154153
_ = d.conn.Close()
155154
}()
156155
err := waitForConnectionOpen(ctx, d.rtc)
157156
if err != nil {
157+
if errors.Is(err, context.DeadlineExceeded) {
158+
_ = d.conn.Close()
159+
}
158160
errCh <- err
159161
return
160162
}

wsnet/dial_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"strconv"
1111
"testing"
12+
"time"
1213

1314
"cdr.dev/slog/sloggers/slogtest"
1415
"github.com/pion/ice/v2"
@@ -51,6 +52,18 @@ func ExampleDial_basic() {
5152
}
5253

5354
func TestDial(t *testing.T) {
55+
t.Run("Timeout", func(t *testing.T) {
56+
t.Parallel()
57+
58+
connectAddr, _ := createDumbBroker(t)
59+
60+
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Millisecond*50)
61+
defer cancelFunc()
62+
dialer, err := DialWebsocket(ctx, connectAddr, nil, nil)
63+
require.True(t, errors.Is(err, context.DeadlineExceeded))
64+
require.Error(t, dialer.conn.Close(), "already wrote close")
65+
})
66+
5467
t.Run("Ping", func(t *testing.T) {
5568
t.Parallel()
5669

wsnet/rtc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func waitForConnectionOpen(ctx context.Context, conn *webrtc.PeerConnection) err
256256
})
257257
<-ctx.Done()
258258
if ctx.Err() == context.DeadlineExceeded {
259-
return ctx.Err()
259+
return context.DeadlineExceeded
260260
}
261261
return nil
262262
}

wsnet/wsnet_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ func createDumbBroker(t testing.TB) (connectAddr string, listenAddr string) {
6868
mut.Lock()
6969
defer mut.Unlock()
7070
if sess == nil {
71-
t.Error("listen not called")
71+
// We discard inbound to emulate a pubsub where we don't know if anyone
72+
// is listening on the other side.
73+
_, _ = io.Copy(io.Discard, nc)
7274
return
7375
}
7476
oc, err := sess.Open()

0 commit comments

Comments
 (0)