Skip to content

Commit 781db35

Browse files
authored
fix: ensure the same error listener is not attached multiple times (#599)
1 parent 823371e commit 781db35

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

mysql/lib/mysql_error_handler.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export class MySQLErrorHandler implements ErrorHandler {
2424
private unexpectedError: Error | null = null;
2525
protected static readonly SYNTAX_ERROR_CODES = ["42000", "42S02"];
2626
protected static readonly SYNTAX_ERROR_MESSAGE = "You have an error in your SQL syntax";
27+
protected isNoOpListenerAttached = false;
28+
protected isTrackingListenerAttached = false;
2729

2830
protected noOpListener(error: any) {
2931
// Ignore the received error.
@@ -80,27 +82,32 @@ export class MySQLErrorHandler implements ErrorHandler {
8082
}
8183

8284
attachErrorListener(clientWrapper: ClientWrapper | undefined): void {
83-
if (!clientWrapper || !clientWrapper.client) {
85+
if (!clientWrapper || !clientWrapper.client || this.isTrackingListenerAttached) {
8486
return;
8587
}
8688
this.unexpectedError = null;
87-
clientWrapper.client.removeListener("error", this.noOpListener);
88-
clientWrapper.client.on("error", this.trackingListener);
89+
clientWrapper.client.connection.removeListener("error", this.noOpListener);
90+
this.isNoOpListenerAttached = false;
91+
clientWrapper.client.connection.on("error", this.trackingListener);
92+
this.isTrackingListenerAttached = true;
8993
}
9094

9195
attachNoOpErrorListener(clientWrapper: ClientWrapper | undefined): void {
92-
if (!clientWrapper || !clientWrapper.client) {
96+
if (!clientWrapper || !clientWrapper.client || this.isNoOpListenerAttached) {
9397
return;
9498
}
95-
clientWrapper.client.removeListener("error", this.trackingListener);
96-
clientWrapper.client.on("error", this.noOpListener);
99+
clientWrapper.client.connection.removeListener("error", this.trackingListener);
100+
this.isTrackingListenerAttached = false;
101+
clientWrapper.client.connection.on("error", this.noOpListener);
102+
this.isNoOpListenerAttached = true;
97103
}
98104

99105
removeErrorListener(clientWrapper: ClientWrapper | undefined): void {
100106
if (!clientWrapper || !clientWrapper.client) {
101107
return;
102108
}
103109

104-
clientWrapper.client.removeListener("error", this.trackingListener);
110+
clientWrapper.client.connection.removeListener("error", this.trackingListener);
111+
this.isTrackingListenerAttached = false;
105112
}
106113
}

pg/lib/abstract_pg_error_handler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export abstract class AbstractPgErrorHandler implements ErrorHandler {
2424
protected unexpectedError: Error | null = null;
2525
protected static readonly SYNTAX_ERROR_CODE = "42601";
2626
protected static readonly SYNTAX_ERROR_MESSAGE = "syntax error";
27+
protected isNoOpListenerAttached = false;
28+
protected isTrackingListenerAttached = false;
2729

2830
abstract getNetworkErrors(): string[];
2931

@@ -84,20 +86,24 @@ export abstract class AbstractPgErrorHandler implements ErrorHandler {
8486
}
8587

8688
attachErrorListener(clientWrapper: ClientWrapper | undefined): void {
87-
if (!clientWrapper || !clientWrapper.client) {
89+
if (!clientWrapper || !clientWrapper.client || this.isTrackingListenerAttached) {
8890
return;
8991
}
9092
this.unexpectedError = null;
9193
clientWrapper.client.removeListener("error", this.noOpListener);
94+
this.isNoOpListenerAttached = false;
9295
clientWrapper.client.on("error", this.trackingListener);
96+
this.isTrackingListenerAttached = true;
9397
}
9498

9599
attachNoOpErrorListener(clientWrapper: ClientWrapper | undefined): void {
96-
if (!clientWrapper || !clientWrapper.client) {
100+
if (!clientWrapper || !clientWrapper.client || this.isNoOpListenerAttached) {
97101
return;
98102
}
99103
clientWrapper.client.removeListener("error", this.trackingListener);
104+
this.isTrackingListenerAttached = false;
100105
clientWrapper.client.on("error", this.noOpListener);
106+
this.isNoOpListenerAttached = true;
101107
}
102108

103109
removeErrorListener(clientWrapper: ClientWrapper | undefined): void {
@@ -106,5 +112,6 @@ export abstract class AbstractPgErrorHandler implements ErrorHandler {
106112
}
107113

108114
clientWrapper.client.removeListener("error", this.trackingListener);
115+
this.isTrackingListenerAttached = false;
109116
}
110117
}

0 commit comments

Comments
 (0)