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
17 changes: 5 additions & 12 deletions src/Aspire.Hosting.RemoteHost/Ats/CapabilityDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -617,26 +617,19 @@ public void Register(
return InvokeAsync(capabilityId, args).GetAwaiter().GetResult();
}

private static async Task<object?> InvokeMethodAsync(MethodInfo method, object? target, object?[] methodArgs, bool runSyncOnBackgroundThread)
private static async Task<object?> InvokeMethodAsync(MethodInfo method, object? target, object?[] methodArgs, bool runInvocationOnBackgroundThread)
{
if (runSyncOnBackgroundThread && !IsAsyncReturnType(method.ReturnType))
if (runInvocationOnBackgroundThread)
{
// Async-returning exports can execute substantial synchronous setup before returning
// their Task or ValueTask. Run that invocation path off the JSON-RPC synchronization context so
// sync-over-async callback proxies can still receive nested RPC responses.
Comment thread
sebastienros marked this conversation as resolved.
Resolved
return await Task.Run(() => InvokeMethodCore(method, target, methodArgs)).ConfigureAwait(false);
}

return InvokeMethodCore(method, target, methodArgs);
}

private static bool IsAsyncReturnType(Type returnType)
{
if (typeof(Task).IsAssignableFrom(returnType) || returnType == typeof(ValueTask))
{
return true;
}

return returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(ValueTask<>);
}

private static async Task<object?> UnwrapAsyncResultAsync(object? result, Type returnType)
{
try
Expand Down
11 changes: 6 additions & 5 deletions src/Aspire.Hosting/Ats/AspireExportAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,19 @@ public AspireExportAttribute(Type type)
public bool ExposeMethods { get; set; }

/// <summary>
/// Gets or sets whether synchronous exported methods should be invoked on a background thread by the ATS dispatcher.
/// Gets or sets whether the exported method invocation should run on a background thread by the ATS dispatcher.
/// </summary>
/// <remarks>
/// <para>
/// Set this to <see langword="true"/> for synchronous exports that may invoke synchronous callback delegates which in turn
/// Set this to <see langword="true"/> for exports that may invoke synchronous callback delegates which in turn
/// re-enter the remote host through ATS. Running the export on a background thread allows the JSON-RPC request loop to
/// continue processing nested callback and capability invocations while the synchronous method waits for the callback to
/// continue processing nested callback and capability invocations while the method waits for the callback to
/// complete.
/// </para>
/// <para>
/// This setting only affects synchronous exported methods. Async exports that already return <see cref="Task"/> or
/// <see cref="Task{TResult}"/> are awaited normally and do not use this option.
/// For async exports, this applies to the synchronous invocation path that returns the <see cref="Task"/>,
/// <see cref="Task{TResult}"/>, <see cref="ValueTask"/>, or <see cref="ValueTask{TResult}"/>. This is important
/// when the async method performs startup work or invokes callbacks before its first asynchronous yield.
/// </para>
/// <para>
/// When applied to a type, this setting also applies to exported members discovered from that type unless an individual
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.TypeSystem/AtsCapabilityInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public sealed class AtsCapabilityInfo
public string? SourceLocation { get; init; }

/// <summary>
/// Gets or sets whether synchronous invocations of this capability should run on a background thread.
/// Gets or sets whether this capability's invocation should run on a background thread.
/// </summary>
public bool RunSyncOnBackgroundThread { get; init; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Aspire.TypeSystem/AttributeDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public sealed class AspireExportData
public bool ExposeMethods { get; init; }

/// <summary>
/// Gets whether synchronous exported methods should be invoked on a background thread by the ATS dispatcher.
/// Gets whether exported method invocations should run on a background thread by the ATS dispatcher.
/// </summary>
public bool RunSyncOnBackgroundThread { get; init; }
}
Expand Down
133 changes: 129 additions & 4 deletions tests/Aspire.Hosting.RemoteHost.Tests/CapabilityDispatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -734,19 +734,44 @@ public void Invoke_SyncCapabilityWithBackgroundThreadOptIn_RunsOnBackgroundThrea
}

