@@ -191,13 +191,15 @@ export default class Zemu {
191191
192192 static stopAllEmuContainers ( ) : void {
193193 const timer = setTimeout ( ( ) => {
194- console . log ( 'Could not kill all containers before timeout!' )
194+ process . stderr . write ( 'Could not kill all containers before timeout!\n ' )
195195 process . exit ( 1 )
196196 } , KILL_TIMEOUT )
197197
198198 // Clean up pool first
199199 if ( Zemu . containerPool ) {
200- Zemu . containerPool . cleanup ( ) . catch ( ( error ) => console . warn ( 'Failed to cleanup container pool:' , error ) )
200+ Zemu . containerPool . cleanup ( ) . catch ( ( error ) => {
201+ process . stderr . write ( `Failed to cleanup container pool: ${ error } \n` )
202+ } )
201203 }
202204
203205 // Then kill any remaining containers
@@ -409,31 +411,32 @@ export default class Zemu {
409411 }
410412
411413 async close ( ) : Promise < void > {
412- try {
413- this . stopGRPCServer ( )
414+ this . stopGRPCServer ( )
414415
416+ try {
415417 if ( this . usingPool && this . pooledContainer && Zemu . containerPool ) {
416418 this . log ( 'Returning container to pool' )
417419 await Zemu . containerPool . release ( this . pooledContainer )
418- this . usingPool = false
419- this . pooledContainer = null
420420 } else {
421421 this . log ( 'Stopping container' )
422422 await this . emuContainer . stop ( )
423423 }
424424 } catch ( error ) {
425425 this . log ( `Error during close: ${ error } ` )
426426 // If pool return fails, try to stop container directly
427+ if ( this . usingPool && this . emuContainer ) {
428+ this . log ( 'Attempting direct container stop after pool release failure' )
429+ await this . emuContainer . stop ( ) . catch ( ( stopError ) => {
430+ this . log ( `Failed to stop container directly: ${ stopError } ` )
431+ } )
432+ }
433+ throw error
434+ } finally {
435+ // Always clean up state
427436 if ( this . usingPool ) {
428- try {
429- await this . emuContainer . stop ( )
430- } catch ( stopError ) {
431- this . log ( `Failed to stop container after pool release failure: ${ stopError } ` )
432- }
433437 this . usingPool = false
434438 this . pooledContainer = null
435439 }
436- throw error
437440 }
438441 }
439442
@@ -469,7 +472,7 @@ export default class Zemu {
469472 }
470473 }
471474
472- // For exchange and other methods , apply similar wrapping
475+ // For exchange method , apply similar wrapping
473476 if ( prop === 'exchange' ) {
474477 return async function ( apdu : Buffer ) {
475478 try {
@@ -487,6 +490,23 @@ export default class Zemu {
487490 }
488491 }
489492
493+ // For setScrambleKey method, wrap if it exists
494+ if ( prop === 'setScrambleKey' && typeof target [ prop ] === 'function' ) {
495+ return async function ( key : string ) {
496+ try {
497+ self . lastTransportError = null
498+ const result = await ( target as any ) . setScrambleKey ( key )
499+ return result
500+ } catch ( error ) {
501+ self . lastTransportError = error as Error
502+ if ( isCriticalTransportError ( error ) ) {
503+ self . log ( `Critical transport error in setScrambleKey: ${ error } ` )
504+ }
505+ throw error
506+ }
507+ }
508+ }
509+
490510 // For all other properties/methods, return as-is
491511 return Reflect . get ( target , prop , receiver )
492512 } ,
0 commit comments