@@ -69,6 +69,7 @@ class MiraServer extends AppServer {
6969 private chatManager : ChatManager ;
7070 private transcriptionSSEManager = new SSEManager ( ) ; // Manages transcription SSE connections
7171 private dbAPI : DatabaseAPI ; // Database API for user settings
72+ private welcomeTimers = new Map < string , ReturnType < typeof setTimeout > > ( ) ; // Track welcome audio timers per session
7273
7374 constructor ( options : any ) {
7475 super ( options ) ;
@@ -231,11 +232,13 @@ class MiraServer extends AppServer {
231232 // Camera-only glasses: play welcome audio file after a short delay
232233 const welcomeSoundUrl = process . env . WELCOME_SOUND_URL ;
233234 if ( welcomeSoundUrl ) {
234- setTimeout ( ( ) => {
235+ const timerId = setTimeout ( ( ) => {
236+ this . welcomeTimers . delete ( sessionId ) ;
235237 session . audio . playAudio ( { audioUrl : welcomeSoundUrl } ) . catch ( ( err ) => {
236238 logger . debug ( 'Welcome audio failed:' , err ) ;
237239 } ) ;
238- } , 1250 ) ;
240+ } , 2000 ) ;
241+ this . welcomeTimers . set ( sessionId , timerId ) ;
239242 }
240243 }
241244
@@ -306,6 +309,14 @@ class MiraServer extends AppServer {
306309 protected onStop ( sessionId : string , userId : string , reason : string ) : Promise < void > {
307310 logger . info ( `Stopping Mira service for session ${ sessionId } , user ${ userId } ` ) ;
308311
312+ // Cancel pending welcome audio timer to avoid playing on a dead session
313+ const welcomeTimer = this . welcomeTimers . get ( sessionId ) ;
314+ if ( welcomeTimer ) {
315+ clearTimeout ( welcomeTimer ) ;
316+ this . welcomeTimers . delete ( sessionId ) ;
317+ logger . info ( `Cancelled pending welcome audio for session ${ sessionId } ` ) ;
318+ }
319+
309320 // Clean up transcription manager
310321 const manager = this . transcriptionManagers . get ( sessionId ) ;
311322 if ( manager ) {
0 commit comments