@@ -102,7 +102,14 @@ class GDBServer(threading.Thread):
102102 This class start a GDB server listening a gdb connection on a specific port.
103103 It implements the RSP (Remote Serial Protocol).
104104 """
105- def __init__ (self , session , core = None , server_listening_callback = None ):
105+
106+ ## Notification event for the gdbserver beginnning to listen on its RSP port.
107+ GDBSERVER_START_LISTENING_EVENT = 'gdbserver-start-listening'
108+
109+ ## Timer delay for sending the notification that the server is listening.
110+ START_LISTENING_NOTIFY_DELAY = 0.03 # 30 ms
111+
112+ def __init__ (self , session , core = None ):
106113 super (GDBServer , self ).__init__ ()
107114 self .session = session
108115 self .board = session .board
@@ -140,7 +147,6 @@ def __init__(self, session, core=None, server_listening_callback=None):
140147 'report_core_number' ,
141148 ])
142149
143- self .server_listening_callback = server_listening_callback
144150 self .packet_size = 2048
145151 self .packet_io = None
146152 self .gdb_features = []
@@ -313,9 +319,13 @@ def run(self):
313319 try :
314320 self .detach_event .clear ()
315321
316- # Inform callback that the server is running.
317- if self .server_listening_callback :
318- self .server_listening_callback (self )
322+ # Notify listeners that the server is running after a short delay.
323+ #
324+ # This timer prevents a race condition where the notification is sent before the server is
325+ # actually listening. It's not a 100% guarantee, though.
326+ notify_timer = threading .Timer (self .START_LISTENING_NOTIFY_DELAY , self .session .notify ,
327+ args = (self .GDBSERVER_START_LISTENING_EVENT , self ))
328+ notify_timer .start ()
319329
320330 while not self .shutdown_event .isSet () and not self .detach_event .isSet ():
321331 connected = self .abstract_socket .connect ()
0 commit comments