From c5922378ea972b164d56b7c592c6aa9d2a571746 Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 10:48:52 -0600 Subject: [PATCH 1/7] misc cleanup to improve readability --- src/sessions.ts | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 0788ff1538..8990da6de7 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -768,7 +768,7 @@ export class ClientSession if ( fnError.hasErrorLabel(MongoErrorLabel.TransientTransactionError) && - (this.timeoutContext != null || now() - startTime < MAX_TIMEOUT) + (this.timeoutContext?.csotEnabled() || now() - startTime < MAX_TIMEOUT) ) { continue; } @@ -786,26 +786,26 @@ export class ClientSession await this.commitTransaction(); committed = true; } catch (commitError) { - /* - * Note: a maxTimeMS error will have the MaxTimeMSExpired - * code (50) and can be reported as a top-level error or - * inside writeConcernError, ex. - * { ok:0, code: 50, codeName: 'MaxTimeMSExpired' } - * { ok:1, writeConcernError: { code: 50, codeName: 'MaxTimeMSExpired' } } - */ - if ( - !isMaxTimeMSExpiredError(commitError) && - commitError.hasErrorLabel(MongoErrorLabel.UnknownTransactionCommitResult) && - (this.timeoutContext != null || now() - startTime < MAX_TIMEOUT) - ) { - continue; - } - - if ( - commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError) && - (this.timeoutContext != null || now() - startTime < MAX_TIMEOUT) - ) { - break; + // If CSOT is enabled, we repeatedly retry until timeoutMS expires. + // If CSOT is not enabled, do we still have time remaining or have we timed out? + if (this.timeoutContext?.csotEnabled() || now() - startTime < MAX_TIMEOUT) { + if ( + !isMaxTimeMSExpiredError(commitError) && + commitError.hasErrorLabel(MongoErrorLabel.UnknownTransactionCommitResult) + ) { + /* + * Note: a maxTimeMS error will have the MaxTimeMSExpired + * code (50) and can be reported as a top-level error or + * inside writeConcernError, ex. + * { ok:0, code: 50, codeName: 'MaxTimeMSExpired' } + * { ok:1, writeConcernError: { code: 50, codeName: 'MaxTimeMSExpired' } } + */ + continue; + } + + if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { + break; + } } throw commitError; From 60d3aa4e7380a37a871477f3945ccf2806c73f59 Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 11:19:13 -0600 Subject: [PATCH 2/7] add exponential backoff to withTransaction retry loop --- src/sessions.ts | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 8990da6de7..f03e6aa479 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -1,3 +1,5 @@ +import { setTimeout } from 'timers/promises'; + import { Binary, type Document, Long, type Timestamp } from './bson'; import type { CommandOptions, Connection } from './cmap/connection'; import { ConnectionPoolMetrics } from './cmap/metrics'; @@ -776,7 +778,7 @@ export class ClientSession throw fnError; } - while (!committed) { + for (let retry = 0; !committed; ++retry) { try { /* * We will rely on ClientSession.commitTransaction() to @@ -786,9 +788,23 @@ export class ClientSession await this.commitTransaction(); committed = true; } catch (commitError) { + const hasNotTimedOut = + this.timeoutContext?.csotEnabled() || now() - startTime < MAX_TIMEOUT; + + /** + * will the provided backoffMS exceed the withTransaction's deadline? + */ + const willExceedTransactionDeadline = (backoffMS: number) => { + return ( + (this.timeoutContext?.csotEnabled() && + backoffMS > this.timeoutContext.remainingTimeMS) || + now() + backoffMS > startTime + MAX_TIMEOUT + ); + }; + // If CSOT is enabled, we repeatedly retry until timeoutMS expires. // If CSOT is not enabled, do we still have time remaining or have we timed out? - if (this.timeoutContext?.csotEnabled() || now() - startTime < MAX_TIMEOUT) { + if (hasNotTimedOut) { if ( !isMaxTimeMSExpiredError(commitError) && commitError.hasErrorLabel(MongoErrorLabel.UnknownTransactionCommitResult) @@ -800,6 +816,19 @@ export class ClientSession * { ok:0, code: 50, codeName: 'MaxTimeMSExpired' } * { ok:1, writeConcernError: { code: 50, codeName: 'MaxTimeMSExpired' } } */ + + const BACKOFF_INITIAL_MS = 1; + const BACKOFF_MAX_MS = 500; + const jitter = Math.random(); + const backoffMS = + jitter * Math.min(BACKOFF_INITIAL_MS * 1.25 ** retry, BACKOFF_MAX_MS); + + if (willExceedTransactionDeadline(backoffMS)) { + break; + } + + await setTimeout(backoffMS); + continue; } From 283199503efbfd445a2e7e05e4e13adcce0abe1f Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 14:10:01 -0600 Subject: [PATCH 3/7] test --- .../transactions-convenient-api.prose.test.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts diff --git a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts new file mode 100644 index 0000000000..16a03f8b36 --- /dev/null +++ b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts @@ -0,0 +1,58 @@ +import { expect } from 'chai'; +import { test } from 'mocha'; + +import { type CommandFailedEvent, type MongoClient } from '../../mongodb'; +import { configureFailPoint } from '../../tools/utils'; +import { filterForCommands } from '../shared'; + +describe('Retry Backoff is Enforced', function () { + // Drivers should test that retries within `withTransaction` do not occur immediately. Optionally, set BACKOFF_INITIAL to a + // higher value to decrease flakiness of this test. Configure a fail point that forces 30 retries. Check that the total + // time for all retries exceeded 1.25 seconds. + + let client: MongoClient; + let failures: Array; + + beforeEach(async function () { + client = this.configuration.newClient({}, { monitorCommands: true }); + + failures = []; + client.on('commandFailed', filterForCommands('commitTransaction', failures)); + + await client.connect(); + + await configureFailPoint(this.configuration, { + configureFailPoint: 'failCommand', + mode: { + times: 30 + }, + data: { + failCommands: ['commitTransaction'], + errorCode: 24, + errorLabels: ['UnknownTransactionCommitResult'] + } + }); + }); + + afterEach(async function () { + await client?.close(); + }); + + for (let i = 0; i < 250; ++i) { + test.only('works' + i, async function () { + const start = performance.now(); + + await client.withSession(async s => { + await s.withTransaction(async s => { + await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); + }); + }); + + const end = performance.now(); + + expect(failures).to.have.lengthOf(30); + + expect(end - start).to.be.greaterThan(1250); + }); + } +}); From 9e1fb94e65d3c990de95221e0ee8bb457579ab56 Mon Sep 17 00:00:00 2001 From: bailey Date: Thu, 30 Oct 2025 09:28:19 -0600 Subject: [PATCH 4/7] failures --- src/sessions.ts | 14 ++++---- .../transactions-convenient-api.prose.test.ts | 36 ++++++++++++------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index f03e6aa479..1a3fb73727 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -731,10 +731,10 @@ export class ClientSession const startTime = this.timeoutContext?.csotEnabled() ? this.timeoutContext.start : now(); let committed = false; - let result: any; + let result: T; try { - while (!committed) { + for (let retry = 0; !committed; ++retry) { this.startTransaction(options); // may throw on error try { @@ -778,7 +778,7 @@ export class ClientSession throw fnError; } - for (let retry = 0; !committed; ++retry) { + while (!committed) { try { /* * We will rely on ClientSession.commitTransaction() to @@ -816,7 +816,10 @@ export class ClientSession * { ok:0, code: 50, codeName: 'MaxTimeMSExpired' } * { ok:1, writeConcernError: { code: 50, codeName: 'MaxTimeMSExpired' } } */ + continue; + } + if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { const BACKOFF_INITIAL_MS = 1; const BACKOFF_MAX_MS = 500; const jitter = Math.random(); @@ -829,10 +832,6 @@ export class ClientSession await setTimeout(backoffMS); - continue; - } - - if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { break; } } @@ -841,6 +840,7 @@ export class ClientSession } } } + return result; } finally { this.timeoutContext = null; diff --git a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts index 16a03f8b36..c763090ca0 100644 --- a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts +++ b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts @@ -5,6 +5,8 @@ import { type CommandFailedEvent, type MongoClient } from '../../mongodb'; import { configureFailPoint } from '../../tools/utils'; import { filterForCommands } from '../shared'; +const COMMIT_FAIL_TIMES = 35; + describe('Retry Backoff is Enforced', function () { // Drivers should test that retries within `withTransaction` do not occur immediately. Optionally, set BACKOFF_INITIAL to a // 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 () { await configureFailPoint(this.configuration, { configureFailPoint: 'failCommand', mode: { - times: 30 + times: COMMIT_FAIL_TIMES }, data: { failCommands: ['commitTransaction'], - errorCode: 24, - errorLabels: ['UnknownTransactionCommitResult'] + errorCode: 24 } }); }); @@ -39,20 +40,29 @@ describe('Retry Backoff is Enforced', function () { }); for (let i = 0; i < 250; ++i) { - test.only('works' + i, async function () { - const start = performance.now(); + test.only( + 'works' + i, + { + requires: { + mongodb: '>=4.4', // failCommand + topology: '!single' // transactions can't run on standalone servers + } + }, + async function () { + const start = performance.now(); - await client.withSession(async s => { - await s.withTransaction(async s => { - await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); + await client.withSession(async s => { + await s.withTransaction(async s => { + await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); + }); }); - }); - const end = performance.now(); + const end = performance.now(); - expect(failures).to.have.lengthOf(30); + expect(failures).to.have.lengthOf(COMMIT_FAIL_TIMES); - expect(end - start).to.be.greaterThan(1250); - }); + expect(end - start).to.be.greaterThan(1500); + } + ); } }); From 44e977172d84dd149a6be5a9d921a8e2f6805895 Mon Sep 17 00:00:00 2001 From: bailey Date: Thu, 30 Oct 2025 11:13:42 -0600 Subject: [PATCH 5/7] increase test run count --- .../transactions-convenient-api.prose.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts index c763090ca0..7f982c4f0b 100644 --- a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts +++ b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts @@ -61,7 +61,7 @@ describe('Retry Backoff is Enforced', function () { expect(failures).to.have.lengthOf(COMMIT_FAIL_TIMES); - expect(end - start).to.be.greaterThan(1500); + expect(end - start).to.be.greaterThan(1250); } ); } From 7ac265dd20a22d973887bdbaa813ed39f179802e Mon Sep 17 00:00:00 2001 From: bailey Date: Tue, 18 Nov 2025 14:38:08 -0700 Subject: [PATCH 6/7] Update for changes, test passing --- src/sessions.ts | 4 +- .../transactions-convenient-api.prose.test.ts | 92 ++++++++++--------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 1a3fb73727..9bb648b292 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -820,11 +820,11 @@ export class ClientSession } if (commitError.hasErrorLabel(MongoErrorLabel.TransientTransactionError)) { - const BACKOFF_INITIAL_MS = 1; + const BACKOFF_INITIAL_MS = 5; const BACKOFF_MAX_MS = 500; const jitter = Math.random(); const backoffMS = - jitter * Math.min(BACKOFF_INITIAL_MS * 1.25 ** retry, BACKOFF_MAX_MS); + jitter * Math.min(BACKOFF_INITIAL_MS * 1.5 ** retry, BACKOFF_MAX_MS); if (willExceedTransactionDeadline(backoffMS)) { break; diff --git a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts index 7f982c4f0b..398cce6317 100644 --- a/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts +++ b/test/integration/transactions-convenient-api/transactions-convenient-api.prose.test.ts @@ -1,68 +1,72 @@ import { expect } from 'chai'; import { test } from 'mocha'; +import * as sinon from 'sinon'; -import { type CommandFailedEvent, type MongoClient } from '../../mongodb'; -import { configureFailPoint } from '../../tools/utils'; -import { filterForCommands } from '../shared'; +import { type MongoClient } from '../../../src'; +import { configureFailPoint, type FailCommandFailPoint, measureDuration } from '../../tools/utils'; -const COMMIT_FAIL_TIMES = 35; +const failCommand: FailCommandFailPoint = { + configureFailPoint: 'failCommand', + mode: { + times: 13 + }, + data: { + failCommands: ['commitTransaction'], + errorCode: 251 + } +}; describe('Retry Backoff is Enforced', function () { - // Drivers should test that retries within `withTransaction` do not occur immediately. Optionally, set BACKOFF_INITIAL to a - // higher value to decrease flakiness of this test. Configure a fail point that forces 30 retries. Check that the total - // time for all retries exceeded 1.25 seconds. - let client: MongoClient; - let failures: Array; beforeEach(async function () { - client = this.configuration.newClient({}, { monitorCommands: true }); - - failures = []; - client.on('commandFailed', filterForCommands('commitTransaction', failures)); - - await client.connect(); - - await configureFailPoint(this.configuration, { - configureFailPoint: 'failCommand', - mode: { - times: COMMIT_FAIL_TIMES - }, - data: { - failCommands: ['commitTransaction'], - errorCode: 24 - } - }); + client = this.configuration.newClient(); }); afterEach(async function () { + sinon.restore(); await client?.close(); }); - for (let i = 0; i < 250; ++i) { - test.only( - 'works' + i, - { - requires: { - mongodb: '>=4.4', // failCommand - topology: '!single' // transactions can't run on standalone servers - } - }, - async function () { - const start = performance.now(); + test( + 'works', + { + requires: { + mongodb: '>=4.4', // failCommand + topology: '!single' // transactions can't run on standalone servers + } + }, + async function () { + const randomStub = sinon.stub(Math, 'random'); + + randomStub.returns(0); - await client.withSession(async s => { + await configureFailPoint(this.configuration, failCommand); + + const { duration: noBackoffTime } = await measureDuration(() => { + return client.withSession(async s => { await s.withTransaction(async s => { await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); }); }); + }); - const end = performance.now(); + randomStub.returns(1); - expect(failures).to.have.lengthOf(COMMIT_FAIL_TIMES); + await configureFailPoint(this.configuration, failCommand); - expect(end - start).to.be.greaterThan(1250); - } - ); - } + const { duration: fullBackoffDuration } = await measureDuration(() => { + return client.withSession(async s => { + await s.withTransaction(async s => { + await client.db('foo').collection('bar').insertOne({ name: 'bailey' }, { session: s }); + }); + }); + }); + + expect(fullBackoffDuration).to.be.within( + noBackoffTime + 2200 - 1000, + noBackoffTime + 2200 + 1000 + ); + } + ); }); From 40737766834b538aa9f844f5edad654b35964683 Mon Sep 17 00:00:00 2001 From: bailey Date: Tue, 18 Nov 2025 14:50:41 -0700 Subject: [PATCH 7/7] misc cleanup --- src/sessions.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/sessions.ts b/src/sessions.ts index 9bb648b292..b149304028 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -788,22 +788,11 @@ export class ClientSession await this.commitTransaction(); committed = true; } catch (commitError) { + // If CSOT is enabled, we repeatedly retry until timeoutMS expires. + // If CSOT is not enabled, do we still have time remaining or have we timed out? const hasNotTimedOut = this.timeoutContext?.csotEnabled() || now() - startTime < MAX_TIMEOUT; - /** - * will the provided backoffMS exceed the withTransaction's deadline? - */ - const willExceedTransactionDeadline = (backoffMS: number) => { - return ( - (this.timeoutContext?.csotEnabled() && - backoffMS > this.timeoutContext.remainingTimeMS) || - now() + backoffMS > startTime + MAX_TIMEOUT - ); - }; - - // If CSOT is enabled, we repeatedly retry until timeoutMS expires. - // If CSOT is not enabled, do we still have time remaining or have we timed out? if (hasNotTimedOut) { if ( !isMaxTimeMSExpiredError(commitError) && @@ -826,7 +815,12 @@ export class ClientSession const backoffMS = jitter * Math.min(BACKOFF_INITIAL_MS * 1.5 ** retry, BACKOFF_MAX_MS); - if (willExceedTransactionDeadline(backoffMS)) { + const willExceedTransactionDeadline = + (this.timeoutContext?.csotEnabled() && + backoffMS > this.timeoutContext.remainingTimeMS) || + now() + backoffMS > startTime + MAX_TIMEOUT; + + if (willExceedTransactionDeadline) { break; } @@ -841,6 +835,7 @@ export class ClientSession } } + // @ts-expect-error Result is always defined if we reach here, the for-loop above convinces TS it is not. return result; } finally { this.timeoutContext = null;