Skip to content

Commit b2fbc21

Browse files
authored
fix(watch): fix pool leak on unexpected connection termination (#711)
1 parent efd3861 commit b2fbc21

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,7 @@ Original error: ${e.message}
12041204

12051205
_handleClientError: (e: Error) => void;
12061206
_handleClientError(e) {
1207-
// Client is already cleaned up
1208-
this.client = null;
1209-
this._reallyReleaseClient = null;
1207+
this._releaseClient(false);
12101208
this._reconnect(e);
12111209
}
12121210
async _reconnect(e) {
@@ -1265,25 +1263,32 @@ Original error: ${e.message}
12651263

12661264
async stop() {
12671265
this.stopped = true;
1266+
this._handleChange.cancel();
12681267
await this._releaseClient();
12691268
}
12701269

1271-
async _releaseClient() {
1270+
/**
1271+
* Only pass `false` to this function if you know the client is going to be
1272+
* terminated; otherwise we risk leaving listeners running.
1273+
*/
1274+
async _releaseClient(clientIsStillViable = true) {
12721275
// $FlowFixMe
1273-
this._handleChange.cancel();
12741276
const pgClient = this.client;
12751277
const reallyReleaseClient = this._reallyReleaseClient;
12761278
this.client = null;
12771279
this._reallyReleaseClient = null;
12781280
if (pgClient) {
1279-
pgClient.query("unlisten postgraphile_watch").catch(e => {
1280-
debug(`Error occurred trying to unlisten watch: ${e}`);
1281-
});
1281+
// Don't attempt to run a query after a client has errored.
1282+
if (clientIsStillViable) {
1283+
pgClient.query("unlisten postgraphile_watch").catch(e => {
1284+
debug(`Error occurred trying to unlisten watch: ${e}`);
1285+
});
1286+
}
12821287
pgClient.removeListener("notification", this._listener);
12831288
pgClient.removeListener("error", this._handleClientError);
1284-
if (reallyReleaseClient) {
1285-
await reallyReleaseClient();
1286-
}
1289+
}
1290+
if (reallyReleaseClient) {
1291+
await reallyReleaseClient();
12871292
}
12881293
}
12891294
}

0 commit comments

Comments
 (0)