---
title: "Aspire CLI can stop in-process test AppHosts via the auxiliary backchannel"
url: "https://www.dariuszparys.com/notes/aspire-cli-stops-in-process-test-apphosts"
author: "Dariusz Parys"
published: 2026-11-09T00:00:00.000Z
description: "Aspire integration tests fail at random when someone runs aspire stop or aspire start, because the AppHost inside the test process opens the same CLI backchannel socket as a real one."
---

# Aspire CLI can stop in-process test AppHosts via the auxiliary backchannel

## 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 registers `AuxiliaryBackchannelService` as 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 `DistributedApplicationTestingBuilder` runs inside the test process. That AppHost reports the real AppHost project path.
- What the CLI reaches through that socket:
  - `aspire stop` stops the AppHost whose path lies under the current directory, when it is the only one there.
  - `aspire stop --all` stops every AppHost.
  - `aspire start` can stop a previous instance of the same AppHost.
- When the test AppHost stops, its resource notification stream closes, and `WaitForResourceHealthyAsync` throws.
- Changing `AppHost:Path` doesn't help: command-line args can't override it, and `--all` ignores 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.

```csharp
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`.
