Skip to content

Commit 709b3ae

Browse files
committed
docs(objectql): declare the async after-hook commit dispatch semantic, and correct the abort-rollback twin's header
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5
1 parent a777ec0 commit 709b3ae

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎.changeset/33760-async-hook-on-commit.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
---
2-
'@objectstack/objectql': patch
2+
'@objectstack/objectql': minor
3+
'@objectstack/spec': patch
34
---
45

56
fix(objectql): an `async: true` after-hook fired inside a unit of work runs once that unit commits, and not at all when it rolls back
67

7-
Clause-②: yes
8-
Metadata-protocol: none
8+
Clause-②: no
9+
Metadata-protocol: semantic
910

1011
An `async: true` after-hook fired inside a unit of work started at once, while the unit was still open. Two things followed. On a database with more than one connection (Postgres, MySQL, Mongo), its `ctx.api` reads and writes ran on their own connection before the triggering write was committed, so an async `afterUpdate` that re-read its record could read the old values, and an async `afterInsert` that updated the new row or inserted a child referencing it could find no row. And when the unit rolled back, the hook had already run: its writes landed, referencing a row that was never stored. Measured on Postgres 16 and on SQLite through knex, for a caller's `engine.transaction()`, a `ctx.api.transaction(fn)`, a nested unit, and the unit the write door opens for a blocking `after*` hook beside the async one.
1112

@@ -17,3 +18,5 @@ This changes what the changelog entry for the previous fix (#33703) told hook au
1718
- Outside any unit an async hook starts at once, after the write, as before. Its writes still run outside every unit, on their own connection, and nothing waits for it.
1819

1920
An async hook whose job was to react to a write that failed (to record a refused attempt, say) no longer runs for it. Use a blocking hook, or react to the caller's error, for that.
21+
22+
The `async` key's documentation in `@objectstack/spec` (`HookSchema.async`, and the `HookEvent` note on `ctx.api` writes rolling back with the unit, which now names blocking hooks only) says the same.

‎packages/qa/dogfood/test/hook-after-abort-rollback.dogfood.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
// hook answers REST's sanitised `500 INTERNAL_ERROR` (its terminal branch) on
1616
// both sides of the fix; what this file pins is what is STORED.
1717
//
18-
// One more case the unit made necessary: an `async: true` hook is started
19-
// inside the unit and writes after it commits. It used to inherit the finished
20-
// handle and lose its write ("Transaction query already complete", measured).
18+
// One more case the unit made necessary: an `async: true` hook fired inside
19+
// the unit. It used to start inside the unit, inherit the finished handle and
20+
// lose its write ("Transaction query already complete", measured). Since
21+
// #33760 it starts only after the unit commits, and not at all on a rollback;
22+
// the positive control below still reads its late note.
2123

2224
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
2325
import { bootStack, type VerifyStack } from '@objectstack/verify';

0 commit comments

Comments
 (0)