Repository navigation
Description
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::1for an attach; and when LSP4IJ uses127.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::1too, 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:
SocketStreamConnectionProvider.startAndConnectandstartAndAcceptuseInetAddress.getLoopbackAddress().accept()listens onInetAddress.getLoopbackAddress().UserDefinedLanguageServerDefinitiondefaults an empty host to it as well.
The DAP support makes the same assumption, so this isn't new in #1870, but a general topic:
DAPDebugProcessandDAPServerReadyTrackerfall back toInetAddress.getLoopbackAddress()when no address is known.- Its templates avoid the problem by passing
127.0.0.1to the debug adapter, asvscode-js-debugdoes withdapDebugServer.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
localhostresolves to both127.0.0.1and::1, and the order depends on the OS resolver.- Since Node.js 17.0.0 (nodejs/node#39987),
dns.lookupkeeps that order (verbatim) instead of putting IPv4 first, see thednsdocs. A server that listens on a host name binds to the first address only. - Java's
InetAddress.getLoopbackAddress()is127.0.0.1unlessjava.net.preferIPv6Addressesis set, and a plainSocket.connecttries 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
::1too.
Possible fixes
- Connect to every loopback address: when the host is the default, try
127.0.0.1and then::1(or each address ofInetAddress.getAllByName("localhost")) in each attempt. This is small and covers attach and start-and-connect. - Listen on both loopback addresses: for start-and-accept, bind a second
ServerSocketon::1with 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. - Give the host to the command: a
${host}macro next to${port}, so a template can pass127.0.0.1to the server, as the DAP templates do with a literal127.0.0.1today. Docs: in the user-defined language server guide, tell users to make the server listen on, or connect to,Done in fix: socket transport errors and checks for 0.30.0 #1966.127.0.0.1rather thanlocalhost, and mention::1as the host for attach.Error message: when a start with the default host times out, mention that a server onDone in fix: socket transport errors and checks for 0.30.0 #1966, for an attach too.localhostmay use::1.
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,
localhostand 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
- Node.js
dnsdocs - nodejs/node commit 1b2749ecbe, 3c37ce5710, 4acca8ed84
- vscode-jsonrpc 8.2.1, lib/node/main.js
- vscode-languageserver 9.0.1, lib/node/main.js
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.
Activity
feat: the socket transport of a language server is no longer experime…
feat: the socket transport of a language server is no longer experime…
fix: name the host and port of a socket connection that fails, and hi…
fix: socket transport errors and checks for 0.30.0 (#1966)
fix: connect to ::1 too without a host, and say when failed starts di…
fix: pause between the restarts of a language server whose start fail…
fix: listen on both loopback addresses for a language server which co…