Skip to content

Commit

Permalink
[RSDK-9664] - Fix deadlock in hotSwappableMediaSource (viamrobotics#4804
Browse files Browse the repository at this point in the history
)
  • Loading branch information
seanavery authored Feb 24, 2025
1 parent f4a3b65 commit f60083f
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion gostream/swapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ func (swapper *hotSwappableMediaSource[T, U]) Stream(
errHandlers ...ErrorHandler,
) (MediaStream[T], error) {
swapper.mu.RLock()
defer swapper.mu.RUnlock()

if swapper.src == nil {
swapper.mu.RUnlock()
return nil, errSwapperClosed
}

Expand All @@ -72,6 +72,12 @@ func (swapper *hotSwappableMediaSource[T, U]) Stream(
errHandlers: errHandlers,
cancelCtx: swapper.cancelCtx,
}

// Release the read lock before calling init to avoid potential deadlocks.
// This ensures that the Swap method can acquire the write lock if needed
// while the initialization process is ongoing.
swapper.mu.RUnlock()

stream.mu.Lock()
defer stream.mu.Unlock()
if err := stream.init(ctx); err != nil {
Expand Down

0 comments on commit f60083f

Please sign in to comment.