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

csharpier server and ipv6 #1242

Closed
wuzzeb opened this issue Apr 26, 2024 · 2 comments · Fixed by #1244
Closed

csharpier server and ipv6 #1242

wuzzeb opened this issue Apr 26, 2024 · 2 comments · Fixed by #1244

Comments

@wuzzeb
Copy link

wuzzeb commented Apr 26, 2024

Environments

  • IDE Version: vscode 1.88.1
  • Extension Version: 1.7.1
  • CSharpier Version: 0.28.1
  • Operating System: linux

Log Output

["WARN" - 9:34:00 AM] Failed posting to the csharpier server. FetchError: request to http://localhost:49152/format failed, reason: connect ECONNREFUSED ::1:49152

Steps to reproduce

Configure your system so that localhost resolve to the IPv6 address ::1 instead of 127.0.0.1

The Problem

So looking at the code which starts the server, https://github.com/belav/csharpier/blob/main/Src/CSharpier.Cli/Server/ServerFormatter.cs#L23 it is passing the IPAddress.Loopback field to kestrel, and from the docs https://learn.microsoft.com/en-us/dotnet/api/system.net.ipaddress.loopback the IPAdress.Loopback is the IPv4 address 127.0.0.1

Therefore, csharpier only binds to the IPv4 address 127.0.0.1. I can confirm that using lsof to list open ports, csharpier is listening only on 127.0.0.1:49152 and not listening on IPv6 ::1

The problem is the vscode extension is attempting to connect to http://localhost:49152 which resolves to the IPv6 address ::1 and thus gets a connection refused.

Two possible fixes

When localhost is specified, Kestrel attempts to bind to both IPv4 and IPv6 loopback interfaces. If the requested port is in use by another service on either loopback interface, Kestrel fails to start. If either loopback interface is unavailable for any other reason (most commonly because IPv6 isn't supported), Kestrel logs a warning.

So instead of passing IPAddress.Loopback, just pass the string "localhost". It seems there is IPAddress.IPv6Loopback field but I think just using the string "localhost" and letting kestrel do it is probably better.

@belav
Copy link
Owner

belav commented Apr 26, 2024

I spent way too much time debating which option to go with, and decided to just go with updating the extensions because it would be easier to test. Probably could have had all 3 extensions updated in the time I debated it.

Thanks for all the details! The vscode fix is out nowish and VS + Rider will be along shortly.

belav added a commit that referenced this issue Apr 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…::1 extensions could not format (#1244)

* Use 127.0.0.1 to avoid issue when localhost resolves to ::1

references #1242

* Use 127.0.0.1 for VS extension

* Use 127.0.0.1 for rider extension
@wuzzeb
Copy link
Author

wuzzeb commented Apr 26, 2024

Thanks, 1.7.2 works

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