From d4e85b4252b59cb7aee095930490917b2e6ae0b5 Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 11:51:19 -0600 Subject: [PATCH 1/4] data bearing servers only --- test/tools/unified-spec-runner/entities.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/tools/unified-spec-runner/entities.ts b/test/tools/unified-spec-runner/entities.ts index 4e238567fd..f332e2de33 100644 --- a/test/tools/unified-spec-runner/entities.ts +++ b/test/tools/unified-spec-runner/entities.ts @@ -629,10 +629,10 @@ export class EntitiesMap extends Map { if (entity.client.awaitMinPoolSizeMS) { if (client.topology?.s?.servers) { const timeout = Timeout.expires(entity.client.awaitMinPoolSizeMS); - const servers = client.topology.s.servers.values(); - const poolSizeChecks = Array.from(servers).map(server => - checkMinPoolSize(server.pool) + const servers = Array.from(client.topology.s.servers.values()).filter( + ({ description: { isDataBearing } }) => isDataBearing ); + const poolSizeChecks = servers.map(server => checkMinPoolSize(server.pool)); try { await Promise.race([Promise.allSettled(poolSizeChecks), timeout]); } catch (error) { From d26230d6f53ded5448231368a8a39b2d2af104e0 Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 10:48:52 -0600 Subject: [PATCH 2/4] 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 605136ea46cae492ecbe52e2679a3b5d757b2aec Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 11:19:13 -0600 Subject: [PATCH 3/4] 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 b6c76a9978d3dd0afd936473babf522360f7dab6 Mon Sep 17 00:00:00 2001 From: bailey Date: Wed, 29 Oct 2025 14:10:01 -0600 Subject: [PATCH 4/4] 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); + }); + } +});