Skip to content

Instantly share code, notes, and snippets.

@nvasilakis
Created February 18, 2025 18:18
Show Gist options
  • Select an option

  • Save nvasilakis/add11803adb7182af156dd390cf3eaca to your computer and use it in GitHub Desktop.

Select an option

Save nvasilakis/add11803adb7182af156dd390cf3eaca to your computer and use it in GitHub Desktop.
post
We've received various questions on Ed and during hours about `ECONN...` errors. We've put together a short tips and tricks doc to help you debug these. These errors often come from subtle race-conditions.
### ECONNREFUSED
The message's recipient's HTTP server is not running at all at the time the message was sent. Examine whether you check for the recipient to start, then have the sender send a message. In other words, you are (a) starting the recipient node too late or (b) closing it too early.
### ECONNRESET
The message's recipient shut down during communication. This indicates that you are shutting down the recipient node too early. Typically, this error appears in tests because the done is misused. If done is called too early, you will get a `ECONNRESET`.
The key to debugging these is closely inspecting your callbacks for race conditions. For instance, the following pseudocode contains a race condition that will lead to an `ECONNRESET`.
```js
for (i = 0; i < n; i++) {
comm.send(foo, bar, () => {
if (i === n - 1) {
done()
}
}
}
```
Can you spot it? The race condition stems from the weak ordering guarantees of HTTP across separate requests. The _i_th HTTP request is not guaranteed to arrive before the _i+1_th request, meaning that you may have a situation where done is called before or during the _n_-2th request's delivery.
If there's anything unclear, please leave comments on this post and we will explain in more detail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment