Skip to content

Commit

Permalink
fix(data-service): don't abort the transaction from 'outside' the tra…
Browse files Browse the repository at this point in the history
…nsaction COMPASS-7368 (#5107)

* don't abort the transaction from 'outside' the transaction

* clarify a bit
  • Loading branch information
lerouxb authored Nov 14, 2023
1 parent 472fe26 commit 2126ae5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/data-service/src/data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2391,10 +2391,8 @@ class DataServiceImpl extends WithLogContext implements DataService {
.sort({ _id: 1 })
.toArray();

if (session.inTransaction()) {
await session.abortTransaction();
await session.endSession();
}
await session.abortTransaction();
await session.endSession();

const changes = docsToPreview.map((before, idx) => ({
before,
Expand All @@ -2409,10 +2407,14 @@ class DataServiceImpl extends WithLogContext implements DataService {
} catch (err: any) {
if (isTransactionAbortError(err)) {
// The transaction was aborted while it was still calculating the
// preview. Just return something here rather than erroring.
// preview. Just return something here rather than erroring. No
// reason to abort the transaction or end the session again because
// we only got here because that already happened.
return { changes: [] };
}

// The only way this if statement would be true is if aborting the
// transaction and ending the session itself failed above. Unlikely.
if (session.inTransaction()) {
await session.abortTransaction();
await session.endSession();
Expand All @@ -2421,11 +2423,13 @@ class DataServiceImpl extends WithLogContext implements DataService {
throw err;
}
},
async (session) => {
if (session.inTransaction()) {
await session.abortTransaction();
await session.endSession();
}
async () => {
// Rely on the session being killed when we cancel the operation. It can
// take a few seconds before the driver reacts, but the Promise.race()
// against the abort signal should cause the UI to immediately be
// notified that the operation was cancelled. We don't abort the
// transaction or end the session as well because the code we run inside
// session.withTransaction() above would experience race conditions.
},
abortSignal
);
Expand Down

0 comments on commit 2126ae5

Please sign in to comment.