fix: force logs to flush on close in peer.(*Conn) (#1268)

This commit is contained in:
Colin Adler
2022-05-03 08:36:48 -05:00
committed by GitHub
parent 9319c39257
commit eda85a0141

View File

@ -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
}