diff --git a/peer/conn.go b/peer/conn.go index 662a00f93a706..e9126443b8eca 100644 --- a/peer/conn.go +++ b/peer/conn.go @@ -147,6 +147,7 @@ func (c *Conn) logger() slog.Logger { if !valid { return slog.Logger{} } + return log } @@ -566,11 +567,14 @@ func (c *Conn) isClosed() bool { func (c *Conn) CloseWithError(err error) error { c.closeMutex.Lock() defer c.closeMutex.Unlock() + if c.isClosed() { return c.closeError } - c.logger().Debug(context.Background(), "closing conn with error", slog.Error(err)) + logger := c.logger() + + logger.Debug(context.Background(), "closing conn with error", slog.Error(err)) if err == nil { c.closeError = ErrClosed } else { @@ -588,11 +592,11 @@ func (c *Conn) CloseWithError(err error) error { // Waiting for pion/webrtc to report closed state on both of these // ensures no goroutine leaks. if c.rtc.ConnectionState() != webrtc.PeerConnectionStateNew { - c.logger().Debug(context.Background(), "waiting for rtc connection close...") + logger.Debug(context.Background(), "waiting for rtc connection close...") <-c.closedRTC } if c.rtc.ICEConnectionState() != webrtc.ICEConnectionStateNew { - c.logger().Debug(context.Background(), "waiting for ice connection close...") + logger.Debug(context.Background(), "waiting for ice connection close...") <-c.closedICE } @@ -600,9 +604,11 @@ func (c *Conn) CloseWithError(err error) error { // All logging, goroutines, and async functionality is cleaned up after this. c.dcClosedWaitGroup.Wait() - c.logger().Debug(context.Background(), "closed") // Disable logging! c.loggerValue.Store(slog.Logger{}) + logger.Sync() + + logger.Debug(context.Background(), "closed") close(c.closed) return err }