From ffa012ab3cfc4c6120117ea420b11115fcd89e57 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Mon, 17 Jul 2023 00:33:34 +0200 Subject: [PATCH] make sure components are closed in a specific order (#2065) --- internal/core/core.go | 37 +++++++++++++-------------------- internal/core/hls_manager.go | 3 +-- internal/core/path_manager.go | 3 +-- internal/core/rtmp_server.go | 3 +-- internal/core/rtsp_server.go | 3 +-- internal/core/webrtc_manager.go | 3 +-- 6 files changed, 20 insertions(+), 32 deletions(-) diff --git a/internal/core/core.go b/internal/core/core.go index a794c53a..4fbd64f1 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -241,7 +241,6 @@ func (p *Core) createResources(initial bool) error { if p.pathManager == nil { p.pathManager = newPathManager( - p.ctx, p.conf.ExternalAuthenticationURL, p.conf.RTSPAddress, p.conf.AuthMethods, @@ -263,7 +262,6 @@ func (p *Core) createResources(initial bool) error { _, useUDP := p.conf.Protocols[conf.Protocol(gortsplib.TransportUDP)] _, useMulticast := p.conf.Protocols[conf.Protocol(gortsplib.TransportUDPMulticast)] p.rtspServer, err = newRTSPServer( - p.ctx, p.conf.RTSPAddress, p.conf.AuthMethods, p.conf.ReadTimeout, @@ -299,7 +297,6 @@ func (p *Core) createResources(initial bool) error { p.conf.Encryption == conf.EncryptionOptional) { if p.rtspsServer == nil { p.rtspsServer, err = newRTSPServer( - p.ctx, p.conf.RTSPSAddress, p.conf.AuthMethods, p.conf.ReadTimeout, @@ -335,7 +332,6 @@ func (p *Core) createResources(initial bool) error { p.conf.RTMPEncryption == conf.EncryptionOptional) { if p.rtmpServer == nil { p.rtmpServer, err = newRTMPServer( - p.ctx, p.conf.RTMPAddress, p.conf.ReadTimeout, p.conf.WriteTimeout, @@ -362,7 +358,6 @@ func (p *Core) createResources(initial bool) error { p.conf.RTMPEncryption == conf.EncryptionOptional) { if p.rtmpsServer == nil { p.rtmpsServer, err = newRTMPServer( - p.ctx, p.conf.RTMPSAddress, p.conf.ReadTimeout, p.conf.WriteTimeout, @@ -387,7 +382,6 @@ func (p *Core) createResources(initial bool) error { if !p.conf.HLSDisable { if p.hlsManager == nil { p.hlsManager, err = newHLSManager( - p.ctx, p.conf.HLSAddress, p.conf.HLSEncryption, p.conf.HLSServerKey, @@ -417,7 +411,6 @@ func (p *Core) createResources(initial bool) error { if !p.conf.WebRTCDisable { if p.webRTCManager == nil { p.webRTCManager, err = newWebRTCManager( - p.ctx, p.conf.WebRTCAddress, p.conf.WebRTCEncryption, p.conf.WebRTCServerKey, @@ -628,21 +621,6 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) { } } - if closeRTSPSServer && p.rtspsServer != nil { - p.rtspsServer.close() - p.rtspsServer = nil - } - - if closeRTSPServer && p.rtspServer != nil { - p.rtspServer.close() - p.rtspServer = nil - } - - if closePathManager && p.pathManager != nil { - p.pathManager.close() - p.pathManager = nil - } - if closeWebRTCManager && p.webRTCManager != nil { p.webRTCManager.close() p.webRTCManager = nil @@ -663,6 +641,21 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) { p.rtmpServer = nil } + if closeRTSPSServer && p.rtspsServer != nil { + p.rtspsServer.close() + p.rtspsServer = nil + } + + if closeRTSPServer && p.rtspServer != nil { + p.rtspServer.close() + p.rtspServer = nil + } + + if closePathManager && p.pathManager != nil { + p.pathManager.close() + p.pathManager = nil + } + if closePPROF && p.pprof != nil { p.pprof.close() p.pprof = nil diff --git a/internal/core/hls_manager.go b/internal/core/hls_manager.go index adafbbc6..e844b973 100644 --- a/internal/core/hls_manager.go +++ b/internal/core/hls_manager.go @@ -63,7 +63,6 @@ type hlsManager struct { } func newHLSManager( - parentCtx context.Context, address string, encryption bool, serverKey string, @@ -84,7 +83,7 @@ func newHLSManager( metrics *metrics, parent hlsManagerParent, ) (*hlsManager, error) { - ctx, ctxCancel := context.WithCancel(parentCtx) + ctx, ctxCancel := context.WithCancel(context.Background()) m := &hlsManager{ externalAuthenticationURL: externalAuthenticationURL, diff --git a/internal/core/path_manager.go b/internal/core/path_manager.go index a7b27f08..ebd33465 100644 --- a/internal/core/path_manager.go +++ b/internal/core/path_manager.go @@ -74,7 +74,6 @@ type pathManager struct { } func newPathManager( - parentCtx context.Context, externalAuthenticationURL string, rtspAddress string, authMethods conf.AuthMethods, @@ -87,7 +86,7 @@ func newPathManager( metrics *metrics, parent pathManagerParent, ) *pathManager { - ctx, ctxCancel := context.WithCancel(parentCtx) + ctx, ctxCancel := context.WithCancel(context.Background()) pm := &pathManager{ externalAuthenticationURL: externalAuthenticationURL, diff --git a/internal/core/rtmp_server.go b/internal/core/rtmp_server.go index e0e2c338..15ba2dd3 100644 --- a/internal/core/rtmp_server.go +++ b/internal/core/rtmp_server.go @@ -74,7 +74,6 @@ type rtmpServer struct { } func newRTMPServer( - parentCtx context.Context, address string, readTimeout conf.StringDuration, writeTimeout conf.StringDuration, @@ -107,7 +106,7 @@ func newRTMPServer( return nil, err } - ctx, ctxCancel := context.WithCancel(parentCtx) + ctx, ctxCancel := context.WithCancel(context.Background()) s := &rtmpServer{ readTimeout: readTimeout, diff --git a/internal/core/rtsp_server.go b/internal/core/rtsp_server.go index cbd7ee0e..19eec72e 100644 --- a/internal/core/rtsp_server.go +++ b/internal/core/rtsp_server.go @@ -63,7 +63,6 @@ type rtspServer struct { } func newRTSPServer( - parentCtx context.Context, address string, authMethods []headers.AuthMethod, readTimeout conf.StringDuration, @@ -88,7 +87,7 @@ func newRTSPServer( pathManager *pathManager, parent rtspServerParent, ) (*rtspServer, error) { - ctx, ctxCancel := context.WithCancel(parentCtx) + ctx, ctxCancel := context.WithCancel(context.Background()) s := &rtspServer{ authMethods: authMethods, diff --git a/internal/core/webrtc_manager.go b/internal/core/webrtc_manager.go index bf699e7f..667dc3ba 100644 --- a/internal/core/webrtc_manager.go +++ b/internal/core/webrtc_manager.go @@ -150,7 +150,6 @@ type webRTCManager struct { } func newWebRTCManager( - parentCtx context.Context, address string, encryption bool, serverKey string, @@ -167,7 +166,7 @@ func newWebRTCManager( iceUDPMuxAddress string, iceTCPMuxAddress string, ) (*webRTCManager, error) { - ctx, ctxCancel := context.WithCancel(parentCtx) + ctx, ctxCancel := context.WithCancel(context.Background()) m := &webRTCManager{ allowOrigin: allowOrigin,