From 11dc2c44cb93bc44649db81727614955b4ff6ff7 Mon Sep 17 00:00:00 2001 From: Bastian Krause Date: Sun, 10 Aug 2025 17:19:15 +0200 Subject: [PATCH] remote/coordinator: drop returns from finally clauses Exceptions in finally clauses containing return statements are swallowed [1]. That means any exception raised in the try block and a subsequent KeyError in the finally block leads to the original exception being swallowed. Since Python 3.14 "the compiler emits a SyntaxWarning when a return, break or continue appears in a finally block (see PEP 765)." [2]. Fix this by moving the code relying on the session into the try clause making the return in the except clause obsolete. [1] https://peps.python.org/pep-0765/#motivation [2] https://docs.python.org/3.14/reference/compound_stmts.html#finally-clause Signed-off-by: Bastian Krause --- labgrid/remote/coordinator.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/labgrid/remote/coordinator.py b/labgrid/remote/coordinator.py index 3eb9c270c..5bddb089e 100644 --- a/labgrid/remote/coordinator.py +++ b/labgrid/remote/coordinator.py @@ -355,13 +355,11 @@ async def request_task(): finally: try: session = self.clients.pop(peer) + running_request_task.cancel() + await running_request_task + logging.debug("client aborted %s, cancelled: %s", session, context.cancelled()) except KeyError: logging.info("Never received startup from peer %s that disconnected", peer) - return - - running_request_task.cancel() - await running_request_task - logging.debug("client aborted %s, cancelled: %s", session, context.cancelled()) def _add_default_place(self, name): if name in self.places: @@ -462,15 +460,13 @@ async def request_task(): try: session = self.exporters.pop(peer) + for groupname, group in session.groups.items(): + for resourcename in group.copy(): + session.set_resource(groupname, resourcename, None) + + logging.debug("exporter aborted %s, cancelled: %s", context.peer(), context.cancelled()) except KeyError: logging.info("Never received startup from peer %s that disconnected", peer) - return - - for groupname, group in session.groups.items(): - for resourcename in group.copy(): - session.set_resource(groupname, resourcename, None) - - logging.debug("exporter aborted %s, cancelled: %s", context.peer(), context.cancelled()) @locked async def AddPlace(self, request, context):