Skip to content

Commit 5367d93

Browse files
authored
ci: Improve peer logging to help identify race (#93)
* ci: Improve peer logging to help identify race * Remove mutex locks * Add hash to write
1 parent 3e88f15 commit 5367d93

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

peer/conn.go

+48-9
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,15 +141,43 @@ 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+
// Close must be locked here otherwise log output can appear
146+
// after the connection has been closed.
147+
c.closeMutex.Lock()
148+
defer c.closeMutex.Unlock()
149+
if c.isClosed() {
150+
return
151+
}
152+
153+
c.opts.Logger.Debug(context.Background(), "ice connection state updated",
154+
slog.F("state", iceConnectionState))
155+
})
156+
c.rtc.OnICEGatheringStateChange(func(iceGatherState webrtc.ICEGathererState) {
157+
// Close can't be locked here, because this is triggered
158+
// when close is called. It doesn't appear this get's
159+
// executed after close though, so it shouldn't cause
160+
// problems.
161+
if c.isClosed() {
162+
return
163+
}
164+
165+
c.opts.Logger.Debug(context.Background(), "ice gathering state updated",
166+
slog.F("state", iceGatherState))
167+
})
143168
c.rtc.OnICECandidate(func(iceCandidate *webrtc.ICECandidate) {
144169
if iceCandidate == nil {
145170
return
146171
}
147-
c.opts.Logger.Debug(context.Background(), "adding local candidate")
172+
json := iceCandidate.ToJSON()
173+
c.opts.Logger.Debug(context.Background(), "writing candidate to channel",
174+
slog.F("hash", sha256.Sum224([]byte(json.Candidate))),
175+
slog.F("length", len(json.Candidate)),
176+
)
148177
select {
149178
case <-c.closed:
150179
break
151-
case c.localCandidateChannel <- iceCandidate.ToJSON():
180+
case c.localCandidateChannel <- json:
152181
}
153182
})
154183
c.rtc.OnDataChannel(func(dc *webrtc.DataChannel) {
@@ -169,8 +198,7 @@ func (c *Conn) init() error {
169198
}
170199

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

175203
switch pcs {
176204
case webrtc.PeerConnectionStateDisconnected:
@@ -311,15 +339,22 @@ func (c *Conn) negotiate() {
311339
c.pendingCandidatesMutex.Lock()
312340
defer c.pendingCandidatesMutex.Unlock()
313341
for _, pendingCandidate := range c.pendingRemoteCandidates {
314-
c.opts.Logger.Debug(context.Background(), "flushing remote candidate")
342+
hash := sha256.Sum224([]byte(pendingCandidate.Candidate))
343+
c.opts.Logger.Debug(context.Background(), "flushing buffered remote candidate",
344+
slog.F("hash", hash),
345+
slog.F("length", len(pendingCandidate.Candidate)),
346+
)
315347
err := c.rtc.AddICECandidate(pendingCandidate)
316348
if err != nil {
317-
_ = c.CloseWithError(xerrors.Errorf("flush pending candidates: %w", err))
349+
_ = c.CloseWithError(xerrors.Errorf("flush pending remote candidate: %w", err))
318350
return
319351
}
320352
}
353+
c.opts.Logger.Debug(context.Background(), "flushed buffered remote candidates",
354+
slog.F("count", len(c.pendingRemoteCandidates)),
355+
)
321356
c.pendingCandidatesFlushed = true
322-
c.opts.Logger.Debug(context.Background(), "flushed remote candidates")
357+
c.pendingRemoteCandidates = make([]webrtc.ICECandidateInit, 0)
323358
}
324359

325360
// LocalCandidate returns a channel that emits when a local candidate
@@ -332,12 +367,16 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
332367
func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) error {
333368
c.pendingCandidatesMutex.Lock()
334369
defer c.pendingCandidatesMutex.Unlock()
370+
fields := []slog.Field{
371+
slog.F("hash", sha256.Sum224([]byte(i.Candidate))),
372+
slog.F("length", len(i.Candidate)),
373+
}
335374
if !c.pendingCandidatesFlushed {
336-
c.opts.Logger.Debug(context.Background(), "adding remote candidate to buffer")
375+
c.opts.Logger.Debug(context.Background(), "bufferring remote candidate", fields...)
337376
c.pendingRemoteCandidates = append(c.pendingRemoteCandidates, i)
338377
return nil
339378
}
340-
c.opts.Logger.Debug(context.Background(), "adding remote candidate")
379+
c.opts.Logger.Debug(context.Background(), "adding remote candidate", fields...)
341380
return c.rtc.AddICECandidate(i)
342381
}
343382

0 commit comments

Comments
 (0)