Skip to content

Commit 79b2557

Browse files
authored
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
1 parent 68882aa commit 79b2557

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

packages/plugin-search/src/utilities/generateReindexHandler.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const generateReindexHandler =
147147
}
148148
}
149149

150-
await initTransaction(req)
150+
const shouldCommit = await initTransaction(req)
151151

152152
try {
153153
const promises = collections.map(async (collection) => {
@@ -157,14 +157,14 @@ export const generateReindexHandler =
157157
} catch (err) {
158158
const message = t('error:unableToReindexCollection', { collection })
159159
payload.logger.error({ err, msg: message })
160-
161-
await killTransaction(req)
162-
throw new Error(message)
163160
}
164161
})
165162

166163
await Promise.all(promises)
167164
} catch (err: any) {
165+
if (shouldCommit) {
166+
await killTransaction(req)
167+
}
168168
return Response.json({ message: err.message }, { headers, status: 500 })
169169
}
170170

@@ -174,7 +174,9 @@ export const generateReindexHandler =
174174
total: aggregateDocs,
175175
})
176176

177-
await commitTransaction(req)
177+
if (shouldCommit) {
178+
await commitTransaction(req)
179+
}
178180

179181
return Response.json({ message }, { headers, status: 200 })
180182
}

0 commit comments

Comments
 (0)