mediamtx/internal/protocols/httpp/handler_exit_on_panic.go
Alessandro Ros 7634370818
Some checks failed
code_lint / go (push) Has been cancelled
code_lint / go_mod (push) Has been cancelled
code_lint / docs (push) Has been cancelled
code_lint / api_docs (push) Has been cancelled
code_test / test_64 (push) Has been cancelled
code_test / test_32 (push) Has been cancelled
code_test / test_e2e (push) Has been cancelled
add read and write timeouts in HTTP servers (#5056)
this prevents zombie connections from piling up.
2025-10-04 10:01:21 +02:00

27 lines
516 B
Go

package httpp
import (
"fmt"
"net/http"
"os"
"runtime"
)
// exit when there's a panic inside the HTTP handler.
// https://github.com/golang/go/issues/16542
type handlerExitOnPanic struct {
h http.Handler
}
func (h *handlerExitOnPanic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
err := recover()
if err != nil {
buf := make([]byte, 1<<20)
n := runtime.Stack(buf, true)
fmt.Fprintf(os.Stderr, "panic: %v\n\n%s", err, buf[:n])
os.Exit(1)
}
}()
h.h.ServeHTTP(w, r)
}