From 43e448d3fe98025eb84daec0eb7ead3d62653d3a Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Sun, 10 Sep 2023 16:57:40 +0200 Subject: [PATCH] client: fix race condition that can lead to crash (#407) this happened when a interleaved frame was read during shutdown. --- client_reader.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/client_reader.go b/client_reader.go index ee1aec6f..1aa0bb33 100644 --- a/client_reader.go +++ b/client_reader.go @@ -1,7 +1,7 @@ package gortsplib import ( - "sync/atomic" + "sync" "github.com/bluenviron/gortsplib/v4/pkg/base" "github.com/bluenviron/gortsplib/v4/pkg/liberrors" @@ -9,7 +9,8 @@ import ( type clientReader struct { c *Client - allowInterleavedFrames atomic.Bool + mutex sync.Mutex + allowInterleavedFrames bool } func newClientReader(c *Client) *clientReader { @@ -23,7 +24,9 @@ func newClientReader(c *Client) *clientReader { } func (r *clientReader) setAllowInterleavedFrames(v bool) { - r.allowInterleavedFrames.Store(v) + r.mutex.Lock() + defer r.mutex.Unlock() + r.allowInterleavedFrames = v } func (r *clientReader) wait() { @@ -58,13 +61,17 @@ func (r *clientReader) runInner() error { r.c.readRequest(what) case *base.InterleavedFrame: - if !r.allowInterleavedFrames.Load() { + r.mutex.Lock() + + if !r.allowInterleavedFrames { + r.mutex.Unlock() return liberrors.ErrClientUnexpectedFrame{} } if cb, ok := r.c.tcpCallbackByChannel[what.Channel]; ok { cb(what.Payload) } + r.mutex.Unlock() } } }