Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Common/src/Common/DynamicTypeAccess/TaskShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ internal sealed class TaskShim<TResult>(object instance)
{
public override Task Instance => (Task)base.Instance;

public TResult Result => InstanceAccessor.GetPropertyValue<TResult>("Result");

private static InstanceAccessor Wrap(object instance)
{
Type taskLikeType = instance.GetType();
var typeAccessor = new TypeAccessor(taskLikeType);
return new InstanceAccessor(typeAccessor, instance);
}

public TResult GetResult()
{
object awaiter = InstanceAccessor.InvokeMethod("GetAwaiter", true)!;
var awaiterAccessor = new InstanceAccessor(new TypeAccessor(awaiter.GetType()), awaiter);
object result = awaiterAccessor.InvokeMethod("GetResult", true)!;
return (TResult)result;
}

public void Dispose()
{
Instance.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public async Task<IDisposable> ListDatabaseNamesAsync(CancellationToken cancella
await task;

using var taskShim = new TaskShim<IDisposable>(task);
return taskShim.Result;
return taskShim.GetResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public async Task<ConnectionInterfaceShim> CreateConnectionAsync(CancellationTok
await task;

using var taskShim = new TaskShim<IDisposable>(task);
return new ConnectionInterfaceShim(_packageResolver, taskShim.Result);
IDisposable connection = taskShim.GetResult();
return new ConnectionInterfaceShim(_packageResolver, connection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static async Task<ConnectionMultiplexerInterfaceShim> ConnectAsync(StackE
await task;

using var taskShim = new TaskShim<IDisposable>(task);
return new ConnectionMultiplexerInterfaceShim(packageResolver, taskShim.Result);
IDisposable connectionMultiplexer = taskShim.GetResult();
return new ConnectionMultiplexerInterfaceShim(packageResolver, connectionMultiplexer);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public async Task<TimeSpan> PingAsync()
await task;

using var taskShim = new TaskShim<TimeSpan>(task);
return taskShim.Result;
return taskShim.GetResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,23 @@ private async Task<string> CreateTraceFileAsync(EventPipeSession session, Cancel
cancellationToken.ThrowIfCancellationRequested();

// check if rundown is taking more than 5 seconds and log
Task timeoutTask = Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
using var timeoutSource = new CancellationTokenSource();
Task timeoutTask = Task.Delay(TimeSpan.FromSeconds(5), timeoutSource.Token);

Task completedTask = await Task.WhenAny(copyTask, timeoutTask);

if (completedTask == timeoutTask && !cancellationToken.IsCancellationRequested)
if (completedTask == timeoutTask)
{
if (!cancellationToken.IsCancellationRequested)
{
_logger.LogInformation("Sufficiently large applications can cause this command to take non-trivial amounts of time.");
}
}
else
{
_logger.LogInformation("Sufficiently large applications can cause this command to take non-trivial amounts of time.");
// Cancel the internal timer allocated inside Task.Delay, so that it does not fire and its resources are released.
// See https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AsyncGuidance.md#using-a-timeout.
await timeoutSource.CancelAsync();
}

await copyTask;
Expand Down