Skip to content

Commit 4c4fc03

Browse files
committed
docs(objectql): async after-hooks run after the commit, afterFind at dispatch, one statement of the rule
The hooks guide's unit-of-work section and its callout now describe blocking hooks, with a section for async ones. The spec's `async` TSDoc names the afterFind exception. The unreleased changeset of the previous fix no longer states the timing the new one replaces, and the new one states the two narrow deviations. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTEivi2wogPpeo3xd5
1 parent c122a90 commit 4c4fc03

4 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎.changeset/33651-async-hook-outside-door-unit.md‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ An object with an `async: true` `afterUpdate` hook worked until a second, blocki
1111

1212
The blocking hook (default `onError: 'abort'`) makes the write door open a unit of work, so that hook's abort can roll the write back. The async hook is dispatched inside that unit and starts at once, while the unit is still open, so its first writes joined it. Nothing waits for an async hook, so the unit committed under those writes. The same happened inside a unit a caller opened (`engine.transaction()`, `ctx.api.transaction(fn)`) whenever the async hook wrote before that unit settled.
1313

14-
An async after-hook now runs outside every unit of work open when it is dispatched. From the first write on, its writes are enrolled in no unit and run on their own connection, as they would with no unit open. What they see is not the same as with no unit open: there, the triggering write is already committed; here, it may not be yet (see the first bullet below). A unit the hook opens itself (`ctx.api.transaction(fn)`) is its own. This makes the existing changelog sentence about work a unit starts and does not await ("It now runs on its own connection, as it would outside any unit") true from the moment that work starts, not only after the unit has settled.
14+
An async after-hook now runs outside every unit of work open when it is dispatched. From the first write on, its writes are enrolled in no unit and run on their own connection, as they would with no unit open. A unit the hook opens itself (`ctx.api.transaction(fn)`) is its own. When the hook starts, and whether it runs at all when that unit rolls back, is stated in one place, the entry for the follow-up fix: an async after-hook fired inside a unit of work runs once that unit commits, and not at all when it rolls back. This makes the existing changelog sentence about work a unit starts and does not await ("It now runs on its own connection, as it would outside any unit") true from the moment that work starts, not only after the unit has settled.
1515

1616
What changes for a hook author:
1717

18-
- An async after-hook runs in the background but is not deferred until the triggering write commits. It can start before that write's unit commits, so it may see the state from before that write, and it is not cancelled if that unit rolls back. On a database with more than one connection (Postgres, MySQL, Mongo), its `ctx.api` reads and writes run on their own connection and do not see the triggering write until its unit commits: an async `afterUpdate` that re-reads its record can read the old values, and an async `afterInsert` that updates the new row or inserts a child that references it can find no row. Before this change, those early statements joined the unit, saw the write, and failed or rolled back with it. An async hook that must see the triggering write should be a blocking hook instead.
1918
- An async hook's writes are stored whatever blocking hooks share its event.
20-
- An async hook's writes no longer roll back with the write that triggered it. Before, they rolled back only when they happened to run before that unit settled. An async hook that must undo its effect when the triggering write fails should be a blocking hook instead.
19+
- An async hook's writes no longer roll back with the write that triggered it. Before, they rolled back only when they happened to run before that unit settled.
2120
- On a database with a single connection (SQLite), an async hook's write waits until the triggering write's unit releases the connection. Nothing waits for the async hook, so the triggering write is not delayed.
2221

2322
Blocking hooks are unchanged: their `ctx.api` writes join the unit and roll back with it. An async hook with no unit open is unchanged too.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ Metadata-protocol: semantic
1010

1111
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.
1212

13-
This changes what the changelog entry for the previous fix (#33703) told hook authors. That entry said an async after-hook "is not deferred until the triggering write commits", "is not cancelled if that unit rolls back", and that a hook that must see the triggering write "should be a blocking hook instead". Now:
13+
This is the one statement of when an async after-hook runs inside a unit of work. The previous fix (#33703) moved its writes outside the unit; its entry in these notes leaves the timing to this one. Now:
1414

1515
- An async after-hook fired inside a unit of work starts after the outermost unit commits. Its `ctx.api` reads see the committed write, on every database. A nested `transaction()` joins the outer unit, so its return is no commit: the outer unit's outcome decides.
1616
- When that unit rolls back, the async hook does not run at all. It writes nothing and calls nothing, so an external effect it makes (a webhook, an email) is never announced for a write that was undone.
1717
- The units this covers are every one the engine opens: a caller's `engine.transaction()`, `ctx.api.transaction(fn)` (in a sandboxed hook body too), an atomic batch or cascade, and the unit the write door opens for a blocking `after*` hook that can abort. A blocking hook beside the async one on the same event still runs inside that unit, before the commit.
1818
- 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.
19+
- This is for the write events (`afterInsert`, `afterUpdate`, `afterDelete`). A rollback does not undo a read, so an async `afterFind` hook starts when the read is dispatched, outside the unit, as before, whatever the unit does next.
20+
- All the async hooks a unit held start together, in the order they were dispatched, once it commits. A bulk write with a per-row async hook holds one per row until then.
1921

2022
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.
2123

24+
Known deviations, both narrow: a write that is stored while its unit is later undone still loses its async hook with that unit, in two cases. (a) A write from work the transaction callback started and did not await, made after the callback returned and before a commit that then fails. (b) A write to an append-only system-ledger object routed to another datasource, which `transaction()` runs outside the unit and which survives its rollback: its async hook is held by the unit and dropped with it.
25+
2226
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.

‎content/docs/automation/hooks.mdx‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,10 @@ An `after*` hook does **not** mean "the write happened". It means **the write
293293
has been requested and will happen unless this unit of work is undone**.
294294
`afterInsert`, `afterUpdate` and `afterDelete` are dispatched *before* the
295295
enclosing transaction commits, so a later refusal in the same unit can roll the
296-
row back after your handler has already run.
296+
row back after your handler has already run. That is true of a **blocking**
297+
hook, the default. An `async: true` hook is dispatched there too, but does not
298+
run there: see [`async: true` after hooks run after the
299+
commit](#async-true-after-hooks-run-after-the-commit).
297300

298301
Four ordinary operations put a write inside such a unit:
299302

@@ -304,7 +307,7 @@ Four ordinary operations put a write inside such a unit:
304307
| `batchData` / `deleteManyData` with `atomic: true` | Every member's `after*` — the batch aborts and rolls back on the first failure |
305308
| Any write you wrapped yourself in `ctx.api.transaction(...)` or `engine.transaction(...)` | Everything in the callback |
306309

307-
What this means when you write a handler:
310+
What this means when you write a blocking handler:
308311

309312
- **Effects that go back through the engine are safe.** Writes made with
310313
`ctx.api.object('x')` join the same transaction and roll back with
@@ -321,12 +324,36 @@ Before hooks carry no such caveat: they run before the write is issued, and
321324
throwing from one refuses the operation outright.
322325

323326
<Callout type="info">
324-
This is a deliberate, ruled semantics ([#7477](https://github.com/objectstack-ai/objectstack/issues/7477)),
325-
not an implementation detail awaiting a fix. Deferring `after*` to commit time
326-
would move a handler's own `ctx.api` writes *outside* the transaction the write
327-
ran in, which is a worse guarantee than the one documented here.
327+
For a blocking hook this is a deliberate, ruled semantics ([#7477](https://github.com/objectstack-ai/objectstack/issues/7477)),
328+
not an implementation detail awaiting a fix. Deferring a blocking `after*` to
329+
commit time would move its own `ctx.api` writes *outside* the transaction the
330+
write ran in, which is a worse guarantee than the one documented here. An
331+
`async: true` hook has no such writes to keep: nothing awaits it, so its writes
332+
never joined the unit, and it is the one that waits for the commit (below).
328333
</Callout>
329334

335+
### `async: true` after hooks run after the commit
336+
337+
An `async: true` hook on `afterInsert`, `afterUpdate` or `afterDelete` is not
338+
awaited: the write returns without waiting for it, and the hook can neither
339+
abort the write nor have its writes rolled back with it. Inside a unit of work
340+
(any row of the table above) it therefore waits for that unit's outcome:
341+
342+
- **The unit commits:** the hook starts once the **outermost** unit has
343+
committed, so its `ctx.api` reads see the triggering write. A nested
344+
`transaction()` joins the outer unit, so its return is no commit.
345+
- **The unit rolls back:** the hook **never runs**. It does not announce a
346+
write that did not survive, so an `async` hook is the one to give an effect
347+
outside the engine. To react to a write that was refused, use a blocking
348+
hook, or record the refusal where the write is made.
349+
- **Outside any unit**, or on a driver with no transactions: the hook starts
350+
at once, as the write is dispatched.
351+
352+
Either way its own `ctx.api` writes run outside the triggering unit, on their
353+
own; a `ctx.api.transaction(...)` it opens is its own unit. An `async`
354+
`afterFind` hook does not wait: a rollback does not undo a read, so it starts
355+
when the read is dispatched, outside any unit, whatever the unit does next.
356+
330357
## Refusing in the user's language — `refusals`
331358

332359
A hook that says "no" to a write throws. A sentence written into the throw
@@ -513,9 +540,10 @@ ctx = {
513540
- Trigger unbounded cascades of writes
514541
- Perform heavy/long-running work inline in a hook
515542
- Mutate `ctx.result` in before hooks (it is only populated for after hooks)
516-
- Treat an `after*` hook as proof the write committed — it fires inside the
517-
unit of work, so an un-retractable external effect there can outlive a
518-
rollback (see [above](#after-hooks-run-inside-the-unit-of-work))
543+
- Treat a blocking `after*` hook as proof the write committed — it fires inside
544+
the unit of work, so an un-retractable external effect there can outlive a
545+
rollback (see [above](#after-hooks-run-inside-the-unit-of-work)); an
546+
`async: true` one runs only after the commit
519547

520548
## Related business logic
521549

‎packages/spec/src/data/hook.zod.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,11 @@ export const HookSchema = lazySchema(() => strictObject(
735735
* writes run outside the unit, on their own connection. Outside any unit it
736736
* starts at once, after the write. Nothing waits for it, and its errors
737737
* never reach the caller.
738+
*
739+
* That waiting is for a WRITE event's hook (`afterInsert` / `afterUpdate` /
740+
* `afterDelete`). A rollback does not undo a read, so an async `afterFind`
741+
* hook starts when the read is dispatched, outside the unit, whatever the
742+
* unit it was read in does next.
738743
*/
739744
async: z.boolean().default(false).describe('Run specifically as fire-and-forget'),
740745

0 commit comments

Comments
 (0)