Skip to content

Commit 8875b2f

Browse files
committed
fix: force logs to flush on close in peer.(*Conn)
1 parent 5dcaf94 commit 8875b2f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

peer/conn.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ type Conn struct {
110110
closedRTCMutex sync.Mutex
111111
closedICE chan struct{}
112112
closedICEMutex sync.Mutex
113-
closeMutex sync.Mutex
113+
closeMutex sync.RWMutex
114114
closeError error
115115

116116
dcOpenChannel chan *webrtc.DataChannel
@@ -143,10 +143,15 @@ type Conn struct {
143143
}
144144

145145
func (c *Conn) logger() slog.Logger {
146+
// The logger gets swapped
147+
c.closeMutex.RLock()
148+
defer c.closeMutex.RUnlock()
149+
146150
log, valid := c.loggerValue.Load().(slog.Logger)
147151
if !valid {
148152
return slog.Logger{}
149153
}
154+
150155
return log
151156
}
152157

@@ -566,11 +571,14 @@ func (c *Conn) isClosed() bool {
566571
func (c *Conn) CloseWithError(err error) error {
567572
c.closeMutex.Lock()
568573
defer c.closeMutex.Unlock()
574+
569575
if c.isClosed() {
570576
return c.closeError
571577
}
572578

573-
c.logger().Debug(context.Background(), "closing conn with error", slog.Error(err))
579+
logger := c.logger()
580+
581+
logger.Debug(context.Background(), "closing conn with error", slog.Error(err))
574582
if err == nil {
575583
c.closeError = ErrClosed
576584
} else {
@@ -588,21 +596,23 @@ func (c *Conn) CloseWithError(err error) error {
588596
// Waiting for pion/webrtc to report closed state on both of these
589597
// ensures no goroutine leaks.
590598
if c.rtc.ConnectionState() != webrtc.PeerConnectionStateNew {
591-
c.logger().Debug(context.Background(), "waiting for rtc connection close...")
599+
logger.Debug(context.Background(), "waiting for rtc connection close...")
592600
<-c.closedRTC
593601
}
594602
if c.rtc.ICEConnectionState() != webrtc.ICEConnectionStateNew {
595-
c.logger().Debug(context.Background(), "waiting for ice connection close...")
603+
logger.Debug(context.Background(), "waiting for ice connection close...")
596604
<-c.closedICE
597605
}
598606

599607
// Waits for all DataChannels to exit before officially labeling as closed.
600608
// All logging, goroutines, and async functionality is cleaned up after this.
601609
c.dcClosedWaitGroup.Wait()
602610

603-
c.logger().Debug(context.Background(), "closed")
604611
// Disable logging!
605612
c.loggerValue.Store(slog.Logger{})
613+
logger.Sync()
614+
615+
logger.Debug(context.Background(), "closed")
606616
close(c.closed)
607617
return err
608618
}

0 commit comments

Comments
 (0)