Context
Aspire integration tests built with DistributedApplicationTestingBuilder fail at random with OperationCanceledException (“Resource ‘x’ failed to become healthy before the operation was cancelled”). They fail within seconds, long before any test timeout, and pass when run alone. It happens when someone runs aspire stop or aspire start while the tests are running.
Insight
- Aspire (13.x up to
main) always registersAuxiliaryBackchannelServiceas a hosted service. Every AppHost therefore opens a socket under~/.aspire/cli/backchannels/auxi.sock.*, and no setting turns it off. - This includes the AppHost that
DistributedApplicationTestingBuilderruns inside the test process. That AppHost reports the real AppHost project path. - What the CLI reaches through that socket:
aspire stopstops the AppHost whose path lies under the current directory, when it is the only one there.aspire stop --allstops every AppHost.aspire startcan stop a previous instance of the same AppHost.
- When the test AppHost stops, its resource notification stream closes, and
WaitForResourceHealthyAsyncthrows. - Changing
AppHost:Pathdoesn’t help: command-line args can’t override it, and--allignores scope anyway.
Fix
Remove the hosted service after CreateAsync. Throw if nothing matched, so an Aspire upgrade fails loudly instead of bringing the flake back.
var builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.MyAppHost>(args, ct);
var backchannels = builder.Services .Where(d => d.ServiceType == typeof(IHostedService) && d.ImplementationFactory?.Method.ReturnType.Name == "AuxiliaryBackchannelService") .ToArray();
if (backchannels.Length == 0) throw new InvalidOperationException("Aspire changed the AuxiliaryBackchannelService registration.");
foreach (var d in backchannels) builder.Services.Remove(d);This works because the factory lambda keeps its concrete return type, even after Aspire passes it through as an IHostedService factory.
To verify: while a test runs, ls ~/.aspire/cli/backchannels/ stays empty, and aspire stop --all reports No running apphost found.