Skip to content

Commit

Permalink
fix(watch): fix pool leak on unexpected connection termination (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Jan 29, 2021
1 parent efd3861 commit b2fbc21
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,7 @@ Original error: ${e.message}

_handleClientError: (e: Error) => void;
_handleClientError(e) {
// Client is already cleaned up
this.client = null;
this._reallyReleaseClient = null;
this._releaseClient(false);
this._reconnect(e);
}
async _reconnect(e) {
Expand Down Expand Up @@ -1265,25 +1263,32 @@ Original error: ${e.message}

async stop() {
this.stopped = true;
this._handleChange.cancel();
await this._releaseClient();
}

async _releaseClient() {
/**
* Only pass `false` to this function if you know the client is going to be
* terminated; otherwise we risk leaving listeners running.
*/
async _releaseClient(clientIsStillViable = true) {
// $FlowFixMe
this._handleChange.cancel();
const pgClient = this.client;
const reallyReleaseClient = this._reallyReleaseClient;
this.client = null;
this._reallyReleaseClient = null;
if (pgClient) {
pgClient.query("unlisten postgraphile_watch").catch(e => {
debug(`Error occurred trying to unlisten watch: ${e}`);
});
// Don't attempt to run a query after a client has errored.
if (clientIsStillViable) {
pgClient.query("unlisten postgraphile_watch").catch(e => {
debug(`Error occurred trying to unlisten watch: ${e}`);
});
}
pgClient.removeListener("notification", this._listener);
pgClient.removeListener("error", this._handleClientError);
if (reallyReleaseClient) {
await reallyReleaseClient();
}
}
if (reallyReleaseClient) {
await reallyReleaseClient();
}
}
}
Expand Down

0 comments on commit b2fbc21

Please sign in to comment.