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

Failed to bind to address already in use #8021

Open
1 task done
LearningProcesss opened this issue Mar 12, 2025 · 6 comments
Open
1 task done

Failed to bind to address already in use #8021

LearningProcesss opened this issue Mar 12, 2025 · 6 comments

Comments

@LearningProcesss
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

No matter what i do Aspire Dashboard always crash, hanging and crashing VsCode.
Tried to change the port bumping the number, killing previous process and rebuilding devcontainer.

Environment

  • Visual Studio Code
  • DevContainer
  • dotnet --version: 9.0.200
  • Aspire.Hosting.AppHost version: 9.1.0
  • dapr --version: CLI version: 1.15.0 Runtime version: 1.15.3

AppHost Program.cs

using System.Diagnostics;
using CommunityToolkit.Aspire.Hosting.Dapr;

IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.DistributedAspireDapr_Insurance_Core>(name: "core-api")
       .WithDaprSidecar(new DaprSidecarOptions
       {
           AppId = "core-api"
       });

builder.Build().Run();

AppHost launchSettings.json

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "profiles": {
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:17128;http://localhost:15026",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_ENVIRONMENT": "Development",
        "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21118",
        "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22047",
        "ASPNETCORE_URLS": "https://localhost:17128;http://localhost:15026",
      }
    },
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:15026",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "DOTNET_ENVIRONMENT": "Development",
        "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19137",
        "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20125",
        "ASPNETCORE_URLS": "http://localhost:15026"
      }
    }
  }
}

Logs

Using launch settings from /workspaces/learning-aspire-dapr/DistributedAspireDapr.Aspire.AppHost/Properties/launchSettings.json...
Building...
warn: Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer[9]
      The ASP.NET Core developer certificate is only trusted by some clients. For information about trusting the ASP.NET Core developer certificate, see https://aka.ms/aspnet/https-trust-dev-cert
info: Aspire.Hosting.DistributedApplication[0]
      Aspire version: 9.1.0+2a8f48ea5811f317a26405eb315aa315cc9e3cea
info: Aspire.Hosting.DistributedApplication[0]
      Distributed application starting.
info: Aspire.Hosting.DistributedApplication[0]
      Application host directory is: /workspaces/learning-aspire-dapr/DistributedAspireDapr.Aspire.AppHost
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/home/vscode/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed. For more information go to https://aka.ms/aspnet/dataprotectionwarning
info: Aspire.Hosting.DistributedApplication[0]
      Now listening on: https://localhost:17128
info: Aspire.Hosting.DistributedApplication[0]
      Login to the dashboard at https://localhost:17128/login?t=175ad69aac66af4cb63a8e973fb47a41
info: Aspire.Hosting.DistributedApplication[0]
      Distributed application started. Press Ctrl+C to shut down.
