Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove task cancellation hacks #6866

Merged
merged 1 commit into from
Jan 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private async Task StartStreaming(UdpClient udpClient, HdHomerunManager hdHomeru
{
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);
openTaskCompletionSource.TrySetException(ex);
Expand All @@ -191,36 +191,24 @@ private async Task CopyTo(UdpClient udpClient, string file, TaskCompletionSource
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
using (var timeOutSource = new CancellationTokenSource())
using (var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
timeOutSource.Token))
var res = await udpClient.ReceiveAsync(cancellationToken)
.AsTask()
.WaitAsync(TimeSpan.FromMilliseconds(30000), CancellationToken.None)
.ConfigureAwait(false);
var buffer = res.Buffer;

var read = buffer.Length - RtpHeaderBytes;

if (read > 0)
{
await fileStream.WriteAsync(buffer.AsMemory(RtpHeaderBytes, read), cancellationToken).ConfigureAwait(false);
}

if (!resolved)
{
var resTask = udpClient.ReceiveAsync(linkedSource.Token).AsTask();
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
timeOutSource.Cancel();
var res = await resTask.ConfigureAwait(false);
var buffer = res.Buffer;

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);
}
resolved = true;
DateOpened = DateTime.UtcNow;
openTaskCompletionSource.TrySetResult(true);
}
}
}
Expand Down
12 changes: 1 addition & 11 deletions Emby.Server.Implementations/Udp/UdpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,11 @@ public void Start(CancellationToken cancellationToken)

private async Task BeginReceiveAsync(CancellationToken cancellationToken)
{
var infiniteTask = Task.Delay(-1, cancellationToken);
while (!cancellationToken.IsCancellationRequested)
{
try
{
var task = _udpSocket.ReceiveFromAsync(_receiveBuffer, SocketFlags.None, _endpoint);
await Task.WhenAny(task, infiniteTask).ConfigureAwait(false);

if (!task.IsCompleted)
{
return;
}

var result = task.Result;

var result = await _udpSocket.ReceiveFromAsync(_receiveBuffer, SocketFlags.None, _endpoint, cancellationToken).ConfigureAwait(false);
var text = Encoding.UTF8.GetString(_receiveBuffer, 0, result.ReceivedBytes);
if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase))
{
Expand Down