Skip to content

Commit 40d204f

Browse files
committed
ci: Improve peer logging to help identify race
1 parent 2b922b1 commit 40d204f

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

peer/conn.go

+38-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"crypto/rand"
7+
"crypto/sha256"
78
"io"
89
"sync"
910
"time"
@@ -140,6 +141,26 @@ type Conn struct {
140141

141142
func (c *Conn) init() error {
142143
c.rtc.OnNegotiationNeeded(c.negotiate)
144+
c.rtc.OnICEConnectionStateChange(func(iceConnectionState webrtc.ICEConnectionState) {
145+
c.closeMutex.Lock()
146+
defer c.closeMutex.Unlock()
147+
if c.isClosed() {
148+
return
149+
}
150+
151+
c.opts.Logger.Debug(context.Background(), "ice connection updated",
152+
slog.F("state", iceConnectionState))
153+
})
154+
c.rtc.OnICEGatheringStateChange(func(iceGatherState webrtc.ICEGathererState) {
155+
c.closeMutex.Lock()
156+
defer c.closeMutex.Unlock()
157+
if c.isClosed() {
158+
return
159+
}
160+
161+
c.opts.Logger.Debug(context.Background(), "ice gathering state updated",
162+
slog.F("state", iceGatherState))
163+
})
143164
c.rtc.OnICECandidate(func(iceCandidate *webrtc.ICECandidate) {
144165
if iceCandidate == nil {
145166
return
@@ -169,8 +190,7 @@ func (c *Conn) init() error {
169190
}
170191

171192
c.opts.Logger.Debug(context.Background(), "rtc connection updated",
172-
slog.F("state", pcs),
173-
slog.F("ice", c.rtc.ICEConnectionState()))
193+
slog.F("state", pcs))
174194

175195
switch pcs {
176196
case webrtc.PeerConnectionStateDisconnected:
@@ -311,15 +331,22 @@ func (c *Conn) negotiate() {
311331
c.pendingCandidatesMutex.Lock()
312332
defer c.pendingCandidatesMutex.Unlock()
313333
for _, pendingCandidate := range c.pendingRemoteCandidates {
314-
c.opts.Logger.Debug(context.Background(), "flushing remote candidate")
334+
hash := sha256.Sum224([]byte(pendingCandidate.Candidate))
335+
c.opts.Logger.Debug(context.Background(), "flushing buffered remote candidate",
336+
slog.F("hash", hash),
337+
slog.F("length", len(pendingCandidate.Candidate)),
338+
)
315339
err := c.rtc.AddICECandidate(pendingCandidate)
316340
if err != nil {
317-
_ = c.CloseWithError(xerrors.Errorf("flush pending candidates: %w", err))
341+
_ = c.CloseWithError(xerrors.Errorf("flush pending remote candidate: %w", err))
318342
return
319343
}
320344
}
345+
c.opts.Logger.Debug(context.Background(), "flushed buffered remote candidates",
346+
slog.F("count", len(c.pendingRemoteCandidates)),
347+
)
321348
c.pendingCandidatesFlushed = true
322-
c.opts.Logger.Debug(context.Background(), "flushed remote candidates")
349+
c.pendingRemoteCandidates = make([]webrtc.ICECandidateInit, 0)
323350
}
324351

325352
// LocalCandidate returns a channel that emits when a local candidate
@@ -332,12 +359,16 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
332359
func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) error {
333360
c.pendingCandidatesMutex.Lock()
334361
defer c.pendingCandidatesMutex.Unlock()
362+
fields := []slog.Field{
363+
slog.F("hash", sha256.Sum224([]byte(i.Candidate))),
364+
slog.F("length", len(i.Candidate)),
365+
}
335366
if !c.pendingCandidatesFlushed {
336-
c.opts.Logger.Debug(context.Background(), "adding remote candidate to buffer")
367+
c.opts.Logger.Debug(context.Background(), "bufferring remote candidate", fields...)
337368
c.pendingRemoteCandidates = append(c.pendingRemoteCandidates, i)
338369
return nil
339370
}
340-
c.opts.Logger.Debug(context.Background(), "adding remote candidate")
371+
c.opts.Logger.Debug(context.Background(), "adding remote candidate", fields...)
341372
return c.rtc.AddICECandidate(i)
342373
}
343374

0 commit comments

Comments
 (0)