diff --git a/src/logical-replication-service.ts b/src/logical-replication-service.ts index 39bd98d..f1b923d 100644 --- a/src/logical-replication-service.ts +++ b/src/logical-replication-service.ts @@ -102,11 +102,14 @@ export class LogicalReplicationService extends EventEmitter2 implements LogicalR this._messageQueue = []; this._processing = false; + // End the client before removing listeners so the subscribe() promise + // can resolve/reject properly when the connection closes. + await this._client?.end(); + this._connection?.removeAllListeners(); this._connection = null; this._client?.removeAllListeners(); - await this._client?.end(); this._client = null; this.checkStandbyStatus(false); @@ -170,7 +173,10 @@ export class LogicalReplicationService extends EventEmitter2 implements LogicalR this._lastLsn = lsn; }); - return plugin.start(client, slotName, this._lastLsn || '0/00000000'); + return plugin.start(client, slotName, this._lastLsn || '0/00000000').catch(e => { + if (!this._stop || !/Connection\s+terminated/i.test(e?.toString())) + throw e; + }); } catch (e) { await this.stop(); this.emit('error', e); diff --git a/tsconfig.json b/tsconfig.json index b6ec4d8..fdf01ae 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -36,7 +36,7 @@ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ - // "types": [], /* Specify type package names to be included without being referenced in a source file. */ + "types": ["jest", "node"], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ // "resolveJsonModule": true, /* Enable importing .json files. */