@@ -607,12 +607,13 @@ export const searchLiveKnowledge = defineAuthorizedKnowledgeUseCase({
607607 results : [ ] ,
608608 }
609609 }
610+ let session : LiveAccountSession | undefined
610611 try {
611612 signal . throwIfAborted ( )
612613 const resolved = await measureSearchStage ( 'live.resolve' , ( ) =>
613614 resolveListedLiveAccount ( input , userId , account )
614615 )
615- const session = await measureSearchStage ( 'live.session' , ( ) =>
616+ session = await measureSearchStage ( 'live.session' , ( ) =>
616617 openLiveAccountSession ( {
617618 owner : input ,
618619 userId,
@@ -623,9 +624,10 @@ export const searchLiveKnowledge = defineAuthorizedKnowledgeUseCase({
623624 searches : natives . length ,
624625 } )
625626 )
627+ const currentSession = session
626628 return await Promise . all (
627629 natives . map ( ( target ) =>
628- searchQuery ( account , resolved , session , target . native , statusFor ( target ) ) . catch (
630+ searchQuery ( account , resolved , currentSession , target . native , statusFor ( target ) ) . catch (
629631 ( error ) => failed ( error , target )
630632 )
631633 )
@@ -634,6 +636,7 @@ export const searchLiveKnowledge = defineAuthorizedKnowledgeUseCase({
634636 return natives . map ( ( target ) => failed ( error , target ) )
635637 } finally {
636638 settled . abort ( )
639+ await session ?. close ( )
637640 }
638641 }
639642 let searched : SearchedQuery [ ]
@@ -760,8 +763,9 @@ export const readLiveDocument = defineAuthorizedKnowledgeUseCase({
760763 : AbortSignal . timeout ( 15_000 )
761764 const pool = createPinnedConnectionPool ( )
762765 let document : NativeDocument
766+ let session : LiveAccountSession | undefined
763767 try {
764- const session = await openLiveAccountSession ( {
768+ session = await openLiveAccountSession ( {
765769 owner : input ,
766770 userId,
767771 resolved,
@@ -774,7 +778,10 @@ export const readLiveDocument = defineAuthorizedKnowledgeUseCase({
774778 'not_found' ,
775779 'Document is outside your organization’s search scope'
776780 )
777- document = await measureSearchStage ( 'live.read' , ( ) => session . read ( reference , input . filters ) )
781+ const currentSession = session
782+ document = await measureSearchStage ( 'live.read' , ( ) =>
783+ currentSession . read ( reference , input . filters )
784+ )
778785 /** Readers degrade section failures to warnings, so the signal decides cancellation. */
779786 signal . throwIfAborted ( )
780787 const current = await session . verifyCurrent ( document )
@@ -786,7 +793,11 @@ export const readLiveDocument = defineAuthorizedKnowledgeUseCase({
786793 'Document is outside your organization’s search scope'
787794 )
788795 } finally {
789- pool . destroy ( )
796+ try {
797+ await session ?. close ( )
798+ } finally {
799+ pool . destroy ( )
800+ }
790801 }
791802 if ( ! matchesLiveFilters ( document , input . documentId , reference . provider , input . filters ) )
792803 throw new OrchestrationError ( 'not_found' , 'Document is outside the selected search filters' )
0 commit comments