Skip to content

Commit 16f915f

Browse files
coadlerkylecarbs
authored andcommitted
fix: force logs to flush on close in peer.(*Conn) (#1268)
1 parent fa3f633 commit 16f915f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

peer/conn.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (c *Conn) logger() slog.Logger {
147147
if !valid {
148148
return slog.Logger{}
149149
}
150+
150151
return log
151152
}
152153

@@ -566,11 +567,14 @@ func (c *Conn) isClosed() bool {
566567
func (c *Conn) CloseWithError(err error) error {
567568
c.closeMutex.Lock()
568569
defer c.closeMutex.Unlock()
570+
569571
if c.isClosed() {
570572
return c.closeError
571573
}
572574

573-
c.logger().Debug(context.Background(), "closing conn with error", slog.Error(err))
575+
logger := c.logger()
576+
577+
logger.Debug(context.Background(), "closing conn with error", slog.Error(err))
574578
if err == nil {
575579
c.closeError = ErrClosed
576580
} else {
@@ -588,21 +592,23 @@ func (c *Conn) CloseWithError(err error) error {
588592
// Waiting for pion/webrtc to report closed state on both of these
589593
// ensures no goroutine leaks.
590594
if c.rtc.ConnectionState() != webrtc.PeerConnectionStateNew {
591-
c.logger().Debug(context.Background(), "waiting for rtc connection close...")
595+
logger.Debug(context.Background(), "waiting for rtc connection close...")
592596
<-c.closedRTC
593597
}
594598
if c.rtc.ICEConnectionState() != webrtc.ICEConnectionStateNew {
595-
c.logger().Debug(context.Background(), "waiting for ice connection close...")
599+
logger.Debug(context.Background(), "waiting for ice connection close...")
596600
<-c.closedICE
597601
}
598602

599603
// Waits for all DataChannels to exit before officially labeling as closed.
600604
// All logging, goroutines, and async functionality is cleaned up after this.
601605
c.dcClosedWaitGroup.Wait()
602606

603-
c.logger().Debug(context.Background(), "closed")
604607
// Disable logging!
605608
c.loggerValue.Store(slog.Logger{})
609+
logger.Sync()
610+
611+
logger.Debug(context.Background(), "closed")
606612
close(c.closed)
607613
return err
608614
}

0 commit comments

Comments
 (0)