Skip to content

Commit b65177e

Browse files
committed
fix(objectql): open the update's after-abort unit around the engine body, not the door
The unit enclosed the middleware chain, so on a single-connection store the loser of a concurrent claim ran its authorization gate after the winner's commit and was refused as not found or forbidden instead of 409 CONCURRENT_UPDATE (ADR-0142 D4). Claude-Session: https://claude.ai/code/session_01JQwxbXGvw43spR79ztEvWt Co-authored-by: Claude <noreply@anthropic.com>
1 parent 510e96a commit b65177e

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

‎packages/objectql/src/engine-update.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,20 @@ export class UpdateFacet {
158158
object = this.resolveObjectName(object);
159159
// [#22782, #34412] An aborting `afterUpdate`, or a refusal after a `beforeUpdate` hook's
160160
// `ctx.api` write, rolls this write back with it — see `opensAfterAbortUnit`.
161-
if (this.opensAfterAbortUnit('afterUpdate', object, options?.context)) {
162-
return this.inAfterAbortUnit(() => this.update(object, data, options));
163-
}
161+
//
162+
// [#35142] The unit opens around the ENGINE BODY (the executor below), not
163+
// around the door: the middleware chain runs first, outside it, as it does
164+
// for a write that opens no unit. Opened around the door, the unit enclosed
165+
// the authorization gate's reads too, and on a single-connection store
166+
// (SQLite) a second writer's whole write then waited for the first one's
167+
// commit: the loser of a claim race (ADR-0142 D4) read the claimed row in
168+
// its gate and was refused there as not found or forbidden, never reaching
169+
// the conditional write whose 0-row answer is `ConcurrentUpdateError`.
170+
// Everything the unit exists for — the `before*` hooks' `ctx.api` writes,
171+
// the row, the `after*` chain — runs inside the executor.
172+
const ownUnit = this.opensAfterAbortUnit('afterUpdate', object, options?.context);
173+
const inOwnUnit = <T>(body: () => Promise<T>): (() => Promise<T>) =>
174+
ownUnit ? () => this.inAfterAbortUnit(body) : body;
164175
this.logger.debug('Update operation starting', { object });
165176
this.assertWriteAllowed(object, 'update');
166177
// [#8844] See the insert face — an organization row that changes identity
@@ -461,7 +472,7 @@ export class UpdateFacet {
461472
throw new ReadonlyFieldRejectedError(object, fields, strictDrops);
462473
};
463474

464-
await this.executeWithMiddleware(opCtx, async () => {
475+
await this.executeWithMiddleware(opCtx, inOwnUnit(async () => {
465476
// [ADR-0139 D1] A write's predicate never carries a `ReadableIds` leaf.
466477
// The door refused the caller's; this refuses one the middleware chain
467478
// wrote — here, at the executor's entry, because the prior-row reads
@@ -2374,7 +2385,7 @@ export class UpdateFacet {
23742385
await this.releaseSecretHandles(object, writtenSecrets, opCtx.context, 'refused');
23752386
throw e;
23762387
}
2377-
});
2388+
}));
23782389

23792390
return opCtx.result;
23802391
}

0 commit comments

Comments
 (0)