fix(db): take the write lock in every transaction that writes

UnitOfWork took the engine's write lock, but four repositories opened
transactions of their own: the demo addon set, the plugin user erasure's
savepoints, and the plugin metadata and scheduler cap checks. On Postgres
those ran beside the others on the pool.

writeTransaction (src/db/engine/write-transaction.ts) is now the one
transaction that writes: UnitOfWork wraps it, and the demo seed and reset,
which run outside the container, call it with the engine the environment
names. The capped plugin writes run in their caller's unit of work instead
of a transaction of their own, and the plugin erasure runs in the erasure's
transaction without savepoints, since its three tables come with the
migrations on both engines. lint:tx counts writeTransaction as a
transaction.
This commit is contained in:
Maurice
2026-11-17 14:14:29 +01:00
parent 981052224c
commit 61ea6b648c
9 changed files with 107 additions and 64 deletions
+5 -3
View File
@@ -19,8 +19,8 @@
* A method's "write units" are then counted over its body: * A method's "write units" are then counted over its body:
* - a call to a write, on a constructor-injected field (`this.trips.x()`) * - a call to a write, on a constructor-injected field (`this.trips.x()`)
* or on its own class (`this.x()`), is one unit; * or on its own class (`this.x()`), is one unit;
* - a `.transactional(...)` call that holds any write is one unit, however * - a `.transactional(...)` or `writeTransaction(...)` call that holds any
* many writes it holds; * write is one unit, however many writes it holds;
* - a call to a method of another class that is itself reported counts as * - a call to a method of another class that is itself reported counts as
* one unit here (the fault is reported once, where it lives); * one unit here (the fault is reported once, where it lives);
* - a write inside a loop, or inside a callback handed to forEach/map/ * - a write inside a loop, or inside a callback handed to forEach/map/
@@ -343,7 +343,9 @@ export function analyse(classes) {
const receiver = ts.isPropertyAccessExpression(callee) const receiver = ts.isPropertyAccessExpression(callee)
? count(cls, callee.expression, covered) ? count(cls, callee.expression, covered)
: count(cls, callee, covered); : count(cls, callee, covered);
if (method === 'transactional') { // `writeTransaction(em, engine, fn)` (src/db/engine/write-transaction.ts) is the
// transaction `UnitOfWork.transactional` opens, for code outside the container.
if (method === 'transactional' || (ts.isIdentifier(callee) && callee.text === 'writeTransaction')) {
let inner = 0; let inner = 0;
frames.push([]); // what runs inside is one unit, not a list of its own frames.push([]); // what runs inside is one unit, not a list of its own
for (const arg of node.arguments) inner += count(cls, arg, true); for (const arg of node.arguments) inner += count(cls, arg, true);
+30
View File
@@ -0,0 +1,30 @@
import type { DatabaseEngine } from './engine';
import type { EntityManager } from '@mikro-orm/core';
/**
* The one way TREK opens a transaction that writes: `em.transactional()` with
* its default `NESTED` propagation (a call inside an open transaction becomes
* a savepoint), whose first statement takes the engine's write lock
* (`DatabaseEngine.serializeWrites`).
*
* On SQLite the lock is free, since one connection already writes one
* transaction at a time. On Postgres a pool runs transactions side by side,
* and without the lock two of them would both read the same `MAX(...) + 1`,
* the same count under a cap or the same "not there yet" before an insert.
* With it, a check read inside the transaction still holds at its write.
*
* `UnitOfWork.transactional` is this for the Nest container; code that runs
* outside it (the demo seed and reset) calls it directly. ESLint
* (`trek/transactions-through-unit-of-work`) refuses any other transaction
* under src/ outside the engine and the boot-time schema files.
*/
export function writeTransaction<T>(
em: EntityManager,
engine: Pick<DatabaseEngine, 'serializeWrites'>,
fn: (em: EntityManager) => Promise<T>,
): Promise<T> {
return em.transactional(async (tx) => {
await engine.serializeWrites(tx);
return fn(tx);
});
}
+7 -2
View File
@@ -1,4 +1,6 @@
import { currentTimestampKysely } from '../dialect/kysely-functions'; import { currentTimestampKysely } from '../dialect/kysely-functions';
import { resolveEngine } from '../engine';
import { writeTransaction } from '../engine/write-transaction';
import { Addons } from '../entities/Addons.entity'; import { Addons } from '../entities/Addons.entity';
import { AppSettings } from '../entities/AppSettings.entity'; import { AppSettings } from '../entities/AppSettings.entity';
import { DocumentProviders } from '../entities/DocumentProviders.entity'; import { DocumentProviders } from '../entities/DocumentProviders.entity';
@@ -420,7 +422,10 @@ export class DemoRepository {
/** /**
* Writes the demo's addon states in one transaction, so a reset that fails * Writes the demo's addon states in one transaction, so a reset that fails
* halfway does not leave half the shelf switched. One UPDATE per shelf and * halfway does not leave half the shelf switched. The transaction takes the
* engine's write lock (`writeTransaction`), as a `UnitOfWork` one does: the
* seed and the reset run outside the Nest container, so the engine is the
* one the environment names. One UPDATE per shelf and
* state (generic inherited `nativeUpdate` over `id IN (...)`, the statement * state (generic inherited `nativeUpdate` over `id IN (...)`, the statement
* `setEnabled` issues for one row) and one upsert for all the switches * `setEnabled` issues for one row) and one upsert for all the switches
* (`AppSettingsRepository#setValue`'s), however long the lists are. A named * (`AppSettingsRepository#setValue`'s), however long the lists are. A named
@@ -430,7 +435,7 @@ export class DemoRepository {
async applyAddonStates(states: DemoAddonStates): Promise<void> { async applyAddonStates(states: DemoAddonStates): Promise<void> {
this.validateRequestContext(); this.validateRequestContext();
const switches = Object.entries(states.appSettings).map(([key, value]) => ({ key, value })); const switches = Object.entries(states.appSettings).map(([key, value]) => ({ key, value }));
await this.em.transactional(async () => { await writeTransaction(this.em, resolveEngine(), async () => {
const addons = this.em.getRepository(Addons); const addons = this.em.getRepository(Addons);
await addons.nativeUpdate({ id: { $in: [...states.addons.on] } }, { enabled: true }); await addons.nativeUpdate({ id: { $in: [...states.addons.on] } }, { enabled: true });
await addons.nativeUpdate({ id: { $in: [...states.addons.off] } }, { enabled: false }); await addons.nativeUpdate({ id: { $in: [...states.addons.off] } }, { enabled: false });
@@ -1,4 +1,3 @@
import { isMissingTableError } from '../dialect/driver-errors';
import type { EntityManager } from '@mikro-orm/core'; import type { EntityManager } from '@mikro-orm/core';
/** /**
@@ -39,24 +38,20 @@ export class MaintenanceRepository {
* `plugin_oauth_tokens`, `plugin_oauth_state`), called inside the erasure's * `plugin_oauth_tokens`, `plugin_oauth_state`), called inside the erasure's
* own `UnitOfWork.transactional(...)`. * own `UnitOfWork.transactional(...)`.
* *
* A table that does not exist (a slimmed-down test schema) is skipped; any * The three tables come with the migrations on both engines, so a failure
* other failure propagates. Each statement runs in its own savepoint, since * here is a real one and propagates, rolling the erasure back. (A table
* on Postgres a failed statement aborts the whole transaction it is in. * missing from a slimmed-down test schema used to be skipped in a savepoint
* `em.getTransactionContext()` hands the statement the open transaction's * of its own; there is no such schema any more, and the savepoint was a
* connection, which a bare `execute()` would wait on forever. * transaction outside `UnitOfWork`.) `getTransactionContext()` hands the
* statement the open transaction's connection, which a bare `execute()`
* would wait on forever.
*/ */
async deletePluginUserData(userId: number): Promise<void> { async deletePluginUserData(userId: number): Promise<void> {
this.validateRequestContext(); this.validateRequestContext();
for (const table of ['plugin_user_config', 'plugin_oauth_tokens', 'plugin_oauth_state']) { for (const table of ['plugin_user_config', 'plugin_oauth_tokens', 'plugin_oauth_state']) {
try { await this.em
await this.em.transactional((em) => .getConnection()
em .execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', this.em.getTransactionContext());
.getConnection()
.execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', em.getTransactionContext()),
);
} catch (err: unknown) {
if (!isMissingTableError(err)) throw err;
}
} }
} }
} }
@@ -90,12 +90,11 @@ export class PluginEntityMetadataRepository extends TrekRepository<PluginEntityM
* separately-awaited calls — two concurrent `meta.set` calls for two * separately-awaited calls — two concurrent `meta.set` calls for two
* DIFFERENT new keys on the same (plugin, entity) could both read * DIFFERENT new keys on the same (plugin, entity) could both read
* `count < max` before either's write landed, so a 100-key cap let a live * `count < max` before either's write landed, so a 100-key cap let a live
* burst land 130 rows. The three run in one transaction, which keeps them * burst land 130 rows. The three run in the caller's
* together on SQLite's single writer. On Postgres two transactions on two * `UnitOfWork.transactional` (`MetaRpc#set`), whose write lock puts two of
* pool connections both read the same count, and only the engine's write * them one after the other on either engine; a transaction of its own here
* lock, which `UnitOfWork` takes, puts them one after the other, so the * would not take that lock on Postgres, where two pool connections would
* caller runs this inside `UnitOfWork.transactional` (`MetaRpc#set`), and * both read the same count. Returns whether the
* the transaction here becomes a savepoint of that one. Returns whether the
* write happened; `false` means a NEW key was blocked by the cap (the * write happened; `false` means a NEW key was blocked by the cap (the
* caller maps that to `BadParams`, same message as before). * caller maps that to `BadParams`, same message as before).
*/ */
@@ -107,14 +106,12 @@ export class PluginEntityMetadataRepository extends TrekRepository<PluginEntityM
value: string, value: string,
maxKeys: number, maxKeys: number,
): Promise<boolean> { ): Promise<boolean> {
return this.getEntityManager().transactional(async () => { const exists = (await this.findValue(pluginId, entityType, entityId, key)) !== null;
const exists = (await this.findValue(pluginId, entityType, entityId, key)) !== null; if (!exists) {
if (!exists) { const n = await this.countForEntity(pluginId, entityType, entityId);
const n = await this.countForEntity(pluginId, entityType, entityId); if (n >= maxKeys) return false;
if (n >= maxKeys) return false; }
} await this.upsertValue(pluginId, entityType, entityId, key, value);
await this.upsertValue(pluginId, entityType, entityId, key, value); return true;
return true;
});
} }
} }
@@ -137,22 +137,20 @@ export class PluginScheduledTasksRepository extends TrekRepository<PluginSchedul
* `PluginEntityMetadataRepository.upsertValueCapped` (must-land 2): two * `PluginEntityMetadataRepository.upsertValueCapped` (must-land 2): two
* concurrent `scheduler.set` calls for two different new task names on the * concurrent `scheduler.set` calls for two different new task names on the
* same plugin could both read `count < max` before either's write landed * same plugin could both read `count < max` before either's write landed
* (live: 130 rows on a 100 cap). One transaction keeps them together on * (live: 130 rows on a 100 cap). The caller runs it inside
* SQLite; on Postgres the caller runs it inside `UnitOfWork.transactional` * `UnitOfWork.transactional` (`HostSurfaceRpc#schedulerSet`), whose write
* (`HostSurfaceRpc#schedulerSet`), whose write lock keeps them together * lock keeps them together on either engine, for the reason
* there, for the reason `upsertValueCapped`'s docstring gives. Returns * `upsertValueCapped`'s docstring gives. Returns
* whether the write happened; `false` means a NEW task name was blocked by * whether the write happened; `false` means a NEW task name was blocked by
* the cap. * the cap.
*/ */
async upsertTaskCapped(input: PluginScheduledTaskUpsertInput, maxTasks: number): Promise<boolean> { async upsertTaskCapped(input: PluginScheduledTaskUpsertInput, maxTasks: number): Promise<boolean> {
return this.getEntityManager().transactional(async () => { const exists = await this.existsForPluginAndName(input.plugin_id, input.name);
const exists = await this.existsForPluginAndName(input.plugin_id, input.name); if (!exists) {
if (!exists) { const n = await this.countForPlugin(input.plugin_id);
const n = await this.countForPlugin(input.plugin_id); if (n >= maxTasks) return false;
if (n >= maxTasks) return false; }
} await this.upsertTask(input);
await this.upsertTask(input); return true;
return true;
});
} }
} }
+6 -5
View File
@@ -1,4 +1,5 @@
import type { DatabaseEngine } from '../../db/engine'; import type { DatabaseEngine } from '../../db/engine';
import { writeTransaction } from '../../db/engine/write-transaction';
import { DATABASE_ENGINE } from './database-engine.module'; import { DATABASE_ENGINE } from './database-engine.module';
import { EntityManager } from '@mikro-orm/core'; import { EntityManager } from '@mikro-orm/core';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
@@ -23,7 +24,10 @@ import { Inject, Injectable } from '@nestjs/common';
* The services read `MAX(...) + 1` inside these transactions on the strength * The services read `MAX(...) + 1` inside these transactions on the strength
* of SQLite's single writer. On Postgres a pool of connections would let two * of SQLite's single writer. On Postgres a pool of connections would let two
* of them read the same MAX, so every transaction first takes the engine's * of them read the same MAX, so every transaction first takes the engine's
* write lock (`DatabaseEngine.serializeWrites`), which keeps that guarantee. * write lock (`DatabaseEngine.serializeWrites`, through `writeTransaction`),
* which keeps that guarantee. No other transaction is opened under src/
* (ESLint, `trek/transactions-through-unit-of-work`), so a check read
* inside one of these is never raced by a write outside the lock.
*/ */
@Injectable() @Injectable()
export class UnitOfWork { export class UnitOfWork {
@@ -33,9 +37,6 @@ export class UnitOfWork {
) {} ) {}
transactional<T>(fn: () => Promise<T>): Promise<T> { transactional<T>(fn: () => Promise<T>): Promise<T> {
return this.em.transactional(async (em) => { return writeTransaction(this.em, this.engine, () => fn());
await this.engine.serializeWrites(em);
return fn();
});
} }
} }
@@ -47,12 +47,19 @@ describe('MaintenanceRepository', () => {
} }
}); });
it('MAINTREPO-005: deletePluginUserData skips an absent table and rethrows any other failure', async () => { it('MAINTREPO-005: deletePluginUserData empties the three plugin tables and rethrows any failure, a missing table included', async () => {
const spy = vi.spyOn(t.em.getConnection(), 'execute'); const spy = vi.spyOn(t.em.getConnection(), 'execute');
try { try {
// The first statement is the user_config delete, so the one-shot rejection lands on it.
spy.mockRejectedValueOnce(new TableNotFoundException(new Error('no such table: plugin_user_config')));
await expect(maintenance.deletePluginUserData(1)).resolves.toBeUndefined(); await expect(maintenance.deletePluginUserData(1)).resolves.toBeUndefined();
expect(spy.mock.calls.map((call) => call[0]).filter((sql) => String(sql).startsWith('DELETE'))).toEqual([
'DELETE FROM plugin_user_config WHERE user_id = ?',
'DELETE FROM plugin_oauth_tokens WHERE user_id = ?',
'DELETE FROM plugin_oauth_state WHERE user_id = ?',
]);
// No savepoint swallows it: the erasure's transaction rolls back instead.
spy.mockRejectedValueOnce(new TableNotFoundException(new Error('no such table: plugin_user_config')));
await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('no such table');
spy.mockRejectedValueOnce(new Error('connection lost')); spy.mockRejectedValueOnce(new Error('connection lost'));
await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('connection lost'); await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('connection lost');
@@ -12,12 +12,14 @@
* (`existsById` ignored) from the review's mutation table, none of which any other * (`existsById` ignored) from the review's mutation table, none of which any other
* suite killed. * suite killed.
*/ */
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { Days } from '../../../src/db/entities/Days.entity'; import { Days } from '../../../src/db/entities/Days.entity';
import { PluginEntityMetadata } from '../../../src/db/entities/PluginEntityMetadata.entity'; import { PluginEntityMetadata } from '../../../src/db/entities/PluginEntityMetadata.entity';
import { PluginScheduledTasks } from '../../../src/db/entities/PluginScheduledTasks.entity'; import { PluginScheduledTasks } from '../../../src/db/entities/PluginScheduledTasks.entity';
import { TripMembers } from '../../../src/db/entities/TripMembers.entity'; import { TripMembers } from '../../../src/db/entities/TripMembers.entity';
import { Trips } from '../../../src/db/entities/Trips.entity'; import { Trips } from '../../../src/db/entities/Trips.entity';
import { Users } from '../../../src/db/entities/Users.entity'; import { Users } from '../../../src/db/entities/Users.entity';
import { UnitOfWork } from '../../../src/nest/database/unit-of-work';
import { createSnapshotTestDb } from '../../helpers/db-mock'; import { createSnapshotTestDb } from '../../helpers/db-mock';
import { countRows, findRow, findRows, insertRow, updateRows } from '../../helpers/factories/rows'; import { countRows, findRow, findRows, insertRow, updateRows } from '../../helpers/factories/rows';
import { resetTestDb } from '../../helpers/test-db'; import { resetTestDb } from '../../helpers/test-db';
@@ -218,13 +220,15 @@ describe('R3J-PARITY (Plan 3j Task 7 fix wave, must-land 4c)', () => {
it('must-land 2: 130 concurrent PluginEntityMetadata.upsertValueCapped calls for the SAME (plugin, entity), 130 DIFFERENT new keys, never exceed the 100 cap', async () => { it('must-land 2: 130 concurrent PluginEntityMetadata.upsertValueCapped calls for the SAME (plugin, entity), 130 DIFFERENT new keys, never exceed the 100 cap', async () => {
// The REAL repository method, raced through Promise.all — not a sequential loop — // The REAL repository method, raced through Promise.all — not a sequential loop —
// so the exists+count+write really overlaps (task-7-review.md: live, 130 rows landed // so the exists+count+write really overlaps (task-7-review.md: live, 130 rows landed
// on a 100 cap before this fix). Red without `upsertValueCapped`'s // on a 100 cap before this fix). Each call runs in a unit of work, as `MetaRpc#set`
// `getEntityManager().transactional` wrap: the old three-separately-awaited-calls // runs it: the three-separately-awaited-calls shape outside one let every racer's
// shape let every racer's stale `count < 100` read pass before any of their writes // stale `count < 100` read pass before any of their writes landed.
// landed.
const repo = t.repo(PluginEntityMetadata); const repo = t.repo(PluginEntityMetadata);
const uow = new UnitOfWork(t.em, new SqliteEngine());
const results = await Promise.all( const results = await Promise.all(
Array.from({ length: 130 }, (_, i) => repo.upsertValueCapped('capbomb', 'trip', 1, `k${i}`, String(i), 100)), Array.from({ length: 130 }, (_, i) =>
uow.transactional(() => repo.upsertValueCapped('capbomb', 'trip', 1, `k${i}`, String(i), 100)),
),
); );
expect(results.filter(Boolean).length).toBe(100); // exactly 100 writes accepted expect(results.filter(Boolean).length).toBe(100); // exactly 100 writes accepted
expect(await repo.countForEntity('capbomb', 'trip', 1)).toBe(100); // never more than 100 rows expect(await repo.countForEntity('capbomb', 'trip', 1)).toBe(100); // never more than 100 rows
@@ -232,13 +236,17 @@ describe('R3J-PARITY (Plan 3j Task 7 fix wave, must-land 4c)', () => {
}); });
it('must-land 3: 130 concurrent PluginScheduledTasks.upsertTaskCapped calls for the SAME plugin, 130 DIFFERENT new names, never exceed the 100 cap', async () => { it('must-land 3: 130 concurrent PluginScheduledTasks.upsertTaskCapped calls for the SAME plugin, 130 DIFFERENT new names, never exceed the 100 cap', async () => {
// In a unit of work, as `HostSurfaceRpc#schedulerSet` runs it.
const repo = t.repo(PluginScheduledTasks); const repo = t.repo(PluginScheduledTasks);
const uow = new UnitOfWork(t.em, new SqliteEngine());
const due = Date.now() + 60_000; const due = Date.now() + 60_000;
const results = await Promise.all( const results = await Promise.all(
Array.from({ length: 130 }, (_, i) => Array.from({ length: 130 }, (_, i) =>
repo.upsertTaskCapped( uow.transactional(() =>
{ plugin_id: 'schedbomb', name: `t${i}`, due_at: due, payload: '1', every_ms: null }, repo.upsertTaskCapped(
100, { plugin_id: 'schedbomb', name: `t${i}`, due_at: due, payload: '1', every_ms: null },
100,
),
), ),
), ),
); );