[Fact]
public void Invoke_ValueTaskCapabilityWithBackgroundThreadOptIn_RunsInline()
public void Invoke_TaskCapabilityWithBackgroundThreadOptIn_RunsOnBackgroundThread()
{
var dispatcher = CreateDispatcher(typeof(TestCapabilitiesWithBackgroundThreadDispatch).Assembly);
var callerThreadId = Environment.CurrentManagedThreadId;

var result = dispatcher.Invoke("Aspire.Hosting.RemoteHost.Tests/taskBackgroundThreadProbe", null);

Assert.NotNull(result);
Assert.NotEqual(callerThreadId, result.GetValue<int>());
}

[Fact]
public void Invoke_NonGenericTaskCapabilityWithBackgroundThreadOptIn_RunsOnBackgroundThread()
{
var dispatcher = CreateDispatcher(typeof(TestCapabilitiesWithBackgroundThreadDispatch).Assembly);
var callerThreadId = Environment.CurrentManagedThreadId;

TestCapabilitiesWithBackgroundThreadDispatch.ResetLastObservedThreadId();

dispatcher.Invoke("Aspire.Hosting.RemoteHost.Tests/nonGenericTaskBackgroundThreadProbe", null);

Assert.NotEqual(callerThreadId, TestCapabilitiesWithBackgroundThreadDispatch.LastObservedThreadId);
}

[Fact]
public void Invoke_ValueTaskCapabilityWithBackgroundThreadOptIn_RunsOnBackgroundThread()
{
var dispatcher = CreateDispatcher(typeof(TestCapabilitiesWithBackgroundThreadDispatch).Assembly);
var callerThreadId = Environment.CurrentManagedThreadId;

var result = dispatcher.Invoke("Aspire.Hosting.RemoteHost.Tests/valueTaskBackgroundThreadProbe", null);

Assert.NotNull(result);
Assert.Equal(callerThreadId, result.GetValue<int>());
Assert.NotEqual(callerThreadId, result.GetValue<int>());
}

[Fact]
public void Invoke_NonGenericValueTaskCapabilityWithBackgroundThreadOptIn_RunsInline()
public void Invoke_NonGenericValueTaskCapabilityWithBackgroundThreadOptIn_RunsOnBackgroundThread()
{
var dispatcher = CreateDispatcher(typeof(TestCapabilitiesWithBackgroundThreadDispatch).Assembly);
var callerThreadId = Environment.CurrentManagedThreadId;
Expand All @@ -755,7 +780,56 @@ public void Invoke_NonGenericValueTaskCapabilityWithBackgroundThreadOptIn_RunsIn

dispatcher.Invoke("Aspire.Hosting.RemoteHost.Tests/nonGenericValueTaskBackgroundThreadProbe", null);

Assert.Equal(callerThreadId, TestCapabilitiesWithBackgroundThreadDispatch.LastObservedThreadId);
Assert.NotEqual(callerThreadId, TestCapabilitiesWithBackgroundThreadDispatch.LastObservedThreadId);
}

[Fact]
public void Invoke_AsyncReturningCapabilityWithBackgroundThreadOptIn_InvokesSyncCallbackOnBackgroundThread()
{
var handles = new HandleRegistry();
var invoker = new TestCallbackInvoker();
var (marshaller, callbackFactory) = CreateTestMarshallerWithCallbacks(handles, invoker);
using var _ = callbackFactory;
var dispatcher = new CapabilityDispatcher(handles, marshaller, [typeof(TestCapabilitiesWithBackgroundThreadDispatch).Assembly]);
var callerThreadId = Environment.CurrentManagedThreadId;

TestCapabilitiesWithBackgroundThreadDispatch.ResetLastObservedThreadId();

dispatcher.Invoke(
"Aspire.Hosting.RemoteHost.Tests/invokeSyncCallbackFromAsyncBackgroundThreadProbe",
new JsonObject { ["callback"] = "background-callback" });

Assert.NotEqual(callerThreadId, TestCapabilitiesWithBackgroundThreadDispatch.LastObservedThreadId);
Assert.Single(invoker.Invocations);
Assert.Equal("background-callback", invoker.Invocations[0].CallbackId);
}

