From 6978eb025ffd050ad63b94621844b99ec2944e3f Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 24 Jan 2022 04:27:10 +0100 Subject: [PATCH 1/3] call endSession in transaction helper after we are done with the transaction --- index.d.ts | 2 +- lib/connection.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index ffc8bb26da8..1c1fe12472e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -449,7 +449,7 @@ declare module 'mongoose' { * _Requires MongoDB >= 3.6.0._ Executes the wrapped async function * in a transaction. Mongoose will commit the transaction if the * async function executes successfully and attempt to retry if - * there was a retriable error. + * there was a retryable error. */ transaction(fn: (session: mongodb.ClientSession) => Promise): Promise; diff --git a/lib/connection.js b/lib/connection.js index cd16a60ab11..e6404610b5a 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -504,6 +504,9 @@ Connection.prototype.transaction = function transaction(fn, options) { } delete session[sessionNewDocuments]; throw err; + }) + .finally(() => { + session.endSession(); }); }); }; From 7adfe90c35703e3357fb07d1076975acddcdae3f Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 24 Jan 2022 04:34:52 +0100 Subject: [PATCH 2/3] as it is a promise, we should catch a potential promise rejection --- lib/connection.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index e6404610b5a..65f4ef15bc1 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -506,7 +506,8 @@ Connection.prototype.transaction = function transaction(fn, options) { throw err; }) .finally(() => { - session.endSession(); + session.endSession() + .catch(err => {}); }); }); }; From 25a9d937bfecf0af5a3a8905c2225581583bdb82 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Mon, 24 Jan 2022 04:36:42 +0100 Subject: [PATCH 3/3] remove err --- lib/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index 65f4ef15bc1..c66ca7bd7c4 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -507,7 +507,7 @@ Connection.prototype.transaction = function transaction(fn, options) { }) .finally(() => { session.endSession() - .catch(err => {}); + .catch(() => {}); }); }); };