@@ -511,45 +511,6 @@ const unrenderableSources = new Map<string, number>()
511511 */
512512const UNRENDERABLE_TTL_MS = 5 * 60 * 1000
513513
514- /**
515- * Renders in flight, keyed identically to {@link compiledDocCache}. An artifact
516- * miss is the same for every concurrent reader — a freshly forked workspace whose
517- * document several viewers open at once, or one request whose blocks read the same
518- * file — and rendering is the expensive step, so they share one run instead of each
519- * paying for it. The entry is dropped as soon as the render settles, so a later read
520- * re-renders normally rather than replaying a stale result.
521- */
522- const inFlightRenders = new Map < string , Promise < { buffer : Buffer ; contentType : string } > > ( )
523-
524- /** Rejects when `signal` aborts, so a caller can abandon a shared render without cancelling it. */
525- function rejectOnAbort ( signal : AbortSignal ) : Promise < never > {
526- return new Promise ( ( _resolve , reject ) => {
527- if ( signal . aborted ) {
528- reject ( signal . reason ?? new Error ( 'Aborted' ) )
529- return
530- }
531- signal . addEventListener ( 'abort' , ( ) => reject ( signal . reason ?? new Error ( 'Aborted' ) ) , {
532- once : true ,
533- } )
534- } )
535- }
536-
537- function coalesceRender (
538- key : string ,
539- run : ( ) => Promise < { buffer : Buffer ; contentType : string } >
540- ) : Promise < { buffer : Buffer ; contentType : string } > {
541- const existing = inFlightRenders . get ( key )
542- if ( existing ) return existing
543- const started = run ( ) . finally ( ( ) => inFlightRenders . delete ( key ) )
544- // Every caller races this against its own signal, so all of them can walk away
545- // before it settles. Attach a terminal handler so a later rejection with no
546- // waiters left is not reported as an unhandled rejection — callers still observe
547- // it through their own reference.
548- started . catch ( ( ) => { } )
549- inFlightRenders . set ( key , started )
550- return started
551- }
552-
553514function markUnrenderable ( key : string ) : void {
554515 if ( unrenderableSources . size >= MAX_COMPILED_DOC_CACHE ) {
555516 unrenderableSources . delete ( unrenderableSources . keys ( ) . next ( ) . value as string )
@@ -680,11 +641,7 @@ export async function resolveServableDocBytes(args: {
680641 // (content-addressed), so racing a still-running write-time compile is wasteful
681642 // but correct.
682643 try {
683- // Same shape as the isolated-vm branch below: the shared run carries no
684- // caller's signal, and each caller races its own so an aborting reader gives
685- // up promptly without cancelling the render for everyone else.
686- const shared = coalesceRender ( renderKey , ( ) => compileDoc ( { source, fileName, workspaceId } ) )
687- return await ( signal ? Promise . race ( [ shared , rejectOnAbort ( signal ) ] ) : shared )
644+ return await compileDoc ( { source, fileName, workspaceId } )
688645 } catch ( error ) {
689646 // Only a script error is deterministic — the same bytes will never render, so
690647 // remembering that is safe. Infra failures (sandbox create/timeout, S3, an
@@ -706,22 +663,13 @@ export async function resolveServableDocBytes(args: {
706663 }
707664
708665 try {
709- // The shared run deliberately carries no caller's signal: it is one piece of
710- // work several readers are waiting on, so letting whoever happened to start it
711- // cancel it would reject every other waiter with an AbortError they did not
712- // ask for. Each caller instead races its own signal, so an aborting reader
713- // gives up promptly while the render continues for the rest and still lands in
714- // the cache.
715- const shared = coalesceRender ( renderKey , async ( ) => {
716- const compiled = await runSandboxTask (
717- format . taskId ,
718- { code : source , workspaceId : workspaceId || '' } ,
719- { ownerKey }
720- )
721- compiledCacheSet ( renderKey , compiled )
722- return { buffer : compiled , contentType : format . contentType }
723- } )
724- return await ( signal ? Promise . race ( [ shared , rejectOnAbort ( signal ) ] ) : shared )
666+ const compiled = await runSandboxTask (
667+ format . taskId ,
668+ { code : source , workspaceId : workspaceId || '' } ,
669+ { ownerKey, signal }
670+ )
671+ compiledCacheSet ( renderKey , compiled )
672+ return { buffer : compiled , contentType : format . contentType }
725673 } catch ( error ) {
726674 // Unlike the E2B engine, the isolated-vm task does not distinguish a script
727675 // error from an infra one, so the only signal available here is cancellation —
0 commit comments