@@ -918,6 +918,13 @@ func LogToStderr(stderr bool) {
918
918
// output writes the data to the log files and releases the buffer.
919
919
func (l * loggingT ) output (s severity , log * logr.Logger , buf * buffer , depth int , file string , line int , alsoToStderr bool ) {
920
920
l .mu .Lock ()
921
+ defer func () {
922
+ if err := recover (); err != nil {
923
+ l .mu .Unlock () // Make sure to do `Unlock` if panic occurs below (eg: #L938 Error)
924
+ os .Stderr .Write ([]byte ("panic observed in loggingT.output" )) // Make sure the message appears somewhere.
925
+ }
926
+ }()
927
+
921
928
if l .traceLocation .isSet () {
922
929
if l .traceLocation .match (file , line ) {
923
930
buf .Write (stacks (false ))
@@ -928,7 +935,7 @@ func (l *loggingT) output(s severity, log *logr.Logger, buf *buffer, depth int,
928
935
// TODO: set 'severity' and caller information as structured log info
929
936
// keysAndValues := []interface{}{"severity", severityName[s], "file", file, "line", line}
930
937
if s == errorLog {
931
- l .logr .WithCallDepth (depth + 3 ).Error (nil , string (data ))
938
+ l .logr .WithCallDepth (depth + 3 ).Error (nil , string (data )) // If panic here will be captured by defer
932
939
} else {
933
940
log .WithCallDepth (depth + 3 ).Info (string (data ))
934
941
}
@@ -945,6 +952,7 @@ func (l *loggingT) output(s severity, log *logr.Logger, buf *buffer, depth int,
945
952
if l .file [infoLog ] == nil {
946
953
if err := l .createFiles (infoLog ); err != nil {
947
954
os .Stderr .Write (data ) // Make sure the message appears somewhere.
955
+ l .mu .Unlock () // At this point `logExitFunc` is nil (checked in #L1068), so `Unlock` needed.
948
956
l .exit (err )
949
957
}
950
958
}
@@ -953,6 +961,7 @@ func (l *loggingT) output(s severity, log *logr.Logger, buf *buffer, depth int,
953
961
if l .file [s ] == nil {
954
962
if err := l .createFiles (s ); err != nil {
955
963
os .Stderr .Write (data ) // Make sure the message appears somewhere.
964
+ l .mu .Unlock () // At this point `logExitFunc` is nil (checked in #L1068), so `Unlock` needed.
956
965
l .exit (err )
957
966
}
958
967
}
0 commit comments