Local dfx + node > 18 keepalive problems

I upgraded to node 19 and I have a simple test script which uses agent-js to create an agent and make a couple calls (simple queries).

For node 18 and lower this works just fine, but for version 19 or 20 it fails with all subsequent calls having broken connections. This seems to be because 19+ default to enabling keep-alive for node-fetch which is interacting poorly with the local replica. Interestingly this problem does not exist when talking to mainnet, perhaps because of the boundary nodes when are handling keepalive.

AFAICT, node-fetch is trying to do keepalive but agent-js is sending a Connection: close header, and so the replica does a half-close and then sends a reset on the second command down the socket. This is because node-fetch doesn’t see the Connection: close and assumes it can still issue commands down that connection.