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 54b843ae29
commit 60642cc18b
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 };
}