Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,10 @@ class Connection extends EventEmitter {
*/
declare databaseCollation: Collation | undefined;

private onSocketError: (error: Error) => void;
private onSocketClose: () => void;
private onSocketEnd: () => void;

/**
* Note: be aware of the different options field:
* 1. config.authentication.options
Expand Down Expand Up @@ -1742,6 +1746,10 @@ class Connection extends EventEmitter {

this.state = this.STATE.INITIALIZED;

this.onSocketError = (error: Error) => { this.socketError(error); };
this.onSocketClose = () => { this.socketClose(); };
this.onSocketEnd = () => { this.socketEnd(); };

this._cancelAfterRequestSent = () => {
this.messageIo.sendMessage(TYPE.ATTENTION);
this.createCancelTimer();
Expand All @@ -1756,16 +1764,25 @@ class Connection extends EventEmitter {
if (connectListener) {
const onConnect = (err?: Error) => {
this.removeListener('error', onError);
this.removeListener('end', onEnd);
connectListener(err);
};

const onError = (err: Error) => {
this.removeListener('connect', onConnect);
this.removeListener('end', onEnd);
connectListener(err);
};

const onEnd = () => {
this.removeListener('connect', onConnect);
this.removeListener('error', onError);
connectListener(new Error('unexpected connection end during connect'));
}

this.once('connect', onConnect);
this.once('error', onError);
this.once('end', onEnd);
}

this.transitionTo(this.STATE.CONNECTING);
Expand Down Expand Up @@ -1992,9 +2009,9 @@ class Connection extends EventEmitter {
}

socketHandlingForSendPreLogin(socket: net.Socket) {
socket.on('error', (error) => { this.socketError(error); });
socket.on('close', () => { this.socketClose(); });
socket.on('end', () => { this.socketEnd(); });
socket.on('error', this.onSocketError);
socket.on('close', this.onSocketClose);
socket.on('end', this.onSocketEnd);
socket.setKeepAlive(true, KEEP_ALIVE_INITIAL_DELAY);

this.messageIo = new MessageIO(socket, this.config.options.packetSize, this.debug);
Expand Down Expand Up @@ -3395,6 +3412,8 @@ Connection.prototype.STATE = {
if (handler.loginAckReceived) {
if (handler.routingData) {
this.routingData = handler.routingData;

console.log('socket closed before rerouting?', this.socket?.closed);
this.transitionTo(this.STATE.REROUTING);
} else {
this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
Expand Down Expand Up @@ -3446,6 +3465,7 @@ Connection.prototype.STATE = {
if (handler.loginAckReceived) {
if (handler.routingData) {
this.routingData = handler.routingData;
console.log('socket closed before rerouting?', this.socket?.closed);
return this.transitionTo(this.STATE.REROUTING);
} else {
return this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
Expand Down Expand Up @@ -3512,6 +3532,7 @@ Connection.prototype.STATE = {
if (handler.loginAckReceived) {
if (handler.routingData) {
this.routingData = handler.routingData;
console.log('socket closed before rerouting?', this.socket?.closed);
this.transitionTo(this.STATE.REROUTING);
} else {
this.transitionTo(this.STATE.LOGGED_IN_SENDING_INITIAL_SQL);
Expand Down