Skip to content

Commit 5a39735

Browse files
committed
Gdbserver: replace server listening callback with notification.
1 parent 74a637d commit 5a39735

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

pyocd/__main__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def _process_commands(self, commands):
674674
except IndexError:
675675
pass
676676

677-
def server_listening(self, server):
677+
def _gdbserver_listening_cb(self, note):
678678
"""! @brief Callback invoked when the gdbserver starts listening on its port."""
679679
if self.echo_msg is not None:
680680
print(self.echo_msg, file=sys.stderr)
@@ -770,9 +770,8 @@ def do_gdbserver(self):
770770
# Don't create a server if this core is not listed by the user.
771771
if core_number not in core_list:
772772
continue
773-
gdb = GDBServer(session,
774-
core=core_number,
775-
server_listening_callback=self.server_listening)
773+
gdb = GDBServer(session, core=core_number)
774+
session.subscribe(self._gdbserver_listening_cb, GDBServer.GDBSERVER_START_LISTENING_EVENT, gdb)
776775
session.gdbservers[core_number] = gdb
777776
gdbs.append(gdb)
778777
gdb.start()

pyocd/gdbserver/gdbserver.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)