Skip to content

Commit

Permalink
Merge pull request #84 from dcz-self/ooc
Browse files Browse the repository at this point in the history
openocd: Fix cleanup after exceptions
  • Loading branch information
bradjc authored May 25, 2022
2 parents 32d438c + ea06cc0 commit 9d0f22c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions tockloader/openocd.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def run_terminal(self):

logging.debug('Running "{}".'.format(openocd_command.replace("$", "\$")))

cleanup = []
try:
# This won't print messages from OpenOCD,
# to avoid interfering with the console.
Expand All @@ -404,6 +405,8 @@ def run_terminal(self):
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
cleanup.append(ocd_p.wait)
cleanup.append(ocd_p.kill)

# Delay to give the connection time to start before running
# the RTT listener.
Expand All @@ -414,20 +417,18 @@ def run_terminal(self):
logging.status("Listening for messages.")
listener = socket.socket()
listener.connect(("127.0.0.1", 9999))
cleanup.append(listener.close)
out = listener.makefile(mode="rb")
cleanup.append(out.close)
for out_line in iter(out.readline, ""):
l = out_line.decode("utf-8", errors="replace")
if not l.startswith("###RTT Client: *"):
print(l, end="")
finally:
logging.status("Stopping")
try:
out.close()
listener.close()
ocd_p.kill()
ocd_p.wait()
except UnboundLocalError:
pass
for f in reversed(cleanup):
f()

openocd_command, _ = self._gather_openocd_cmdline(
[
"init",
Expand Down

0 comments on commit 9d0f22c

Please sign in to comment.