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 2954cab

Browse files
coadlerdeansheather
authored andcommitted
fix: ensure error chan is buffered in (*Dialer).negotiate (#437)
(cherry picked from commit 4c89550) (cherry picked from commit cb15635)
1 parent 6091ce3 commit 2954cab

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

wsnet/dial.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,24 +192,20 @@ type Dialer struct {
192192
func (d *Dialer) negotiate(ctx context.Context) (err error) {
193193
var (
194194
decoder = json.NewDecoder(d.conn)
195-
errCh = make(chan error)
195+
errCh = make(chan error, 1)
196196
// If candidates are sent before an offer, we place them here.
197197
// We currently have no assurances to ensure this can't happen,
198198
// so it's better to buffer and process than fail.
199199
pendingCandidates = []webrtc.ICECandidateInit{}
200200
)
201201
go func() {
202202
defer close(errCh)
203-
defer func() {
204-
_ = d.conn.Close()
205-
}()
203+
defer func() { _ = d.conn.Close() }()
206204

207205
err := waitForConnectionOpen(context.Background(), d.rtc)
208206
if err != nil {
209207
d.log.Debug(ctx, "negotiation error", slog.Error(err))
210-
if errors.Is(err, context.DeadlineExceeded) {
211-
_ = d.conn.Close()
212-
}
208+
213209
errCh <- fmt.Errorf("wait for connection to open: %w", err)
214210
return
215211
}
@@ -325,23 +321,28 @@ func (d *Dialer) Ping(ctx context.Context) error {
325321
return err
326322
}
327323
}
324+
328325
d.pingMut.Lock()
329326
defer d.pingMut.Unlock()
327+
330328
d.log.Debug(ctx, "sending ping")
331329
_, err = d.ctrlrw.Write([]byte{'a'})
332330
if err != nil {
333331
return fmt.Errorf("write: %w", err)
334332
}
335-
errCh := make(chan error)
333+
334+
errCh := make(chan error, 1)
336335
go func() {
337336
// There's a race in which connections can get lost-mid ping
338337
// in which case this would block forever.
339338
defer close(errCh)
340339
_, err = d.ctrlrw.Read(make([]byte, 4))
341340
errCh <- err
342341
}()
343-
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
344-
defer cancelFunc()
342+
343+
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
344+
defer cancel()
345+
345346
select {
346347
case err := <-errCh:
347348
return err

0 commit comments

Comments
 (0)