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 () => {