[Fact]
public void Invoke_AsyncReturningCapabilityWithBackgroundThreadOptIn_DoesNotRunSyncCallbackOnCallerSynchronizationContext()
{
var handles = new HandleRegistry();
var invoker = new TestSynchronizationContextCallbackInvoker();
var (marshaller, callbackFactory) = CreateTestMarshallerWithCallbacks(handles, invoker);
using var _ = callbackFactory;
var dispatcher = new CapabilityDispatcher(handles, marshaller, [typeof(TestCapabilitiesWithBackgroundThreadDispatch).Assembly]);
var callerSynchronizationContext = new SynchronizationContext();
var originalSynchronizationContext = SynchronizationContext.Current;

try
{
SynchronizationContext.SetSynchronizationContext(callerSynchronizationContext);

dispatcher.Invoke(
"Aspire.Hosting.RemoteHost.Tests/invokeSyncCallbackFromAsyncBackgroundThreadProbe",
new JsonObject { ["callback"] = "background-callback" });
}
finally
{
SynchronizationContext.SetSynchronizationContext(originalSynchronizationContext);
}

Assert.Single(invoker.Invocations);
Assert.NotSame(callerSynchronizationContext, invoker.LastObservedSynchronizationContext);
}

[Fact]
Expand Down Expand Up @@ -1961,6 +2035,32 @@ private static (AtsMarshaller Marshaller, AtsCallbackProxyFactory CallbackFactor
}
}

/// <summary>
/// Captures the synchronization context observed by callback invocation.
/// </summary>
internal sealed class TestSynchronizationContextCallbackInvoker : ICallbackInvoker
{
public List<(string CallbackId, JsonNode? Args)> Invocations { get; } = [];
public SynchronizationContext? LastObservedSynchronizationContext { get; private set; }
public bool IsConnected => true;

public Task<TResult> InvokeAsync<TResult>(string callbackId, JsonNode? args, CancellationToken cancellationToken = default)
{
LastObservedSynchronizationContext = SynchronizationContext.Current;
Invocations.Add((callbackId, args));

return Task.FromResult(default(TResult)!);
}

public Task InvokeAsync(string callbackId, JsonNode? args, CancellationToken cancellationToken = default)
{
LastObservedSynchronizationContext = SynchronizationContext.Current;
Invocations.Add((callbackId, args));

return Task.CompletedTask;
}
}

/// <summary>
/// Test capabilities for scanning.
/// </summary>
Expand Down Expand Up @@ -2224,6 +2324,21 @@ public static int SyncBackgroundThreadProbe()
return Environment.CurrentManagedThreadId;
}

/// <ats-summary>Captures the current thread for Task&lt;T&gt; invocation</ats-summary>
[AspireExport(RunSyncOnBackgroundThread = true)]
public static Task<int> TaskBackgroundThreadProbe()
{
return Task.FromResult(Environment.CurrentManagedThreadId);
}

/// <ats-summary>Captures the current thread for Task invocation</ats-summary>
[AspireExport(RunSyncOnBackgroundThread = true)]
public static Task NonGenericTaskBackgroundThreadProbe()
{
LastObservedThreadId = Environment.CurrentManagedThreadId;
return Task.CompletedTask;
}

/// <ats-summary>Captures the current thread for ValueTask&lt;T&gt; invocation</ats-summary>
[AspireExport(RunSyncOnBackgroundThread = true)]
public static ValueTask<int> ValueTaskBackgroundThreadProbe()
Expand All @@ -2238,6 +2353,16 @@ public static ValueTask NonGenericValueTaskBackgroundThreadProbe()
LastObservedThreadId = Environment.CurrentManagedThreadId;
return ValueTask.CompletedTask;
}

/// <ats-summary>Synchronously invokes a callback from a Task-returning capability</ats-summary>
[AspireExport(RunSyncOnBackgroundThread = true)]
public static Task InvokeSyncCallbackFromAsyncBackgroundThreadProbe(Func<Task> callback)
{
LastObservedThreadId = Environment.CurrentManagedThreadId;
callback().GetAwaiter().GetResult();

return Task.CompletedTask;
}
}

/// <summary>
Expand Down
Loading