@@ -41,7 +41,7 @@ import {
4141 freshHook ,
4242 laneStageHook
4343} from "./scheduler.js" ;
44- import type { Computed , Link } from "./types.js" ;
44+ import type { Computed , Link , Owner } from "./types.js" ;
4545
4646// The lazily-created Set is the ONE container for pending sources. Its
4747// predecessor — a singular slot promoted to a Set on the second source —
@@ -578,18 +578,14 @@ export function handleAsync<T>(
578578 // close registration uses the ambient owner; and DEFERRED (the flattening
579579 // path — a thenable resolved to an iterable in a later microtask), where
580580 // there is no caller to serve and no ambient owner: sync-settled steps
581- // write through asyncWrite, and close registration goes through the slot
582- // the thenable branch pre-registered while it still owned the context.
583- // Returns whether a sync answer landed (first yield or empty completion) —
584- // meaningful only in the live posture.
585- const consumeIterator = (
586- source : AsyncIterable < T > ,
587- registerClose ?: ( fn : ( ) => void ) => void
588- ) : boolean => {
581+ // write through asyncWrite, and close registration targets `owner` (el
582+ // itself) directly. Returns whether a sync answer landed (first yield or
583+ // empty completion) — meaningful only in the live posture.
584+ const consumeIterator = ( source : AsyncIterable < T > , owner ?: Owner ) : boolean => {
589585 const it = source [ Symbol . asyncIterator ] ( ) ;
590586 let hadValue = false ;
591587 let completed = false ;
592- let initialRead = ! registerClose ;
588+ let initialRead = ! owner ;
593589
594590 const close = ( ) => {
595591 if ( completed ) return ;
@@ -599,7 +595,7 @@ export function handleAsync<T>(
599595 if ( isThenable ( returned ) ) returned . then ( undefined , ( ) => { } ) ;
600596 } catch { }
601597 } ;
602- registerClose ? registerClose ( close ) : cleanup ( close ) ;
598+ cleanup ( close , owner ) ;
603599 // Flight-identity cancellation (#3122): the registration above is the
604600 // owner-death backstop; the teardown slot fires at the _inFlight release
605601 // sites so supersede stops this stream immediately.
@@ -608,8 +604,15 @@ export function handleAsync<T>(
608604 // Release check before each next pull: an unobserved lazy node must tear
609605 // down (its close above runs via disposal, closing the iterator) instead
610606 // of pumping the stream forever with zero subscribers (#2935).
607+ // Later pulls run from an async continuation with no compute catch above
608+ // them: a throw invoking the protocol (next(), the step's then) settles
609+ // as this flight's error instead of escaping as a rejection (#4112).
611610 const iterateOrRelease = ( ) => {
612- if ( ! settleAutodispose ( ) ) iterate ( ) ;
611+ try {
612+ if ( ! settleAutodispose ( ) ) iterate ( ) ;
613+ } catch ( e ) {
614+ handleError ( e ) ;
615+ }
613616 } ;
614617
615618 const iterate = ( ) : boolean => {
@@ -700,7 +703,7 @@ export function handleAsync<T>(
700703 // stream itself rather than settling on the iterable object. One level
701704 // only: A+ `then` already collapses nested thenables, so the resolved
702705 // value is never itself a thenable.
703- const flattenIfIterable = ( value : any , registerClose ?: ( fn : ( ) => void ) => void ) : boolean => {
706+ const flattenIfIterable = ( value : any , owner ?: Owner ) : boolean => {
704707 let innerIterator : any = false ;
705708 if ( typeof value === "object" && value !== null ) {
706709 untrack ( ( ) => {
@@ -709,8 +712,8 @@ export function handleAsync<T>(
709712 }
710713 if ( ! innerIterator ) return false ;
711714 syncValue = undefined as T ;
712- const landed = consumeIterator ( value as AsyncIterable < T > , registerClose ) ;
713- if ( ! registerClose ) liveLanded = landed ;
715+ const landed = consumeIterator ( value as AsyncIterable < T > , owner ) ;
716+ if ( ! owner ) liveLanded = landed ;
714717 return true ;
715718 } ;
716719
@@ -719,34 +722,32 @@ export function handleAsync<T>(
719722 rejected = false ,
720723 syncError : any ,
721724 isSync = true ;
722- // Close registration for the flattening path. Consumption starts in a
723- // microtask where the ambient owner is gone (or worse, someone else's),
724- // so `cleanup()` can't be used — the close targets el's disposal list
725- // directly, exactly where a live cleanup() during this recompute would
726- // have put it. Only a flight that actually flattens becomes
727- // disposal-bearing.
728- const registerDeferredClose = ( fn : ( ) => void ) => {
729- if ( ! el . _disposal ) el . _disposal = fn ;
730- else if ( Array . isArray ( el . _disposal ) ) el . _disposal . push ( fn ) ;
731- else el . _disposal = [ el . _disposal , fn ] ;
732- } ;
733725 ( __DEV__ ? watchAsyncTail ( el , result as PromiseLike < T > ) : ( result as PromiseLike < T > ) ) . then (
734726 v => {
735727 if ( isSync ) {
736728 syncValue = v ;
737729 resolved = true ;
738- } else if (
739- el . _x ?. _inFlight === result &&
740- ! ( el . _flags & REACTIVE_DISPOSED ) &&
741- flattenIfIterable ( v , registerDeferredClose )
742- ) {
743- // Flattened: the stream is the value. Each yield lands through
744- // asyncWrite under this flight's identity; the first one clears
745- // pending exactly like a plain promise resolution would have.
746- // (Disposed nodes never start a pump — their disposal list has
747- // already run, so nothing could ever close the iterator.)
748730 } else {
749- asyncWrite ( v ) ;
731+ // Flattening consumes the stream as the value: each yield lands
732+ // through asyncWrite under this flight's identity; the first one
733+ // clears pending exactly like a plain promise resolution would.
734+ // Disposed nodes never start a pump (their disposal list already
735+ // ran, so nothing could close the iterator). Consumption starts in
736+ // a microtask where the ambient owner is gone, so the close
737+ // registers on el's own disposal list. Iterator setup here has no
738+ // compute catch above it: a protocol throw settles as this
739+ // flight's error (#4112).
740+ try {
741+ if (
742+ el . _x ?. _inFlight === result &&
743+ ! ( el . _flags & REACTIVE_DISPOSED ) &&
744+ flattenIfIterable ( v , el )
745+ )
746+ return ;
747+ asyncWrite ( v ) ;
748+ } catch ( e ) {
749+ handleError ( e ) ;
750+ }
750751 settleAutodispose ( ) ;
751752 }
752753 } ,
0 commit comments