api, hls, metrics, pprof: always close HTTP listeners

due to a race condition, HTTP listeners could remain open even after
the closure of a component.
This commit is contained in:
aler9 2022-08-30 18:42:29 +02:00
parent 3790af946f
commit f25e14183d
4 changed files with 8 additions and 8 deletions

View File

@ -189,6 +189,7 @@ type api struct {
hlsServer apiHLSServer
parent apiParent
ln net.Listener
mutex sync.Mutex
s *http.Server
}
@ -218,6 +219,7 @@ func newAPI(
rtmpsServer: rtmpsServer,
hlsServer: hlsServer,
parent: parent,
ln: ln,
}
router := gin.New()
@ -268,6 +270,7 @@ func newAPI(
func (a *api) close() {
a.log(logger.Info, "listener is closing")
a.ln.Close() // in case Shutdown() is called before Serve()
a.s.Shutdown(context.Background())
}

View File

@ -248,6 +248,7 @@ outer:
s.ctxCancel()
s.ln.Close() // in case Shutdown() is called before Serve()
hs.Shutdown(context.Background())
s.pathManager.hlsServerSet(nil)

View File

@ -79,6 +79,7 @@ func newMetrics(
func (m *metrics) close() {
m.log(logger.Info, "listener is closing")
m.ln.Close() // in case Shutdown() is called before Serve()
m.server.Shutdown(context.Background())
}
@ -87,10 +88,7 @@ func (m *metrics) log(level logger.Level, format string, args ...interface{}) {
}
func (m *metrics) run() {
err := m.server.Serve(m.ln)
if err != http.ErrServerClosed {
panic(err)
}
m.server.Serve(m.ln)
}
func (m *metrics) onMetrics(ctx *gin.Context) {

View File

@ -49,6 +49,7 @@ func newPPROF(
func (pp *pprof) close() {
pp.log(logger.Info, "listener is closing")
pp.ln.Close() // in case Shutdown() is called before Serve()
pp.server.Shutdown(context.Background())
}
@ -57,8 +58,5 @@ func (pp *pprof) log(level logger.Level, format string, args ...interface{}) {
}
func (pp *pprof) run() {
err := pp.server.Serve(pp.ln)
if err != http.ErrServerClosed {
panic(err)
}
pp.server.Serve(pp.ln)
}