@@ -443,13 +443,25 @@ def run_gdb(self):
443443 with self : # disable console control
444444 sys .stderr .write (ANSI_NORMAL )
445445 try :
446- subprocess .call (["%sgdb" % self .toolchain_prefix ,
446+ process = subprocess .Popen (["%sgdb" % self .toolchain_prefix ,
447447 "-ex" , "set serial baud %d" % self .serial .baudrate ,
448448 "-ex" , "target remote %s" % self .serial .port ,
449449 "-ex" , "interrupt" , # monitor has already parsed the first 'reason' command, need a second
450450 self .elf_file ], cwd = "." )
451+ process .wait ()
451452 except KeyboardInterrupt :
452453 pass # happens on Windows, maybe other OSes
454+ finally :
455+ try :
456+ # on Linux, maybe other OSes, gdb sometimes seems to be alive even after wait() returns...
457+ process .terminate ()
458+ except :
459+ pass
460+ try :
461+ # also on Linux, maybe other OSes, gdb sometimes exits uncleanly and breaks the tty mode
462+ subprocess .call (["stty" , "sane" ])
463+ except :
464+ pass # don't care if there's no stty, we tried...
453465 self .prompt_next_action ("gdb exited" )
454466
455467 def output_enable (self , enable ):
@@ -567,6 +579,17 @@ def __init__(self, output):
567579 self .handle = GetStdHandle (STD_ERROR_HANDLE if self .output == sys .stderr else STD_OUTPUT_HANDLE )
568580 self .matched = b''
569581
582+ def _output_write (self , data ):
583+ # Windows 10 bug since the Fall Creators Update, sometimes writing to console randomly fails
584+ # (but usually succeeds afterwards, it seems.)
585+ # Ref https://github.com/espressif/esp-idf/issues/1136
586+ for tries in range (3 ):
587+ try :
588+ self .output .write (data )
589+ return
590+ except IOError :
591+ pass
592+
570593 def write (self , data ):
571594 for b in data :
572595 l = len (self .matched )
@@ -585,18 +608,10 @@ def write(self, data):
585608 color |= FOREGROUND_INTENSITY
586609 SetConsoleTextAttribute (self .handle , color )
587610 else :
588- self .output . write (self .matched ) # not an ANSI color code, display verbatim
611+ self ._output_write (self .matched ) # not an ANSI color code, display verbatim
589612 self .matched = b''
590613 else :
591- try :
592- self .output .write (b )
593- except IOError :
594- # Windows 10 bug since the Fall Creators Update, sometimes writing to console randomly fails
595- # (but usually succeeds the second time, it seems.) Ref https://github.com/espressif/esp-idf/issues/1136
596- try :
597- self .output .write (b )
598- except IOError :
599- pass
614+ self ._output_write (b )
600615 self .matched = b''
601616
602617 def flush (self ):
0 commit comments