Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

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

Mauricec1bbe27ac612
diff --git a/server/scripts/tx-writes.mjs b/server/scripts/tx-writes.mjs
index 6fa61f65a..2df7e032a 100644
--- a/server/scripts/tx-writes.mjs
+++ b/server/scripts/tx-writes.mjs
@@ -19,8 +19,8 @@
* A method's "write units" are then counted over its body:
* - a call to a write, on a constructor-injected field (`this.trips.x()`)
* or on its own class (`this.x()`), is one unit;
- * - a `.transactional(...)` call that holds any write is one unit, however
- * many writes it holds;
+ * - a `.transactional(...)` or `writeTransaction(...)` call that holds any
+ * write is one unit, however many writes it holds;
* - 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);
* - 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)
? count(cls, callee.expression, 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;
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);
diff --git a/server/src/db/engine/write-transaction.ts b/server/src/db/engine/write-transaction.ts
new file mode 100644
index 000000000..22a6cd50b
--- /dev/null
+++ b/server/src/db/engine/write-transaction.ts
@@ -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);
+ });
+}
diff --git a/server/src/db/repositories/DemoRepository.ts b/server/src/db/repositories/DemoRepository.ts
index f8b89576e..5891f774f 100644
--- a/server/src/db/repositories/DemoRepository.ts
+++ b/server/src/db/repositories/DemoRepository.ts
@@ -1,4 +1,6 @@
import { currentTimestampKysely } from '../dialect/kysely-functions';
+import { resolveEngine } from '../engine';
+import { writeTransaction } from '../engine/write-transaction';
import { Addons } from '../entities/Addons.entity';
import { AppSettings } from '../entities/AppSettings.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
- * 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
* `setEnabled` issues for one row) and one upsert for all the switches
* (`AppSettingsRepository#setValue`'s), however long the lists are. A named
@@ -430,7 +435,7 @@ export class DemoRepository {
async applyAddonStates(states: DemoAddonStates): Promise<void> {
this.validateRequestContext();
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);
await addons.nativeUpdate({ id: { $in: [...states.addons.on] } }, { enabled: true });
await addons.nativeUpdate({ id: { $in: [...states.addons.off] } }, { enabled: false });
diff --git a/server/src/db/repositories/MaintenanceRepository.ts b/server/src/db/repositories/MaintenanceRepository.ts
index f492cfb88..311089770 100644
--- a/server/src/db/repositories/MaintenanceRepository.ts
+++ b/server/src/db/repositories/MaintenanceRepository.ts
@@ -1,4 +1,3 @@
-import { isMissingTableError } from '../dialect/driver-errors';
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
* own `UnitOfWork.transactional(...)`.
*
- * A table that does not exist (a slimmed-down test schema) is skipped; any
- * other failure propagates. Each statement runs in its own savepoint, since
- * on Postgres a failed statement aborts the whole transaction it is in.
- * `em.getTransactionContext()` hands the statement the open transaction's
- * connection, which a bare `execute()` would wait on forever.
+ * The three tables come with the migrations on both engines, so a failure
+ * here is a real one and propagates, rolling the erasure back. (A table
+ * missing from a slimmed-down test schema used to be skipped in a savepoint
+ * of its own; there is no such schema any more, and the savepoint was a
+ * 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> {
this.validateRequestContext();
for (const table of ['plugin_user_config', 'plugin_oauth_tokens', 'plugin_oauth_state']) {
- try {
- await this.em.transactional((em) =>
- em
- .getConnection()
- .execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', em.getTransactionContext()),
- );
- } catch (err: unknown) {
- if (!isMissingTableError(err)) throw err;
- }
+ await this.em
+ .getConnection()
+ .execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', this.em.getTransactionContext());
}
}
}
diff --git a/server/src/db/repositories/PluginEntityMetadata.repository.ts b/server/src/db/repositories/PluginEntityMetadata.repository.ts
index 63e487614..63cc05eb4 100644
--- a/server/src/db/repositories/PluginEntityMetadata.repository.ts
+++ b/server/src/db/repositories/PluginEntityMetadata.repository.ts
@@ -90,12 +90,11 @@ export class PluginEntityMetadataRepository extends TrekRepository<PluginEntityM
* separately-awaited calls — two concurrent `meta.set` calls for two
* 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
- * burst land 130 rows. The three run in one transaction, which keeps them
- * together on SQLite's single writer. On Postgres two transactions on two
- * pool connections both read the same count, and only the engine's write
- * lock, which `UnitOfWork` takes, puts them one after the other, so the
- * caller runs this inside `UnitOfWork.transactional` (`MetaRpc#set`), and
- * the transaction here becomes a savepoint of that one. Returns whether the
+ * burst land 130 rows. The three run in the caller's
+ * `UnitOfWork.transactional` (`MetaRpc#set`), whose write lock puts two of
+ * them one after the other on either engine; a transaction of its own here
+ * would not take that lock on Postgres, where two pool connections would
+ * both read the same count. Returns whether the
* write happened; `false` means a NEW key was blocked by the cap (the
* caller maps that to `BadParams`, same message as before).
*/
@@ -107,14 +106,12 @@ export class PluginEntityMetadataRepository extends TrekRepository<PluginEntityM
value: string,
maxKeys: number,
): Promise<boolean> {
- return this.getEntityManager().transactional(async () => {
- const exists = (await this.findValue(pluginId, entityType, entityId, key)) !== null;
- if (!exists) {
- const n = await this.countForEntity(pluginId, entityType, entityId);
- if (n >= maxKeys) return false;
- }
- await this.upsertValue(pluginId, entityType, entityId, key, value);
- return true;
- });
+ const exists = (await this.findValue(pluginId, entityType, entityId, key)) !== null;
+ if (!exists) {
+ const n = await this.countForEntity(pluginId, entityType, entityId);
+ if (n >= maxKeys) return false;
+ }
+ await this.upsertValue(pluginId, entityType, entityId, key, value);
+ return true;
}
}
diff --git a/server/src/db/repositories/PluginScheduledTasks.repository.ts b/server/src/db/repositories/PluginScheduledTasks.repository.ts
index f0c0acc33..e765640a6 100644
--- a/server/src/db/repositories/PluginScheduledTasks.repository.ts
+++ b/server/src/db/repositories/PluginScheduledTasks.repository.ts
@@ -137,22 +137,20 @@ export class PluginScheduledTasksRepository extends TrekRepository<PluginSchedul
* `PluginEntityMetadataRepository.upsertValueCapped` (must-land 2): two
* concurrent `scheduler.set` calls for two different new task names on the
* 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
- * SQLite; on Postgres the caller runs it inside `UnitOfWork.transactional`
- * (`HostSurfaceRpc#schedulerSet`), whose write lock keeps them together
- * there, for the reason `upsertValueCapped`'s docstring gives. Returns
+ * (live: 130 rows on a 100 cap). The caller runs it inside
+ * `UnitOfWork.transactional` (`HostSurfaceRpc#schedulerSet`), whose write
+ * lock keeps them together on either engine, for the reason
+ * `upsertValueCapped`'s docstring gives. Returns
* whether the write happened; `false` means a NEW task name was blocked by
* the cap.
*/
async upsertTaskCapped(input: PluginScheduledTaskUpsertInput, maxTasks: number): Promise<boolean> {
- return this.getEntityManager().transactional(async () => {
- const exists = await this.existsForPluginAndName(input.plugin_id, input.name);
- if (!exists) {
- const n = await this.countForPlugin(input.plugin_id);
- if (n >= maxTasks) return false;
- }
- await this.upsertTask(input);
- return true;
- });
+ const exists = await this.existsForPluginAndName(input.plugin_id, input.name);
+ if (!exists) {
+ const n = await this.countForPlugin(input.plugin_id);
+ if (n >= maxTasks) return false;
+ }
+ await this.upsertTask(input);
+ return true;
}
}
diff --git a/server/src/nest/database/unit-of-work.ts b/server/src/nest/database/unit-of-work.ts
index 1e92efece..d8fe60ae6 100644
--- a/server/src/nest/database/unit-of-work.ts
+++ b/server/src/nest/database/unit-of-work.ts
@@ -1,4 +1,5 @@
import type { DatabaseEngine } from '../../db/engine';
+import { writeTransaction } from '../../db/engine/write-transaction';
import { DATABASE_ENGINE } from './database-engine.module';
import { EntityManager } from '@mikro-orm/core';
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
* 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
- * 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()
export class UnitOfWork {
@@ -33,9 +37,6 @@ export class UnitOfWork {
) {}
transactional<T>(fn: () => Promise<T>): Promise<T> {
- return this.em.transactional(async (em) => {
- await this.engine.serializeWrites(em);
- return fn();
- });
+ return writeTransaction(this.em, this.engine, () => fn());
}
}
diff --git a/server/tests/unit/db/repositories/MaintenanceRepository.test.ts b/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
index 414b28e96..55e09694d 100644
--- a/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
+++ b/server/tests/unit/db/repositories/MaintenanceRepository.test.ts
@@ -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');
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();
+ 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'));
await expect(maintenance.deletePluginUserData(1)).rejects.toThrow('connection lost');
diff --git a/server/tests/unit/plugins/plugin-repository-sql-parity.test.ts b/server/tests/unit/plugins/plugin-repository-sql-parity.test.ts
index 029b7b2cc..83df2f1cb 100644
--- a/server/tests/unit/plugins/plugin-repository-sql-parity.test.ts
+++ b/server/tests/unit/plugins/plugin-repository-sql-parity.test.ts
@@ -12,12 +12,14 @@
* (`existsById` ignored) from the review's mutation table, none of which any other
* suite killed.
*/
+import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
import { Days } from '../../../src/db/entities/Days.entity';
import { PluginEntityMetadata } from '../../../src/db/entities/PluginEntityMetadata.entity';
import { PluginScheduledTasks } from '../../../src/db/entities/PluginScheduledTasks.entity';
import { TripMembers } from '../../../src/db/entities/TripMembers.entity';
import { Trips } from '../../../src/db/entities/Trips.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 { countRows, findRow, findRows, insertRow, updateRows } from '../../helpers/factories/rows';
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 () => {
// 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
- // on a 100 cap before this fix). Red without `upsertValueCapped`'s
- // `getEntityManager().transactional` wrap: the old three-separately-awaited-calls
- // shape let every racer's stale `count < 100` read pass before any of their writes
- // landed.
+ // on a 100 cap before this fix). Each call runs in a unit of work, as `MetaRpc#set`
+ // runs it: the three-separately-awaited-calls shape outside one let every racer's
+ // stale `count < 100` read pass before any of their writes landed.
const repo = t.repo(PluginEntityMetadata);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
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(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 () => {
+ // In a unit of work, as `HostSurfaceRpc#schedulerSet` runs it.
const repo = t.repo(PluginScheduledTasks);
+ const uow = new UnitOfWork(t.em, new SqliteEngine());
const due = Date.now() + 60_000;
const results = await Promise.all(
Array.from({ length: 130 }, (_, i) =>
- repo.upsertTaskCapped(
- { plugin_id: 'schedbomb', name: `t${i}`, due_at: due, payload: '1', every_ms: null },
- 100,
+ uow.transactional(() =>
+ repo.upsertTaskCapped(
+ { plugin_id: 'schedbomb', name: `t${i}`, due_at: due, payload: '1', every_ms: null },
+ 100,
+ ),
),
),
);