fix(plugins): hold the write lock for the plugin cap checks on Postgres

meta.set and scheduler.set checked their caps in a transaction of the
repository's own, which keeps check and write together on SQLite's single
writer but not on Postgres, where two pool connections both read the same
count. Both RPCs now run the capped write inside UnitOfWork.transactional,
which takes the engine's write lock; the repository's transaction becomes a
savepoint of it.
This commit is contained in:
Maurice
2026-11-17 12:07:57 +01:00
parent ec733fc927
commit 7bde1ec9d4
7 changed files with 51 additions and 24 deletions
@@ -90,16 +90,14 @@ 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. Wrapped in `this.getEntityManager().transactional`
* — the same MikroORM API `UnitOfWork.transactional` itself calls (see
* that class's own docstring on why its default NESTED/savepoint
* propagation is safe even when a caller is already inside a transaction)
* — so `MetaRpc` needs no new constructor dependency: this fix wave found
* `tests/helpers/plugin-host.ts` (which hand-constructs `MetaRpc`) mid-edit
* by a concurrent Plan 4 task and could not touch its construction site,
* so the fix stays entirely inside the repository it already owns.
* 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).
* 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
* write happened; `false` means a NEW key was blocked by the cap (the
* caller maps that to `BadParams`, same message as before).
*/
async upsertValueCapped(
pluginId: string,
@@ -137,11 +137,12 @@ 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). `this.getEntityManager().transactional` —
* no new constructor dependency on `HostSurfaceRpc`, for the same
* `tests/helpers/plugin-host.ts` (dirty, mid-edit by a concurrent Plan 4
* task) reason `upsertValueCapped`'s docstring gives. Returns whether the
* write happened; `false` means a NEW task name was blocked by the cap.
* (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
* 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 () => {
@@ -11,6 +11,7 @@ import { BadParams, ForbiddenResource } from '../../../../nest-rpc/rpc-errors';
import { PluginController, PluginMethod } from '../../../../nest-rpc/rpc-kit/decorators';
import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types';
import { asPayload, num, str } from '../../../../nest-rpc/rpc-params';
import { UnitOfWork } from '../../../database/unit-of-work';
import { UnreadableLlmResponse } from '../../../llm-parse/clients/openai-compatible.client';
import { createLlmClient } from '../../../llm-parse/llm-client.factory';
import type { ResolvedLlmConfig } from '../../../llm-parse/llm-config';
@@ -58,6 +59,7 @@ export class HostSurfaceRpc {
@InjectRepository(Trips) private readonly trips: TripsRepository,
// HR5–HR8 (Plan 3j Task 5) — a plugin's own scheduler.set/scheduler.cancel RPCs.
@InjectRepository(PluginScheduledTasks) private readonly scheduledTasks: PluginScheduledTasksRepository,
private readonly uow: UnitOfWork,
) {}
@PluginMethod('users.getById', { permission: 'db:read:users' })
@@ -212,11 +214,16 @@ export class HostSurfaceRpc {
throw new BadParams(`scheduler payload too large (max ${SCHED_PAYLOAD_MAX} bytes)`);
// HR5/HR6/HR7 — Plan 3j Task 7 fix (must-land 3): one atomic call, not
// three separately-awaited ones (task-7-review.md's concurrent-cap-bypass).
// Upsert by (plugin, name): re-scheduling the same name replaces it.
const written = await this.scheduledTasks.upsertTaskCapped(
{ plugin_id: ctx.pluginId, name, due_at: Math.max(dueAt, Date.now()), payload: json, every_ms: everyMs ?? null },
SCHED_MAX,
);
// Upsert by (plugin, name): re-scheduling the same name replaces it. In a
// unit of work, whose write lock keeps the cap check atomic on Postgres too.
const task = {
plugin_id: ctx.pluginId,
name,
due_at: Math.max(dueAt, Date.now()),
payload: json,
every_ms: everyMs ?? null,
};
const written = await this.uow.transactional(() => this.scheduledTasks.upsertTaskCapped(task, SCHED_MAX));
if (!written) throw new BadParams(`too many scheduled tasks (max ${SCHED_MAX})`);
return { scheduled: true };
}
+7 -2
View File
@@ -15,6 +15,7 @@ import { BadParams, ForbiddenResource } from '../../../../nest-rpc/rpc-errors';
import { PluginController, PluginMethod } from '../../../../nest-rpc/rpc-kit/decorators';
import type { PluginRpcContext } from '../../../../nest-rpc/rpc-kit/types';
import { num, str } from '../../../../nest-rpc/rpc-params';
import { UnitOfWork } from '../../../database/unit-of-work';
import { InjectRepository } from '@mikro-orm/nestjs';
/** Core entities a plugin may attach its own db:meta to. */
@@ -57,6 +58,7 @@ export class MetaRpc {
@InjectRepository(Days) private readonly days: DaysRepository,
@InjectRepository(Reservations) private readonly reservations: ReservationsRepository,
@InjectRepository(DayAccommodations) private readonly dayAccommodations: DayAccommodationsRepository,
private readonly uow: UnitOfWork,
) {}
@PluginMethod('meta.get', { permission: 'db:meta' })
@@ -79,8 +81,11 @@ export class MetaRpc {
const json = JSON.stringify(params.value ?? null);
if (json.length > META_VALUE_MAX) throw new BadParams(`metadata value too large (>${META_VALUE_MAX} bytes)`);
// MR2/MR3/MR4 — Plan 3j Task 7 fix (must-land 2): one atomic call, not
// three separately-awaited ones (task-7-review.md's concurrent-cap-bypass).
const written = await this.meta.upsertValueCapped(ctx.pluginId, entityType, entityId, key, json, META_KEYS_MAX);
// three separately-awaited ones (task-7-review.md's concurrent-cap-bypass),
// in a unit of work, whose write lock keeps it atomic on Postgres's pool too.
const written = await this.uow.transactional(() =>
this.meta.upsertValueCapped(ctx.pluginId, entityType, entityId, key, json, META_KEYS_MAX),
);
if (!written) throw new BadParams(`too many metadata keys on this ${entityType} (max ${META_KEYS_MAX})`);
return { key, value: params.value ?? null };
}
+2
View File
@@ -641,6 +641,7 @@ export async function createPluginRpcHostParts(
await createTestDaysRepo(db),
await createTestReservationsRepo(db),
await createTestDayAccommodationsRepo(db),
await createTestUnitOfWork(db),
),
// Plan 3j Task 5 — HostSurfaceRpc's own HR1/HR5–HR9 conversion: the plugin-visible
// user row, the bilateral trip-sharing gate, and its own scheduler.set/cancel.
@@ -655,6 +656,7 @@ export async function createPluginRpcHostParts(
usersRepo,
await createTestTripsRepo(db),
pluginOrm.repo(PluginScheduledTasks),
await createTestUnitOfWork(db),
),
new PluginHooks(undefined as never),
];
+11 -1
View File
@@ -94,7 +94,16 @@ export function allRpcControllers(): object[] {
// Plan 3j Task 5 — MetaRpc/HostSurfaceRpc's own repository conversions (MR#/HR#).
// Plan 4 Task 2 — both classes' own DatabaseService param is gone (canAccessTrip
// now reads through the TripsRepository they already inject), one fewer anyService().
new MetaRpc(anyService(), anyService(), anyService(), anyService(), anyService(), anyService(), anyService()),
new MetaRpc(
anyService(),
anyService(),
anyService(),
anyService(),
anyService(),
anyService(),
anyService(),
anyService(),
),
new HostSurfaceRpc(
anyService(),
anyService(),
@@ -105,6 +114,7 @@ export function allRpcControllers(): object[] {
anyService(),
anyService(),
anyService(),
anyService(),
),
// Owns no wire method, but declares all 18 hook contracts, which requireTotalCoverage checks too.
new PluginHooks(anyService()),
+5 -1
View File
@@ -22,6 +22,7 @@ import { PluginGuards } from '../../../src/nest-rpc/plugin-guards.service';
import { PluginRpcRegistryService } from '../../../src/nest-rpc/rpc-kit/registry.service';
import { createTestPluginRegistry } from '../../../src/nest-rpc/rpc-kit/testing';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import type { UnitOfWork } from '../../../src/nest/database/unit-of-work';
import { UnreadableLlmResponse } from '../../../src/nest/llm-parse/clients/openai-compatible.client';
import type { LlmConfigResolver } from '../../../src/nest/llm-parse/llm-config.resolver';
import type { NotificationsService } from '../../../src/nest/notifications/notifications.service';
@@ -397,9 +398,11 @@ const usersRepo = {
// params are gone: canAccessTrip now reads through the TripsRepository each
// already injects (`tripsRepo` above, findAccessible-only).
const guards = new PluginGuards(tripsRepo, permissions, addons, usersRepo);
/** The capped writes run in a unit of work; the stand-in repositories here are atomic on their own. */
const uow = { transactional: <T>(fn: () => Promise<T>) => fn() } as unknown as UnitOfWork;
const registry = createTestPluginRegistry([
new DbRpc(userSettings),
new MetaRpc(guards, metaRepo, tripsRepo, placesRepo, daysRepo, reservationsRepo, dayAccommodationsRepo),
new MetaRpc(guards, metaRepo, tripsRepo, placesRepo, daysRepo, reservationsRepo, dayAccommodationsRepo, uow),
new HostSurfaceRpc(
realtime,
notifications,
@@ -410,6 +413,7 @@ const registry = createTestPluginRegistry([
usersRepo,
tripsRepo,
scheduledTasksRepo,
uow,
),
]);
const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService);