@@ -48,7 +48,7 @@ export class MI2DebugSession extends DebugSession {
4848 super ( debuggerLinesStartAt1 , isServer ) ;
4949 }
5050
51- protected initDebugger ( ) {
51+ protected async initDebugger ( ) {
5252 this . miDebugger . on ( "launcherror" , this . launchError . bind ( this ) ) ;
5353 this . miDebugger . on ( "quit" , this . quitEvent . bind ( this ) ) ;
5454 this . miDebugger . on ( "exited-normally" , this . quitEvent . bind ( this ) ) ;
@@ -64,6 +64,10 @@ export class MI2DebugSession extends DebugSession {
6464 this . miDebugger . on ( "thread-exited" , this . threadExitedEvent . bind ( this ) ) ;
6565 this . miDebugger . once ( "debug-ready" , ( ( ) => this . sendEvent ( new InitializedEvent ( ) ) ) ) ;
6666 try {
67+ const socketlists = systemPath . join ( os . tmpdir ( ) , "code-debug-sockets" ) ;
68+ if ( fs . existsSync ( socketlists ) ) {
69+ await cleanInvalidSocketPath ( socketlists ) ;
70+ }
6771 this . commandServer = net . createServer ( c => {
6872 c . on ( "data" , data => {
6973 const rawCmd = data . toString ( ) ;
@@ -75,7 +79,7 @@ export class MI2DebugSession extends DebugSession {
7579 args = JSON . parse ( rawCmd . substring ( spaceIndex + 1 ) ) ;
7680 }
7781 Promise . resolve ( this . miDebugger [ func ] . apply ( this . miDebugger , args ) ) . then ( data => {
78- c . write ( data . toString ( ) ) ;
82+ c . write ( data instanceof Object ? JSON . stringify ( data ) . toString ( ) : data . toString ( ) ) ;
7983 } ) ;
8084 } ) ;
8185 } ) ;
@@ -516,10 +520,9 @@ export class MI2DebugSession extends DebugSession {
516520 }
517521 } else if ( typeof id == "string" ) {
518522 // Variable members
519- let variable ;
520523 try {
521524 // TODO: this evaluates on an (effectively) unknown thread for multithreaded programs.
522- variable = await this . miDebugger . evalExpression ( JSON . stringify ( id ) , 0 , 0 ) ;
525+ const variable = await this . miDebugger . evalExpression ( JSON . stringify ( id ) , 0 , 0 ) ;
523526 try {
524527 let expanded = expandValue ( createVariable , variable . result ( "value" ) , id , variable ) ;
525528 if ( ! expanded ) {
@@ -759,3 +762,38 @@ function prettyStringArray(strings) {
759762 return JSON . stringify ( strings ) ;
760763 } else return strings ;
761764}
765+
766+ async function cleanInvalidSocketPath ( socketlists :string ) {
767+ return new Promise ( ( resolve , reject ) => {
768+ fs . readdir ( socketlists , ( err , files ) => {
769+ if ( ! err ) {
770+ if ( files . length == 0 ) resolve ( '' ) ;
771+ files . forEach ( ( file ) => {
772+ try {
773+ const conn = net . connect ( systemPath . join ( socketlists , file ) ) ;
774+ conn . setTimeout ( 200 ) ;
775+ conn . on ( 'error' , ( ) => {
776+ fs . unlink ( systemPath . join ( socketlists , file ) , ( err ) => {
777+ if ( err )
778+ // eslint-disable-next-line no-console
779+ console . error ( "Failed to unlink invalid debug server" ) ;
780+ resolve ( '' ) ;
781+ } ) ;
782+ } ) ;
783+ conn . on ( 'timeout' , ( ) => {
784+ conn . destroy ( ) ;
785+ } ) ;
786+ } catch {
787+ fs . unlink ( systemPath . join ( socketlists , file ) , ( err ) => {
788+ if ( err )
789+ // eslint-disable-next-line no-console
790+ console . error ( "Failed to unlink invalid debug server" ) ;
791+ resolve ( '' ) ;
792+ } ) ;
793+ }
794+ } ) ;
795+ }
796+ resolve ( '' ) ;
797+ } ) ;
798+ } ) ;
799+ }
0 commit comments