Cannot respond to aws tcp healthcheck with js-libp2p

Problem

Opening a plain tcp connection to a js-libp2p port fails, which breaks AWS’ load balancer health checks and prevents usage of AWS ECS.

This seem to happen because libp2p tries to immediately read the stream to negotiate encryption, which always fails in such cases. This happens when using any of the available encryption options (Noise, Plaintext).

Ask

Is there any way to get the libp2p implementation to “succeed” on certain tcp requests to the port? AWS ECS requires the tcp healthcheck to pass otherwise it won’t allow traffic through to the instance since it assume the port is non functional.

Repro

The repository is public and you can see the libp2p configuration here: hub/node.ts at ef7c9c08c15c7953699e5f052ac076702a61614b · farcasterxyz/hub · GitHub

You can approximate this behavior locally by just opening a tcp connection using nc which shows that the first connection attempt is refused over tcp.

➜   nc localhost 8080 -vz
nc: connectx to localhost port 8080 (tcp) failed: Connection refused
Connection to localhost port 8080 [tcp/http-alt] succeeded!

the libp2p application thows the following exception:

  libp2p:tcp:listener new inbound connection /ip4/127.0.0.1/tcp/52626 +2s
  libp2p:upgrader starting the inbound connection upgrade +0ms
  libp2p:upgrader handling inbound crypto protocol selection [ '/plaintext/2.0.0' ] +0ms
  libp2p:tcp:socket socket ended /ip4/127.0.0.1/tcp/52626 +0ms
  libp2p:upgrader:error Failed to upgrade inbound connection /Users/varun/src/farcaster/hub/node_modules/it-reader/src/index.ts:44
        new Error(`stream ended before ${bytes} bytes became available`),
        ^

Error: stream ended before 1 bytes became available
1 Like