Skip to content

Socket transport: only 127.0.0.1, never ::1 #1951

Closed

Description

@muescha

A follow-up of #1870, collected in #1943.

Update: #1966 (in 0.30.0) did fixes 4 and 5: the user guide now says to give the language server the loopback address of the JVM rather than localhost, and to set the host to ::1 for an attach; and when LSP4IJ uses 127.0.0.1, the errors of an attach and of both start modes name the address and port and mention ::1. Fixes 1 to 3 (trying ::1 too, listening on both, a ${host} macro) are still open.

Summary

For a socket, LSP4IJ uses the IPv4 loopback address 127.0.0.1 (InetAddress.getLoopbackAddress()) only, both to connect to a language server and to listen for one. A language server that listens on, or connects to, localhost can end up on the IPv6 loopback ::1 instead, since localhost resolves to both. LSP4IJ and the language server then miss each other, and the start fails only after the 60 s start timeout.

Where

Socket transport, from #1870:

The DAP support makes the same assumption, so this isn't new in #1870, but a general topic:

  • DAPDebugProcess and DAPServerReadyTracker fall back to InetAddress.getLoopbackAddress() when no address is known.
  • Its templates avoid the problem by passing 127.0.0.1 to the debug adapter, as vscode-js-debug does with dapDebugServer.js ${port} 127.0.0.1, and the address can be read from the adapter's output (listening at: 127.0.0.1:…).

Which transports are affected

Transport Who listens Affected when
Attach to a running language server language server the server listens on ::1 only and the host is empty. Workaround: set the host to ::1.
Start language server, LSP4IJ connects to it language server the server listens on localhost and binds to ::1 only. No workaround, since these modes have no host field.
Start language server, it connects to LSP4IJ LSP4IJ, on 127.0.0.1 the server connects to localhost, tries ::1 only, and doesn't fall back to IPv4.

Servers built on vscode-languageserver aren't affected: with --socket, vscode-jsonrpc connects to 127.0.0.1 explicitly (createConnection(port, '127.0.0.1')), and listens on it in the other direction.

Reproduction

macOS, /etc/hosts has both 127.0.0.1 localhost and ::1 localhost, Node v26.5.1, Java 18.

A server that listens on localhost:

// server.js
const s = require('net').createServer(c => c.end());
s.listen(0, 'localhost', () => console.log(s.address()));
// LSP4IJ's start-and-connect, reduced
new Socket().connect(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), port), 1000);
Server listens on Bound to Java to 127.0.0.1 Java to ::1
'localhost' ::1 refused connected
'127.0.0.1' 127.0.0.1 connected refused
no host :: (both) connected connected

listen(0, 'localhost') bound to ::1 in 12 of 12 runs, and to 127.0.0.1 in 6 of 6 runs with node --dns-result-order=ipv4first.

A server that connects to localhost, with Java listening on 127.0.0.1 as LSP4IJ's start-and-accept does:

Node client Result
createConnection({port, host: 'localhost'}), default autoSelectFamily: true connected to 127.0.0.1
createConnection({port, host: 'localhost', autoSelectFamily: false}) ECONNREFUSED ::1
createConnection({port, host: '127.0.0.1'}) connected

So current Node falls back to IPv4 when it connects, but not when it listens.

Why

  • localhost resolves to both 127.0.0.1 and ::1, and the order depends on the OS resolver.
  • Since Node.js 17.0.0 (nodejs/node#39987), dns.lookup keeps that order (verbatim) instead of putting IPv4 first, see the dns docs. A server that listens on a host name binds to the first address only.
  • Java's InetAddress.getLoopbackAddress() is 127.0.0.1 unless java.net.preferIPv6Addresses is set, and a plain Socket.connect tries only the address it's given.

What happens in LSP4IJ

  • Start and connect: every attempt is refused, LSP4IJ retries until the start timeout, then fails with "The language server didn't accept a connection on port … within 60 s". Nothing in that message points at IPv4 versus IPv6. Since fix: socket transport errors and checks for 0.30.0 #1966 the message names the address and mentions ::1.
  • Start and accept: LSP4IJ waits until the start timeout with "The language server didn't connect to port … within 60 s". Since fix: socket transport errors and checks for 0.30.0 #1966 the message names the address and mentions ::1 too.

Possible fixes

  1. Connect to every loopback address: when the host is the default, try 127.0.0.1 and then ::1 (or each address of InetAddress.getAllByName("localhost")) in each attempt. This is small and covers attach and start-and-connect.
  2. Listen on both loopback addresses: for start-and-accept, bind a second ServerSocket on ::1 with the same port, if IPv6 is available, and take the first connection. Binding to :: instead would also accept connections from the network, so that's not an option.
  3. Give the host to the command: a ${host} macro next to ${port}, so a template can pass 127.0.0.1 to the server, as the DAP templates do with a literal 127.0.0.1 today.
  4. Docs: in the user-defined language server guide, tell users to make the server listen on, or connect to, 127.0.0.1 rather than localhost, and mention ::1 as the host for attach. Done in fix: socket transport errors and checks for 0.30.0 #1966.
  5. Error message: when a start with the default host times out, mention that a server on localhost may use ::1. Done in fix: socket transport errors and checks for 0.30.0 #1966, for an attach too.

1 and 4 seem the least effort for most of the benefit (4 is done); 2 and 3 are worth it only if real servers need them. The same change could then apply to the DAP connection.

Open questions

  • Is there a known language server that listens on, or connects to, localhost and is hit by this? So far it's only reproduced with a minimal Node server.
  • Should the fix cover DAP too, in the same change or a separate one?

Sources

How this was checked

Only tested on macOS, with Node v26.5.1 and Java 18 (see the tables above). Not tested: Linux, Windows (where localhost resolves in another order without an entry in the hosts file), and a real language server listening on localhost; the tests used a minimal Node server and a Java client like LSP4IJ's. That Node turns autoSelectFamily on by default since v20 is from memory; it was only checked for v26. The DAP case was only seen in the code, not reproduced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions