undici: SocketError: other side closed

Upon connecting to a proxy with user:pass it’s all good, but when I try to make a request to an proxy which is IP authenticated, I get ‘SocketError: other side closed’. I’m not sending the proxy-authorization header as it’s not needed. Blanked the proxy ip and port below.

var Client = require('undici');
(async ()=>{
	const client = new Client("http://proxyip:proxyport")
	const response = await client.request({
	  method: 'GET',
	  path: 'https://api.ipify.org?format=json',
	  headers: {
	    //'proxy-authorization': `Basic ${Buffer.from(':').toString('base64')}`
	  }
	})

	response.body.setEncoding('utf8')
	let data = ''
	for await (const chunk of response.body) {
	  data += chunk
	}
	//
	console.log(response.statusCode)
	console.log(JSON.parse(data))

	client.close()
})();

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 50 (29 by maintainers)

Commits related to this issue

Most upvoted comments

I’m still facing the issue in 2024.

For anyone pulling their hair over this, this issue is not at all encountered with the bun runtime, give it a shot.

This issue has been closed since 3 years ago; please open a new one with a description and an Minimum Reproducible Example if possible 🙂

I dont think this issue should be closed. @mcollina

I have managed to fix this and other socket errors in my applications by limiting the nuber of connections NodeJS is allowed to open to the server. I observed that the node process was opening too many connections to the server, that is, using too many local ports of the host (16000+ in my case) and eventually these connections would end abruptly and cause the error.

The number of connections can be limited via setting the connections param in the httpAgent

https.Agent({
  maxSockets: 50,
})

50 might not be an ideal number for you and you might still face the issue or your application might slow down because the process has lesser number of connections. You need to trial and error and find a number that works for you. 100 worked well for me when I did over 2 million async API calls for testing the limits.

You can monitor the number of ports used by your node process on unix based systems via the following command

lsof -i -n -P | grep node | awk '{print $9}' | awk -F '->' '{print $1}' | awk -F ':' '{print $2}' | sort -u | wc -l

Bun used around 400-500 ports and was faster than NodeJS. I’ve tested using node v20.13.1 and bun 1.1.8 on my local

I haven’t dug into whether the OS limits the number of connections, runs out of them or NodeJS is unable to handle them

If this doesn’t fix the issue for you and you want to dig in further, a packet capture can be helpful. I did a pcap and analyzed the packets in Wireshark. You’d want to look for the flow, source, destination, origin of the SYN, SYN/ACK, RST packets. The expert analysis tool in Wireshark does a great job at adding clarity as well.

Also, if the server you’re connecting to has rate limits, give a look at what it does to new connection requests and exising connections once the limits are hit

Overall, this issue is now fixed for me and for all of the people I’ve recommended the above mentioned fix.

fwiw, I was able to reproduce this consistently while running the VS Code interactive debugger on test cases of some api code I was writing using the fetch function. If I paused for more than a few second the next fetch call would result in the error.

I assume there’s other ways to fix this, but adding

Connection: close

to the request headers seems to have avoided it for now.

The bug can popup when the disk is full also

The fix is to call await body.text() after every response… 🤦

This definitely seems like a major anti-pattern @mcollina @ronag. We don’t explicitly state anywhere in the undici docs to call a body mixin (otherwise this error will occur). I think there is two things that need done:

  1. There is most likely a bug here https://github.com/nodejs/undici/issues/583#issuecomment-1577449824. Also see our note about body.destroy() not working in combination with signal.
  2. Invoking a body mixin method such as body.text() should not be required in order to keep requests flowing.

You need to consume or destroy the response body. I think the docs mention this.

Alright, I will see if I can narrow it down.

Thanks! Tracking down those conditions is hard 😦.

Alright, I will see if I can narrow it down.