From 79b25577b9c23c5400280bc7f18801dbcb906ad4 Mon Sep 17 00:00:00 2001 From: Patrik <35232443+PatrikKozak@users.noreply.github.com> Date: Tue, 23 Sep 2025 16:59:46 -0400 Subject: [PATCH] fix(plugin-search): transaction state errors in parallel reindex operations (#13915) ### What? Fixed flaky MongoDB transaction state errors ("Attempted illegal state transition from `[TRANSACTION_ABORTED]` to `[TRANSACTION_COMMITTED]`") in plugin-search int tests. ### Why? When reindexing multiple collections in parallel, individual collection failures were calling `killTransaction()` on a shared transaction that other parallel operations were still using, causing MongoDB transaction state conflicts and test flakiness. ### How? - Moved transaction cleanup to outer catch block only - Removed individual `killTransaction` calls that created race conditions - Allow parallel operations to handle their own errors without aborting the shared transaction --- .../src/utilities/generateReindexHandler.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/plugin-search/src/utilities/generateReindexHandler.ts b/packages/plugin-search/src/utilities/generateReindexHandler.ts index c10ad0caa..91b97f38e 100644 --- a/packages/plugin-search/src/utilities/generateReindexHandler.ts +++ b/packages/plugin-search/src/utilities/generateReindexHandler.ts @@ -147,7 +147,7 @@ export const generateReindexHandler = } } - await initTransaction(req) + const shouldCommit = await initTransaction(req) try { const promises = collections.map(async (collection) => { @@ -157,14 +157,14 @@ export const generateReindexHandler = } catch (err) { const message = t('error:unableToReindexCollection', { collection }) payload.logger.error({ err, msg: message }) - - await killTransaction(req) - throw new Error(message) } }) await Promise.all(promises) } catch (err: any) { + if (shouldCommit) { + await killTransaction(req) + } return Response.json({ message: err.message }, { headers, status: 500 }) } @@ -174,7 +174,9 @@ export const generateReindexHandler = total: aggregateDocs, }) - await commitTransaction(req) + if (shouldCommit) { + await commitTransaction(req) + } return Response.json({ message }, { headers, status: 200 }) }