fail: Aspire.Hosting.Dashboard.Microsoft.Extensions.Hosting.Internal.Host[11]
      Hosting failed to start
      System.IO.IOException: Failed to bind to address https://127.0.0.1:17128: address already in use.
       ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
       ---> System.Net.Sockets.SocketException (98): Address already in use
         at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
         at System.Net.Sockets.Socket.Bind(EndPoint localEP)
         at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
         at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
         --- End of inner exception stack trace ---
         at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
         at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass28_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
         --- End of inner exception stack trace ---
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.EndpointsStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
         at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
         at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__14_1(IHostedService service, CancellationToken token)
         at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1170000Z Unhandled exception. System.IO.IOException: Failed to bind to address https://127.0.0.1:17128: address already in use.
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z  ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z  ---> System.Net.Sockets.SocketException (98): Address already in use
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at System.Net.Sockets.Socket.Bind(EndPoint localEP)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    --- End of inner exception stack trace ---
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass28_0`1.<<StartAsync>g__OnBind|0>d.MoveNext()
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z --- End of stack trace from previous location ---
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    --- End of inner exception stack trace ---
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.EndpointsStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__14_1(IHostedService service, CancellationToken token)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Aspire.Dashboard.DashboardWebApplication.Run() in /_/src/Aspire.Dashboard/DashboardWebApplication.cs:line 822
fail: Aspire.Hosting.Dashboard[0]
      2025-03-11T23:40:29.1210000Z    at Program.<Main>$(String[] args) in /_/src/Aspire.Dashboard/Program.cs:line 7
info: Aspire.Hosting.Devcontainers.DevcontainerSettingsWriter[0]
      Port forwarding (aspire-dashboard-https): https://localhost:17128/
info: Aspire.Hosting.Devcontainers.DevcontainerSettingsWriter[0]
      Port forwarding (core-api-https): https://localhost:7123
info: Aspire.Hosting.Devcontainers.DevcontainerSettingsWriter[0]
      Port forwarding (core-api-http): http://localhost:5199
info: Aspire.Hosting.Devcontainers.DevcontainerSettingsWriter[0]
      Port forwarding (core-api-dapr-cli-grpc): http://localhost:41051
info: Aspire.Hosting.Devcontainers.DevcontainerSettingsWriter[0]
      Port forwarding (core-api-dapr-cli-http): http://localhost:34549
info: Aspire.Hosting.Devcontainers.DevcontainerSettingsWriter[0]
      Port forwarding (core-api-dapr-cli-metrics): http://localhost:40185

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version info

.NET SDK:
Version: 9.0.200
Commit: 90e8b202f2
Workload version: 9.0.200-manifests.b4a8049f
MSBuild version: 17.13.8+cbc39bea8

Runtime Environment:
OS Name: debian
OS Version: 12
OS Platform: Linux
RID: linux-x64
Base Path: /usr/share/dotnet/sdk/9.0.200/

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.2
Architecture: x64
Commit: 80aa709f5d

.NET SDKs installed:
9.0.101 [/usr/share/dotnet/sdk]
9.0.200 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 9.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.2 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 9.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
DOTNET_ROOT [/usr/share/dotnet]

global.json file:
Not found

Anything else?

No response

@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Mar 12, 2025
@davidfowl davidfowl added area-orchestrator and removed area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication labels Mar 12, 2025
@davidfowl
Copy link
Member

Check for a process called dcp running on the machine

@LearningProcesss
Copy link
Author

Check for a process called dcp running on the machine

Hi,

are you referencing this workaroud issues/6704#issuecomment-2487583280 ?

If so,
first run from a newly started DevContainer:
ss tells me that DevContainer is just forwarding port 17128 but no process is using it, workoarund is not in place yet

  ss -lptn 'sport = :17128'
  State          Recv-Q         Send-Q           Local Address:Port         Peer Address:Port          Process
  LISTEN       0                   511                 127.0.0.1:17128              0.0.0.0:*

Image

Failed to bind to address https://127.0.0.1:17128

Second run, stop forwarding 17128, workoarund is not in place yet

Failed to bind to address https://127.0.0.1:17128

Third run, no forwarding port, no process is using 17128, workaround checks for any "dcpctrl" and "dcp" (the second one is redundant maybe)

AppHost Program.cs now looks like this

using System.Diagnostics;
using CommunityToolkit.Aspire.Hosting.Dapr;

EnsureDeveloperControlPaneIsNotRunning();

IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.DistributedAspireDapr_Insurance_Core>(name: "core-api")
       .WithDaprSidecar(new DaprSidecarOptions
       {
           AppId = "core-api"
       });

builder.Build().Run();

void EnsureDeveloperControlPaneIsNotRunning()
{
    // The Aspire Developer Control Pane process name
    string[] toBeKilled = ["dcpctrl", "dcp"];

    foreach (var processToBeKilled in toBeKilled)
    {
        Process? process = Process.GetProcesses()
                                   .SingleOrDefault(p => p.ProcessName.Contains(processToBeKilled, StringComparison.OrdinalIgnoreCase));

        if (process == null) return;

        Console.WriteLine($"Shutting down developer control pane from previous run. Process: {process.ProcessName} (ID: {process.Id})");

        Thread.Sleep(TimeSpan.FromSeconds(5)); // Allow Docker containers to shut down to avoid orphaned containers

        try
        {
            process.Kill();
            Console.WriteLine($"Process {process.Id} killed successfully.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Failed to kill process {process.Id}: {ex.Message}");
        }
    }
}

No process is found to be killed, Failed to bind to address https://127.0.0.1:17128

Is there anything else that i can do?

@danegsta
Copy link
Member

I've tried running a sample project with the new devcontainer support on a couple machines and haven't managed to reproduce the issue with a similarly configured App Host. Are you able to share your devcontainer.json so we can see if something in your setup may be causing an issue? Additionally, does the issue reproduce if you run outside of a devcontainer?

@LearningProcesss
Copy link
Author

I've tried running a sample project with the new devcontainer support on a couple machines and haven't managed to reproduce the issue with a similarly configured App Host. Are you able to share your devcontainer.json so we can see if something in your setup may be causing an issue? Additionally, does the issue reproduce if you run outside of a devcontainer?

Hi, thanks for your reply!

Sure, here my devcontainer definition

{
	"name": "C# (.NET)",
	"image": "mcr.microsoft.com/devcontainers/dotnet:1-9.0-bookworm",
	"features": {
		"ghcr.io/devcontainers/features/docker-outside-of-docker:1": {},
		"ghcr.io/devcontainers/features/python:1": {},
		"ghcr.io/devcontainers/features/rust:1": {},
		"ghcr.io/eitsupi/devcontainer-features/duckdb-cli:1": {},
		"ghcr.io/shyim/devcontainers-features/bun:0": {},
		"ghcr.io/jsburckhardt/devcontainer-features/uv:1": {},
		"ghcr.io/dapr/cli/dapr-cli:0": {},
		"ghcr.io/devcontainers-community/npm-features/typescript:1": {},
		"ghcr.io/nikiforovall/devcontainer-features/dotnet-csharpier:1": {},
		"ghcr.io/devcontainers-extra/features/poetry:2": {},
		"ghcr.io/dotnet/aspire-devcontainer-feature/dotnetaspire:1": {}
	},
	"onCreateCommand": "export PATH=$PATH:/home/vscode/.dapr/bin",
	"postStartCommand": "dotnet dev-certs https --trust",
	"runArgs": [
		"--network=host"
	]
}

What's the new devcontainer support? C# Dev Kit new version or pre-release?

@danegsta
Copy link
Member

What's the new devcontainer support? C# Dev Kit new version or pre-release?

Sorry, I just meant the Aspire devcontainer feature that was recently added (and you are using in your config). I’ll do some testing with host networking mode today to see if that could be causing the behavior you’re seeing.

@danegsta
Copy link
Member

I've done some additional testing with host networking and docker-outside-of-docker support (I'd originally tested with docker-in-docker) but still haven't reproduced any port binding issues (admittedly, I'm testing on Windows+WSL and MacOS as I don't currently have a Linux image running). Have you tried changing the port assigned to the Dashboard by updating the applicationUrl ports in your launch profile (you can remove the ASPNETCORE_URLS environment variables for the AppHost as they shouldn't be necessary)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants