@@ -364,9 +364,15 @@ async function makeCommitEngine() {
364364 let dropped ! : ( hook : string ) => void ;
365365 const drops : string [ ] = [ ] ;
366366 const quiet = ( ) => { } ;
367+ // A deferred run's failure reaches the wrapper's error line (fire-and-forget).
368+ let errored ! : ( hook : string ) => void ;
369+ const erroredRun = new Promise < string > ( ( r ) => { errored = r ; } ) ;
367370 const logger = {
368371 debug : ( msg : string , meta ?: any ) => { if ( / r o l l e d b a c k / . test ( msg ) ) { drops . push ( meta ?. hook ) ; dropped ?.( meta ?. hook ) ; } } ,
369- info : quiet , warn : quiet , error : quiet ,
372+ info : quiet , warn : quiet ,
373+ error : ( msg : string , _err ?: unknown , meta ?: any ) => {
374+ if ( / f i r e - a n d - f o r g e t / . test ( msg ) ) { events . push ( `error logged:${ meta ?. hook } ` ) ; errored ( meta ?. hook ) ; }
375+ } ,
370376 } ;
371377 const engine = new ObjectQL ( { logger } as any ) ;
372378 const driver = makeDriver ( ) ;
@@ -417,17 +423,40 @@ async function makeCommitEngine() {
417423 settle [ name ] ! ( 0 ) ;
418424 } ,
419425 } as unknown as Hook ;
420- const bind = ( withGate : boolean , opts : { userPin ?: boolean } = { } ) => bindHooksToEngine (
426+ // [#33760 review#2] A read's `async` hook: a rollback does not undo the read,
427+ // so its run is not the unit's to hold or drop. It writes a note of its own.
428+ const readNote = {
429+ name : 'read_note' , object : 'rec' , events : [ 'afterFind' ] , async : true , onError : 'log' ,
430+ handler : async ( ctx : any ) => {
431+ events . push ( 'read-hook started' ) ;
432+ runOf ( 'read' ) ;
433+ await ctx . api . object ( 'note' ) . insert ( { name : 'note:read' } ) ;
434+ settle . read ! ( 0 ) ;
435+ } ,
436+ } as unknown as Hook ;
437+ // A deferred run that throws once it starts, after the commit. The default
438+ // `onError` ('abort'), so the throw reaches the fire-and-forget catch.
439+ const thrower = {
440+ name : 'thrower' , object : 'rec' , events : [ 'afterInsert' ] , async : true ,
441+ handler : async ( ) => { events . push ( 'thrower started' ) ; throw new Error ( 'the deferred run fails' ) ; } ,
442+ } as unknown as Hook ;
443+ const bind = ( withGate : boolean , opts : { userPin ?: boolean ; read ?: boolean ; thrower ?: boolean } = { } ) => bindHooksToEngine (
421444 engine ,
422- opts . userPin ? [ userPin ] : withGate ? [ asyncNote , gate ] : [ asyncNote ] ,
445+ opts . read ? [ readNote ] : opts . thrower ? [ thrower ] : opts . userPin ? [ userPin ] : withGate ? [ asyncNote , gate ] : [ asyncNote ] ,
423446 { packageId : 'app:t' , logger : logger as any } ,
424447 ) ;
425448 const api = new ScopedContext ( { isSystem : true } as any , engine as any ) ;
426449 const droppedRun = ( ) => new Promise < string > ( ( r ) => { dropped = r ; } ) ;
427450 const notes = ( ) => driver . rowsOf ( 'note' ) . map ( ( r : any ) => r . name ) ;
428- return { engine, driver, api, events, drops, bind, runOf, droppedRun, notes, apiKinds } ;
451+ return { engine, driver, api, events, drops, bind, runOf, droppedRun, notes, apiKinds, erroredRun } ;
429452}
430453
454+ /** Did the read's hook start while the unit is still open? Bounded, so a run held for the commit reads as `not started`. */
455+ const startedInUnit = ( started : Promise < unknown > ) => Promise . race ( [
456+ started . then ( ( ) => 'started in the unit' ) ,
457+ new Promise < string > ( ( r ) => setTimeout ( ( ) => r ( 'not started in the unit' ) , 200 ) ) ,
458+ ] ) ;
459+
431460describe ( '#33760 — an async after-hook fired inside a unit runs on its commit, never on its rollback' , ( ) => {
432461 it ( 'engine.transaction() commits: the hook starts after the commit, sees its row, and writes outside the unit' , async ( ) => {
433462 const { engine, driver, events, bind, runOf, notes } = await makeCommitEngine ( ) ;
@@ -575,6 +604,63 @@ describe('#33760 — an async after-hook fired inside a unit runs on its commit,
575604 expect ( drops ) . toEqual ( [ 'user_pin' ] ) ;
576605 } ) ;
577606
607+ it ( 'a deferred run that throws after the commit: transaction() still resolves and the failure is logged' , async ( ) => {
608+ const { engine, events, bind, erroredRun } = await makeCommitEngine ( ) ;
609+ bind ( false , { thrower : true } ) ;
610+ await expect ( engine . transaction ( async ( trx : any ) => {
611+ await engine . insert ( 'rec' , { name : 'r1' } , { context : { ...trx , isSystem : true } } ) ;
612+ events . push ( 'callback returns' ) ;
613+ return 'resolved' ;
614+ } ) ) . resolves . toBe ( 'resolved' ) ;
615+ expect ( await erroredRun ) . toBe ( 'thrower' ) ;
616+ expect ( events ) . toEqual ( [ 'callback returns' , 'commit' , 'thrower started' , 'error logged:thrower' ] ) ;
617+ } ) ;
618+
619+ it ( 'afterFind inside engine.transaction() that commits: a read\'s async hook starts at dispatch, in the unit\'s lifetime, not after the commit' , async ( ) => {
620+ const { engine, driver, events, bind, runOf, notes } = await makeCommitEngine ( ) ;
621+ await engine . insert ( 'rec' , { name : 'r0' } , { context : { isSystem : true } } ) ;
622+ bind ( false , { read : true } ) ;
623+ await engine . transaction ( async ( trx : any ) => {
624+ await engine . find ( 'rec' , { } , { context : { ...trx , isSystem : true } } as any ) ;
625+ events . push ( await startedInUnit ( runOf ( 'read' ) ) ) ;
626+ } ) ;
627+ await runOf ( 'read' ) ;
628+ expect ( events ) . toEqual ( [ 'read-hook started' , 'started in the unit' , 'commit' ] ) ;
629+ expect ( notes ( ) ) . toEqual ( [ 'note:read' ] ) ;
630+ // Outside the unit, as under #33651: its write enrolled in nothing.
631+ expect ( unenrolled ( driver ) . filter ( ( [ o ] ) => o === 'note' ) ) . toEqual ( [ [ 'note' , true ] ] ) ;
632+ } ) ;
633+
634+ it ( 'afterFind inside engine.transaction() that rolls back: the read\'s async hook still runs, outside the unit' , async ( ) => {
635+ const { engine, driver, events, drops, bind, runOf } = await makeCommitEngine ( ) ;
636+ await engine . insert ( 'rec' , { name : 'r0' } , { context : { isSystem : true } } ) ;
637+ bind ( false , { read : true } ) ;
638+ await expect ( engine . transaction ( async ( trx : any ) => {
639+ await engine . find ( 'rec' , { } , { context : { ...trx , isSystem : true } } as any ) ;
640+ events . push ( await startedInUnit ( runOf ( 'read' ) ) ) ;
641+ throw new Error ( 'the caller rolls back' ) ;
642+ } ) ) . rejects . toThrow ( 'the caller rolls back' ) ;
643+ await runOf ( 'read' ) ;
644+ expect ( events ) . toEqual ( [ 'read-hook started' , 'started in the unit' , 'rollback' ] ) ;
645+ expect ( drops ) . toEqual ( [ ] ) ;
646+ // Its write enrolled in nothing. (This double's rollback restores a whole
647+ // store snapshot, so the row itself is not read here; the dogfood twin
648+ // reads it standing on SQLite.)
649+ expect ( unenrolled ( driver ) . filter ( ( [ o ] ) => o === 'note' ) ) . toEqual ( [ [ 'note' , true ] ] ) ;
650+ } ) ;
651+
652+ it ( 'CONTROL afterFind outside any unit: the read\'s async hook starts at dispatch, unchanged' , async ( ) => {
653+ const { driver, api, events, bind, runOf, notes } = await makeCommitEngine ( ) ;
654+ await api . object ( 'rec' ) . insert ( { name : 'r0' } ) ;
655+ bind ( false , { read : true } ) ;
656+ await api . object ( 'rec' ) . find ( { } ) ;
657+ events . push ( 'find returns' ) ;
658+ await runOf ( 'read' ) ;
659+ expect ( events ) . toEqual ( [ 'read-hook started' , 'find returns' ] ) ;
660+ expect ( driver . begun ) . toHaveLength ( 0 ) ;
661+ expect ( notes ( ) ) . toEqual ( [ 'note:read' ] ) ;
662+ } ) ;
663+
578664 it ( 'CONTROL outside any unit: the hook starts at dispatch, before the write returns, unchanged' , async ( ) => {
579665 const { driver, api, events, bind, runOf } = await makeCommitEngine ( ) ;
580666 bind ( false ) ;
0 commit comments