From 8ffb54603adf45975568702ca8a9d69bfe444d75 Mon Sep 17 00:00:00 2001 From: zachhide <66033020+zachhide@users.noreply.github.com> Date: Tue, 30 Jun 2026 01:13:51 -0400 Subject: Fix NullReferenceException in GetStreamingState for closed live streams When a client polls the HLS playlist (e.g. live.m3u8) after a live stream has been disposed because its consumer count dropped to zero, GetLiveStreamWithDirectStreamProvider returns a null MediaSource. The live branch of GetStreamingState then dereferenced it unconditionally, throwing a NullReferenceException and returning HTTP 500 for every poll until the client re-opens the stream. Guard against the null MediaSource and throw ResourceNotFoundException so the request returns 404 instead of crashing. Fixes #17009 Co-Authored-By: Claude Opus 4.8 (1M context) --- Jellyfin.Api/Helpers/StreamingHelpers.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Jellyfin.Api/Helpers/StreamingHelpers.cs b/Jellyfin.Api/Helpers/StreamingHelpers.cs index bae2756303..6a6aac1327 100644 --- a/Jellyfin.Api/Helpers/StreamingHelpers.cs +++ b/Jellyfin.Api/Helpers/StreamingHelpers.cs @@ -144,6 +144,15 @@ public static class StreamingHelpers mediaSource = liveStreamInfo.Item1; state.DirectStreamProvider = liveStreamInfo.Item2; + // The requested live stream is no longer open. This commonly happens when a client keeps + // polling the HLS playlist (e.g. live.m3u8) after the stream was disposed because its + // consumer count dropped to zero. GetLiveStreamWithDirectStreamProvider returns a null + // MediaSource in that case, so return 404 instead of dereferencing it below. + if (mediaSource is null) + { + throw new ResourceNotFoundException($"The live stream with id {streamingRequest.LiveStreamId} could not be found or is no longer available."); + } + // Cap the max bitrate when it is too high. This is usually due to ffmpeg is unable to probe the source liveTV streams' bitrate. if (mediaSource.FallbackMaxStreamingBitrate is not null && streamingRequest.VideoBitRate is not null) { -- cgit v1.2.3