The Free Software Media System - Server Backend & API
fix: accessing Standard* of a Process requires manually disposing them afterwards (#10125)
Claus Vium956e3dab4341
diff --git a/Jellyfin.Api/Helpers/TranscodingJobHelper.cs b/Jellyfin.Api/Helpers/TranscodingJobHelper.csindex cee8e0f9be..73ebb396d0 100644--- a/Jellyfin.Api/Helpers/TranscodingJobHelper.cs+++ b/Jellyfin.Api/Helpers/TranscodingJobHelper.cs@@ -620,7 +620,7 @@ public class TranscodingJobHelper : IDisposablestate.TranscodingJob = transcodingJob;// Important - don't await the log task or we won't be able to kill FFmpeg when the user stops playback- _ = new JobLogger(_logger).StartStreamingLog(state, process.StandardError.BaseStream, logStream);+ _ = new JobLogger(_logger).StartStreamingLog(state, process.StandardError, logStream);// Wait for the file to exist before proceedingvar ffmpegTargetFile = state.WaitForPath ?? outputPath;diff --git a/MediaBrowser.Controller/MediaEncoding/JobLogger.cs b/MediaBrowser.Controller/MediaEncoding/JobLogger.csindex 3b34af4e96..3d288b9f86 100644--- a/MediaBrowser.Controller/MediaEncoding/JobLogger.cs+++ b/MediaBrowser.Controller/MediaEncoding/JobLogger.cs@@ -20,12 +20,12 @@ namespace MediaBrowser.Controller.MediaEncoding_logger = logger;}- public async Task StartStreamingLog(EncodingJobInfo state, Stream source, Stream target)+ public async Task StartStreamingLog(EncodingJobInfo state, StreamReader reader, Stream target){try{using (target)- using (var reader = new StreamReader(source))+ using (reader){while (!reader.EndOfStream && reader.BaseStream.CanRead){diff --git a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.csindex 38118ed0e1..db119ce5c5 100644--- a/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs+++ b/MediaBrowser.MediaEncoding/Encoder/EncoderValidator.cs@@ -553,7 +553,8 @@ namespace MediaBrowser.MediaEncoding.Encoderprivate string GetProcessOutput(string path, string arguments, bool readStdErr, string? testKey){- using (var process = new Process()+ var redirectStandardIn = !string.IsNullOrEmpty(testKey);+ using (var process = new Process{StartInfo = new ProcessStartInfo(path, arguments){@@ -561,7 +562,7 @@ namespace MediaBrowser.MediaEncoding.EncoderUseShellExecute = false,WindowStyle = ProcessWindowStyle.Hidden,ErrorDialog = false,- RedirectStandardInput = !string.IsNullOrEmpty(testKey),+ RedirectStandardInput = redirectStandardIn,RedirectStandardOutput = true,RedirectStandardError = true}@@ -571,12 +572,14 @@ namespace MediaBrowser.MediaEncoding.Encoderprocess.Start();- if (!string.IsNullOrEmpty(testKey))+ if (redirectStandardIn){- process.StandardInput.Write(testKey);+ using var writer = process.StandardInput;+ writer.Write(testKey);}- return readStdErr ? process.StandardError.ReadToEnd() : process.StandardOutput.ReadToEnd();+ using var reader = readStdErr ? process.StandardError : process.StandardOutput;+ return reader.ReadToEnd();}}}diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.csindex 9d6cdf7282..346e97ae12 100644--- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs+++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs@@ -511,7 +511,8 @@ namespace MediaBrowser.MediaEncoding.Encoderusing (var processWrapper = new ProcessWrapper(process, this)){StartProcess(processWrapper);- await process.StandardOutput.BaseStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);+ using var reader = process.StandardOutput;+ await reader.BaseStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);memoryStream.Seek(0, SeekOrigin.Begin);InternalMediaInfoResult result;trydiff --git a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs b/MediaBrowser.Providers/MediaInfo/AudioFileProber.csindex 9bcb1c39bd..44f998742f 100644--- a/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs+++ b/MediaBrowser.Providers/MediaInfo/AudioFileProber.cs@@ -130,7 +130,8 @@ namespace MediaBrowser.Providers.MediaInfothrow;}- output = await process.StandardError.ReadToEndAsync(cancellationToken).ConfigureAwait(false);+ using var reader = process.StandardError;+ output = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false);cancellationToken.ThrowIfCancellationRequested();MatchCollection split = LUFSRegex().Matches(output);diff --git a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.csindex febe9516af..479e6ffdc8 100644--- a/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs+++ b/src/Jellyfin.MediaEncoding.Keyframes/FfProbe/FfProbeKeyframeExtractor.cs@@ -68,51 +68,54 @@ public static class FfProbeKeyframeExtractordouble streamDuration = 0;double formatDuration = 0;- while (!reader.EndOfStream)+ using (reader){- var line = reader.ReadLine().AsSpan();- if (line.IsEmpty)+ while (!reader.EndOfStream){- continue;- }+ var line = reader.ReadLine().AsSpan();+ if (line.IsEmpty)+ {+ continue;+ }- var firstComma = line.IndexOf(',');- var lineType = line[..firstComma];- var rest = line[(firstComma + 1)..];- if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase))- {- // Split time and flags from the packet line. Example line: packet,7169.079000,K_- var secondComma = rest.IndexOf(',');- var ptsTime = rest[..secondComma];- var flags = rest[(secondComma + 1)..];- if (flags.StartsWith("K_"))+ var firstComma = line.IndexOf(',');+ var lineType = line[..firstComma];+ var rest = line[(firstComma + 1)..];+ if (lineType.Equals("packet", StringComparison.OrdinalIgnoreCase)){- if (double.TryParse(ptsTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var keyframe))+ // Split time and flags from the packet line. Example line: packet,7169.079000,K_+ var secondComma = rest.IndexOf(',');+ var ptsTime = rest[..secondComma];+ var flags = rest[(secondComma + 1)..];+ if (flags.StartsWith("K_")){- // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise down to 1 ms when converting double.- keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond));+ if (double.TryParse(ptsTime, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var keyframe))+ {+ // Have to manually convert to ticks to avoid rounding errors as TimeSpan is only precise down to 1 ms when converting double.+ keyframes.Add(Convert.ToInt64(keyframe * TimeSpan.TicksPerSecond));+ }}}- }- else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase))- {- if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult))+ else if (lineType.Equals("stream", StringComparison.OrdinalIgnoreCase)){- streamDuration = streamDurationResult;+ if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var streamDurationResult))+ {+ streamDuration = streamDurationResult;+ }}- }- else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase))- {- if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult))+ else if (lineType.Equals("format", StringComparison.OrdinalIgnoreCase)){- formatDuration = formatDurationResult;+ if (double.TryParse(rest, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var formatDurationResult))+ {+ formatDuration = formatDurationResult;+ }}}- }- // Prefer the stream duration as it should be more accurate- var duration = streamDuration > 0 ? streamDuration : formatDuration;+ // Prefer the stream duration as it should be more accurate+ var duration = streamDuration > 0 ? streamDuration : formatDuration;- return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes);+ return new KeyframeData(TimeSpan.FromSeconds(duration).Ticks, keyframes);+ }}}