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

Commit 4c89550

Browse files
authored
fix: ensure error chan is buffered in (*Dialer).negotiate (#437)
1 parent d2df9e1 commit 4c89550

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
@@ -193,24 +193,20 @@ type Dialer struct {
193193
func (d *Dialer) negotiate(ctx context.Context) (err error) {
194194
var (
195195
decoder = json.NewDecoder(d.conn)
196-
errCh = make(chan error)
196+
errCh = make(chan error, 1)
197197
// If candidates are sent before an offer, we place them here.
198198
// We currently have no assurances to ensure this can't happen,
199199
// so it's better to buffer and process than fail.
200200
pendingCandidates = []webrtc.ICECandidateInit{}
201201
)
202202
go func() {
203203
defer close(errCh)
204-
defer func() {
205-
_ = d.conn.Close()
206-
}()
204+
defer func() { _ = d.conn.Close() }()
207205

208206
err := waitForConnectionOpen(context.Background(), d.rtc)
209207
if err != nil {
210208
d.log.Debug(ctx, "negotiation error", slog.Error(err))
211-
if errors.Is(err, context.DeadlineExceeded) {
212-
_ = d.conn.Close()
213-
}
209+
214210
errCh <- fmt.Errorf("wait for connection to open: %w", err)
215211
return
216212
}
@@ -331,23 +327,28 @@ func (d *Dialer) Ping(ctx context.Context) error {
331327
return err
332328
}
333329
}
330+
334331
d.pingMut.Lock()
335332
defer d.pingMut.Unlock()
333+
336334
d.log.Debug(ctx, "sending ping")
337335
_, err = d.ctrlrw.Write([]byte{'a'})
338336
if err != nil {
339337
return fmt.Errorf("write: %w", err)
340338
}
341-
errCh := make(chan error)
339+
340+
errCh := make(chan error, 1)
342341
go func() {
343342
// There's a race in which connections can get lost-mid ping
344343
// in which case this would block forever.
345344
defer close(errCh)
346345
_, err = d.ctrlrw.Read(make([]byte, 4))
347346
errCh <- err
348347
}()
349-
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
350-
defer cancelFunc()
348+
349+
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
350+
defer cancel()
351+
351352
select {
352353
case err := <-errCh:
353354
return err

0 commit comments

Comments
 (0)