Skip to content

Commit 9e1fb94

Browse files
failures
1 parent 2831995 commit 9e1fb94

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/sessions.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,10 @@ export class ClientSession
731731
const startTime = this.timeoutContext?.csotEnabled() ? this.timeoutContext.start : now();
732732

733733
let committed = false;
734-
let result: any;
734+
let result: T;
735735

736736
try {
737-
while (!committed) {
737+
for (let retry = 0; !committed; ++retry) {
738738
this.startTransaction(options); // may throw on error
739739

740740
try {
@@ -778,7 +778,7 @@ export class ClientSession
778778
throw fnError;
779779
}
780780

781-
for (let retry = 0; !committed; ++retry) {
781+
while (!committed) {
782782
try {
783783
/*
784784
* We will rely on ClientSession.commitTransaction() to
@@ -816,7 +816,10 @@ export class ClientSession
816816
* { ok:0, code: 50, codeName: 'MaxTimeMSExpired' }
817817
* { ok:1, writeConcernError: { code: 50, codeName: 'MaxTimeMSExpired' } }
818818
*/
819+
continue;
820+
}
819821

822+
if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) {
820823
const BACKOFF_INITIAL_MS = 1;
821824
const BACKOFF_MAX_MS = 500;
822825
const jitter = Math.random();
@@ -829,10 +832,6 @@ export class ClientSession
829832

830833
await setTimeout(backoffMS);
831834

832-
continue;
833-
}
834-
835-
if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) {
836835
break;
837836
}
838837
}
@@ -841,6 +840,7 @@ export class ClientSession
841840
}
842841
}
843842
}
843+
844844
return result;
845845
} finally {
846846
this.timeoutContext = null;

test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { type CommandFailedEvent, type MongoClient } from '../../mongodb';
55
import { configureFailPoint } from '../../tools/utils';
66
import { filterForCommands } from '../shared';
77

8+
const COMMIT_FAIL_TIMES = 35;
9+
810
describe('Retry Backoff is Enforced', function () {
911
// Drivers should test that retries within `withTransaction` do not occur immediately. Optionally, set BACKOFF_INITIAL to a
1012
// higher value to decrease flakiness of this test. Configure a fail point that forces 30 retries. Check that the total
@@ -24,12 +26,11 @@ describe('Retry Backoff is Enforced', function () {
2426
await configureFailPoint(this.configuration, {
2527
configureFailPoint: 'failCommand',
2628
mode: {
27-
times: 30
29+
times: COMMIT_FAIL_TIMES
2830
},
2931
data: {
3032
failCommands: ['commitTransaction'],
31-
errorCode: 24,
32-
errorLabels: ['UnknownTransactionCommitResult']
33+
errorCode: 24
3334
}
3435
});
3536
});
@@ -39,20 +40,29 @@ describe('Retry Backoff is Enforced', function () {
3940
});
4041

4142
for (let i = 0; i < 250; ++i) {
42-
test.only('works' + i, async function () {
43-
const start = performance.now();
43+
test.only(
44+
'works' + i,
45+
{
46+
requires: {
47+
mongodb: '>=4.4', // failCommand
48+
topology: '!single' // transactions can't run on standalone servers
49+
}
50+
},
51+
async function () {
52+
const start = performance.now();
4453

45-
await client.withSession(async s => {
46-
await s.withTransaction(async s => {
47-
await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s });
54+
await client.withSession(async s => {
55+
await s.withTransaction(async s => {
56+
await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s });
57+
});
4858
});
49-
});
5059

51-
const end = performance.now();
60+
const end = performance.now();
5261

53-
expect(failures).to.have.lengthOf(30);
62+
expect(failures).to.have.lengthOf(COMMIT_FAIL_TIMES);
5463

55-
expect(end - start).to.be.greaterThan(1250);
56-
});
64+
expect(end - start).to.be.greaterThan(1500);
65+
}
66+
);
5767
}
5868
});

0 commit comments

Comments
 (0)