Merge pull request #6866 from Bond-009/timeout

This commit is contained in:
Cody Robibero 2022-01-08 10:53:58 -07:00 committed by GitHub
commit 54d24ddeaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 40 deletions

View File

@ -165,7 +165,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
{ {
await CopyTo(udpClient, TempFilePath, openTaskCompletionSource, cancellationToken).ConfigureAwait(false); await CopyTo(udpClient, TempFilePath, openTaskCompletionSource, cancellationToken).ConfigureAwait(false);
} }
catch (OperationCanceledException ex) catch (Exception ex) when (ex is OperationCanceledException || ex is TimeoutException)
{ {
Logger.LogInformation("HDHR UDP stream cancelled or timed out from {0}", remoteAddress); Logger.LogInformation("HDHR UDP stream cancelled or timed out from {0}", remoteAddress);
openTaskCompletionSource.TrySetException(ex); openTaskCompletionSource.TrySetException(ex);
@ -191,36 +191,24 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
while (true) while (true)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
using (var timeOutSource = new CancellationTokenSource()) var res = await udpClient.ReceiveAsync(cancellationToken)
using (var linkedSource = CancellationTokenSource.CreateLinkedTokenSource( .AsTask()
cancellationToken, .WaitAsync(TimeSpan.FromMilliseconds(30000), CancellationToken.None)
timeOutSource.Token)) .ConfigureAwait(false);
var buffer = res.Buffer;
var read = buffer.Length - RtpHeaderBytes;
if (read > 0)
{ {
var resTask = udpClient.ReceiveAsync(linkedSource.Token).AsTask(); await fileStream.WriteAsync(buffer.AsMemory(RtpHeaderBytes, read), cancellationToken).ConfigureAwait(false);
if (await Task.WhenAny(resTask, Task.Delay(30000, linkedSource.Token)).ConfigureAwait(false) != resTask) }
{
resTask.Dispose();
break;
}
// We don't want all these delay tasks to keep running if (!resolved)
timeOutSource.Cancel(); {
var res = await resTask.ConfigureAwait(false); resolved = true;
var buffer = res.Buffer; DateOpened = DateTime.UtcNow;
openTaskCompletionSource.TrySetResult(true);
var read = buffer.Length - RtpHeaderBytes;
if (read > 0)
{
await fileStream.WriteAsync(buffer.AsMemory(RtpHeaderBytes, read), linkedSource.Token).ConfigureAwait(false);
}
if (!resolved)
{
resolved = true;
DateOpened = DateTime.UtcNow;
openTaskCompletionSource.TrySetResult(true);
}
} }
} }
} }

View File

@ -97,21 +97,11 @@ namespace Emby.Server.Implementations.Udp
private async Task BeginReceiveAsync(CancellationToken cancellationToken) private async Task BeginReceiveAsync(CancellationToken cancellationToken)
{ {
var infiniteTask = Task.Delay(-1, cancellationToken);
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
try try
{ {
var task = _udpSocket.ReceiveFromAsync(_receiveBuffer, SocketFlags.None, _endpoint); var result = await _udpSocket.ReceiveFromAsync(_receiveBuffer, SocketFlags.None, _endpoint, cancellationToken).ConfigureAwait(false);
await Task.WhenAny(task, infiniteTask).ConfigureAwait(false);
if (!task.IsCompleted)
{
return;
}
var result = task.Result;
var text = Encoding.UTF8.GetString(_receiveBuffer, 0, result.ReceivedBytes); var text = Encoding.UTF8.GetString(_receiveBuffer, 0, result.ReceivedBytes);
if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase)) if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase))
{ {