From 603d082cc328508c4bc37b6dd608004f51797be5 Mon Sep 17 00:00:00 2001 From: Dan Gottlieb Date: Fri, 20 Dec 2024 13:45:24 -0500 Subject: [PATCH] RSDK-9602: Prevent lazyEncodedImage.Bounds from getting gostream into a panic loop. (#4650) --- gostream/stream.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/gostream/stream.go b/gostream/stream.go index a270f8b2011..85ffdad12c6 100644 --- a/gostream/stream.go +++ b/gostream/stream.go @@ -4,6 +4,7 @@ package gostream import ( "context" "errors" + "fmt" "image" "sync" "time" @@ -269,7 +270,31 @@ func (bs *basicStream) processInputFrames() { if frame, ok := framePair.Media.(*rimage.LazyEncodedImage); ok && frame.MIMEType() == utils2.MimeTypeH264 { encodedFrame = frame.RawData() // nothing to do; already encoded } else { - bounds := framePair.Media.Bounds() + var bounds image.Rectangle + var boundsError any + func() { + defer func() { + if paniced := recover(); paniced != nil { + boundsError = paniced + } + }() + + bounds = framePair.Media.Bounds() + }() + + if boundsError != nil { + bs.logger.Errorw("Getting frame bounds failed", "err", fmt.Sprintf("%s", boundsError)) + // Dan: It's unclear why we get this error. There's reason to believe this pops + // up when a camera is reconfigured/removed. In which case I'd expect the + // `basicStream` to soon be closed. Making this `initErr = true` assignment to + // exit the `processInputFrames` goroutine unnecessary. But I'm choosing to be + // conservative for the worst case. Where we may be in a permanent bad state and + // (until we understand the problem better) we would spew logs until the user + // stops the stream. + initErr = true + return + } + newDx, newDy := bounds.Dx(), bounds.Dy() if bs.videoEncoder == nil || dx != newDx || dy != newDy { dx, dy = newDx, newDy