mirror of
https://github.com/minio/minio.git
synced 2025-04-11 23:12:12 -04:00
replace os.Exit with panic for logger.CriticalIf (#6065)
This commit prevents complete server failures caused by `logger.CriticalIf` calls. Instead of calling `os.Exit(1)` the function now executes a panic with a special value indicating that a critical error happend. At the top HTTP handler layer panics are recovered and if its a critical error the client gets an InternalServerError status code. Further this allows unit tests to cover critical-error code paths.
This commit is contained in:
parent
5fbdd70de9
commit
cd152f404a
@ -197,7 +197,7 @@ func StartGateway(ctx *cli.Context, gw Gateway) {
|
|||||||
getCert = globalTLSCerts.GetCertificate
|
getCert = globalTLSCerts.GetCertificate
|
||||||
}
|
}
|
||||||
|
|
||||||
globalHTTPServer = xhttp.NewServer([]string{gatewayAddr}, registerHandlers(router, globalHandlers...), getCert)
|
globalHTTPServer = xhttp.NewServer([]string{gatewayAddr}, criticalErrorHandler{registerHandlers(router, globalHandlers...)}, getCert)
|
||||||
globalHTTPServer.UpdateBytesReadFunc = globalConnStats.incInputBytes
|
globalHTTPServer.UpdateBytesReadFunc = globalConnStats.incInputBytes
|
||||||
globalHTTPServer.UpdateBytesWrittenFunc = globalConnStats.incOutputBytes
|
globalHTTPServer.UpdateBytesWrittenFunc = globalConnStats.incOutputBytes
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -738,3 +738,20 @@ func (s securityHeaderHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
|
|||||||
header.Set("Content-Security-Policy", "block-all-mixed-content") // prevent mixed (HTTP / HTTPS content)
|
header.Set("Content-Security-Policy", "block-all-mixed-content") // prevent mixed (HTTP / HTTPS content)
|
||||||
s.handler.ServeHTTP(w, r)
|
s.handler.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// criticalErrorHandler handles critical server failures caused by
|
||||||
|
// `panic(logger.ErrCritical)` as done by `logger.CriticalIf`.
|
||||||
|
//
|
||||||
|
// It should be always the first / highest HTTP handler.
|
||||||
|
type criticalErrorHandler struct{ handler http.Handler }
|
||||||
|
|
||||||
|
func (h criticalErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err == logger.ErrCritical { // handle
|
||||||
|
writeErrorResponse(w, ErrInternalError, r.URL)
|
||||||
|
} else if err != nil {
|
||||||
|
panic(err) // forward other panic calls
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
h.handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
@ -362,13 +362,15 @@ func LogIf(ctx context.Context, err error) {
|
|||||||
fmt.Println(output)
|
fmt.Println(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CriticalIf :
|
// ErrCritical is the value panic'd whenever CriticalIf is called.
|
||||||
// Like LogIf with exit
|
var ErrCritical struct{}
|
||||||
// It'll be called for fatal error conditions during run-time
|
|
||||||
|
// CriticalIf logs the provided error on the console. It fails the
|
||||||
|
// current go-routine by causing a `panic(ErrCritical)`.
|
||||||
func CriticalIf(ctx context.Context, err error) {
|
func CriticalIf(ctx context.Context, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogIf(ctx, err)
|
LogIf(ctx, err)
|
||||||
os.Exit(1)
|
panic(ErrCritical)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,7 +288,7 @@ func serverMain(ctx *cli.Context) {
|
|||||||
getCert = globalTLSCerts.GetCertificate
|
getCert = globalTLSCerts.GetCertificate
|
||||||
}
|
}
|
||||||
|
|
||||||
globalHTTPServer = xhttp.NewServer([]string{globalMinioAddr}, handler, getCert)
|
globalHTTPServer = xhttp.NewServer([]string{globalMinioAddr}, criticalErrorHandler{handler}, getCert)
|
||||||
globalHTTPServer.UpdateBytesReadFunc = globalConnStats.incInputBytes
|
globalHTTPServer.UpdateBytesReadFunc = globalConnStats.incInputBytes
|
||||||
globalHTTPServer.UpdateBytesWrittenFunc = globalConnStats.incOutputBytes
|
globalHTTPServer.UpdateBytesWrittenFunc = globalConnStats.incOutputBytes
|
||||||
go func() {
|
go func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user