From 6b32ae6a6f08f95a741949fd8098ebfe1ee0e8ce Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 9 Sep 2021 18:37:38 -0500 Subject: [PATCH 1/4] fix: ensure error chan is buffered in (*Dialer).negotiate --- wsnet/dial.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wsnet/dial.go b/wsnet/dial.go index f4410e79..5b554cb1 100644 --- a/wsnet/dial.go +++ b/wsnet/dial.go @@ -193,7 +193,7 @@ type Dialer struct { func (d *Dialer) negotiate(ctx context.Context) (err error) { var ( decoder = json.NewDecoder(d.conn) - errCh = make(chan error) + errCh = make(chan error, 1) // If candidates are sent before an offer, we place them here. // We currently have no assurances to ensure this can't happen, // so it's better to buffer and process than fail. From 62bf89e1a8992dccc2682a17567db64cecd894cf Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 9 Sep 2021 18:45:06 -0500 Subject: [PATCH 2/4] fixup! fix: ensure error chan is buffered in (*Dialer).negotiate --- wsnet/dial.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/wsnet/dial.go b/wsnet/dial.go index 5b554cb1..7bc41535 100644 --- a/wsnet/dial.go +++ b/wsnet/dial.go @@ -201,16 +201,12 @@ func (d *Dialer) negotiate(ctx context.Context) (err error) { ) go func() { defer close(errCh) - defer func() { - _ = d.conn.Close() - }() + defer func() { _ = d.conn.Close() }() err := waitForConnectionOpen(context.Background(), d.rtc) if err != nil { d.log.Debug(ctx, "negotiation error", slog.Error(err)) - if errors.Is(err, context.DeadlineExceeded) { - _ = d.conn.Close() - } + errCh <- fmt.Errorf("wait for connection to open: %w", err) return } From e07aa108ae3cf0172dcbc6f3f4e4455612013bd2 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 9 Sep 2021 18:46:40 -0500 Subject: [PATCH 3/4] fixup! fix: ensure error chan is buffered in (*Dialer).negotiate --- wsnet/dial.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/wsnet/dial.go b/wsnet/dial.go index 7bc41535..7fd702d3 100644 --- a/wsnet/dial.go +++ b/wsnet/dial.go @@ -327,14 +327,17 @@ func (d *Dialer) Ping(ctx context.Context) error { return err } } + d.pingMut.Lock() defer d.pingMut.Unlock() + d.log.Debug(ctx, "sending ping") _, err = d.ctrlrw.Write([]byte{'a'}) if err != nil { return fmt.Errorf("write: %w", err) } - errCh := make(chan error) + + errCh := make(chan error, 1) go func() { // There's a race in which connections can get lost-mid ping // in which case this would block forever. @@ -342,6 +345,7 @@ func (d *Dialer) Ping(ctx context.Context) error { _, err = d.ctrlrw.Read(make([]byte, 4)) errCh <- err }() + ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15) defer cancelFunc() select { From 944da7d5134f68f52d570ade99825664e9d35062 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 9 Sep 2021 18:48:04 -0500 Subject: [PATCH 4/4] fixup! fix: ensure error chan is buffered in (*Dialer).negotiate --- wsnet/dial.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wsnet/dial.go b/wsnet/dial.go index 7fd702d3..53b4a186 100644 --- a/wsnet/dial.go +++ b/wsnet/dial.go @@ -346,8 +346,9 @@ func (d *Dialer) Ping(ctx context.Context) error { errCh <- err }() - ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15) - defer cancelFunc() + ctx, cancel := context.WithTimeout(ctx, time.Second*15) + defer cancel() + select { case err := <-errCh: return err