aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBond-009 <bond.009@outlook.com>2022-02-12 20:24:02 +0100
committerGitHub <noreply@github.com>2022-02-12 20:24:02 +0100
commit69e7deb07d35a8391defce3590d31d36cc367ce6 (patch)
tree4394844cab0b999b63f5f52b6d1e4d0a1e13a24a
parent8a2ebae74d719b43c1fcf91bd5422061e9cd69a7 (diff)
parent8ec71c094aff8149baaa384283005718bc495b81 (diff)
Merge pull request #7300 from cvium/fix_keepreading
-rw-r--r--Jellyfin.Api/Helpers/ProgressiveFileStream.cs16
1 files changed, 9 insertions, 7 deletions
diff --git a/Jellyfin.Api/Helpers/ProgressiveFileStream.cs b/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
index 3fa07720a..6f5b64ea8 100644
--- a/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
+++ b/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
@@ -83,10 +83,10 @@ namespace Jellyfin.Api.Helpers
int totalBytesRead = 0;
var stopwatch = Stopwatch.StartNew();
- while (KeepReading(stopwatch.ElapsedMilliseconds))
+ while (true)
{
totalBytesRead += _stream.Read(buffer);
- if (totalBytesRead > 0)
+ if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds))
{
break;
}
@@ -109,10 +109,10 @@ namespace Jellyfin.Api.Helpers
int totalBytesRead = 0;
var stopwatch = Stopwatch.StartNew();
- while (KeepReading(stopwatch.ElapsedMilliseconds))
+ while (true)
{
totalBytesRead += await _stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
- if (totalBytesRead > 0)
+ if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds))
{
break;
}
@@ -172,10 +172,12 @@ namespace Jellyfin.Api.Helpers
}
}
- private bool KeepReading(long elapsed)
+ private bool StopReading(int bytesRead, long elapsed)
{
- // If the job is null it's a live stream and will require user action to close, but don't keep it open indefinitely
- return !_job?.HasExited ?? elapsed < _timeoutMs;
+ // It should stop reading when anything has been successfully read or if the job has exited
+ // If the job is null, however, it's a live stream and will require user action to close,
+ // but don't keep it open indefinitely if it isn't reading anything
+ return bytesRead > 0 || (_job?.HasExited ?? elapsed >= _timeoutMs);
}
}
}