TypeOrm: This socket has been ended by the other party
See original GitHub issueI’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:
- Created a year ago
- Comments:9 (9 by maintainers)
Go for it @tommarien
@sidorares is it ok if i create a small pr for this change, just so that connection emits ‘end’ events from the underlying stream ?