question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeOrm: This socket has been ended by the other party

See original GitHub issue

I’ve seen there a lot of open issues considering this, i may have stumbled upon something interesting while investigating the very same error.

Our context

We are using an AWS serverless architecture where our mysql server is behind an RDS Proxy.

First i was thinking we were missing the configuration to keep our connections in the pool alive, see this issue for more info.

Although that seems functional it did not have any effect on our particular situation.

We have our idleclientTimeout for the RDS proxy configured around 120 secs, so the issue is really a big issue with us. Although we could up the timeout to something more reasonable we would keep finding ourselves in the same situation after lets say 30 minutes.

Because of the specific sleepstate of lamda’s on aws, the keepalive makes no difference, because no user codes executes anymore after running the function.

I’ve seen we did some handling in connection.js of the connection’s close event.

Findings

The socket has been ended happens when the connection is used by a query, so i changed something in the connection.js class and added a listener on the end event

    this.stream.on('end', () => {
      console.warn('connection recycle');
    });

    // below is existing code ;)
    this.stream.on('close', () => {
      // we need to set this flag everywhere where we want connection to close
      if (this._closing) {
        return;
      }
      if (!this._protocolError) {
        // no particular error message before disconnect
        this._protocolError = new Error('Connection lost: The server closed the connection.');
        this._protocolError.fatal = true;
        this._protocolError.code = 'PROTOCOL_CONNECTION_LOST';
      }
      this._notifyError(this._protocolError);
    });

With that change, i could see that when starting the function the log contained a ‘connection recycle’ item, just before the function fired. And off course failed with the ‘socket has been ended shizzle’

From the time is changed that function to contain the same logic as the `on('close`` handler the problem of the socket has been ended disappears and everything works is it should.

    this.stream.on('end', () => {
      console.warn('connection recycle');
      // we need to set this flag everywhere where we want connection to close
      if (this._closing) {
        return;
      }
      if (!this._protocolError) {
        // no particular error message before disconnect
        this._protocolError = new Error('Connection lost: The server closed the connection.');
        this._protocolError.fatal = true;
        this._protocolError.code = 'PROTOCOL_CONNECTION_LOST';
      }
      this._notifyError(this._protocolError);
    });

Please advise, i don’t mind creating a pull for this if you think this is useful 😃

i don’t think this is the actual code change that is needed but i just seems to solve the issue, it has something to do with the setting of the _fatalError.

Update

when i compare the handling with the mysql package i see them handling the end event too, so i guess we need to do something there

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
sidorarescommented, Nov 4, 2022

Go for it @tommarien

0reactions
tommariencommented, Nov 4, 2022

@sidorares is it ok if i create a small pr for this change, just so that connection emits ‘end’ events from the underlying stream ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Pool] This socket has been ended by the other party #447
I'm using the latest v1.1.1. { Error: This socket has been ended by the other party at TLSSocket.writeAfterFIN [as write] (net.js: ...
Read more >
Intermittent error "This socket has been ended by the other party"
The error is "This socket has been ended by the other party". This is my Dockerfile with the opened ports: EXPOSE 7474 7473...
Read more >
Using AWS Lambda with RDS Proxy: Socket Ended Error
Some background:** I have a Node 12 Lambda running an Apollo GraphQL server. ... Error: This socket has been ended by the other...
Read more >
How to handle a socket hang up error in Node.js usually - Quora
I ignore the error (because 9/10 times the client just closed the browser); use res.end() to make sure the response has been closed;...
Read more >
Node.js with MySQL - w3resource
connection.end(function(err) { // The connection is terminated now });. This will make sure all previously enqueued queries are still before ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found