Skip to content

Commit 1fbda8c

Browse files
Emantorjluebbe
authored andcommitted
remote/exporter: guard against repeated resource requests
Handle acquire/release calls for already acquired/released resources as errors by raising an exception and providing the reason to the coordinator. That also means that these calls will now return an error to the coordinator. Signed-off-by: Rouven Czerwinski <r.czerwinski@pengutronix.de>
1 parent 4a69387 commit 1fbda8c

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

labgrid/remote/exporter.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ExporterError(Exception):
3636
class BrokenResourceError(ExporterError):
3737
pass
3838

39+
class UnknownResourceError(ExporterError):
40+
pass
41+
42+
class InvalidResourceRequestError(ExporterError):
43+
pass
3944

4045
def log_subprocess_kernel_stack(logger, child):
4146
if child.poll() is not None: # nothing to check if no longer running
@@ -889,7 +894,7 @@ async def message_pump(self):
889894
out_message.set_acquired_request.resource_name,
890895
)
891896
success = True
892-
except BrokenResourceError as e:
897+
except (BrokenResourceError, InvalidResourceRequestError, UnknownResourceError) as e:
893898
reason = e.args[0]
894899
finally:
895900
in_message = labgrid_coordinator_pb2.ExporterInMessage()
@@ -924,8 +929,10 @@ async def message_pump(self):
924929
async def acquire(self, group_name, resource_name, place_name):
925930
resource = self.groups.get(group_name, {}).get(resource_name)
926931
if resource is None:
927-
logging.error("acquire request for unknown resource %s/%s by %s", group_name, resource_name, place_name)
928-
return
932+
raise UnknownResourceError(f"acquire request for unknown resource {group_name}/{resource_name} by {place_name}")
933+
934+
if resource.acquired:
935+
raise InvalidResourceRequestError(f"Resource {group_name}/{resource_name} is already acquired by {resource.acquired}")
929936

930937
try:
931938
resource.acquire(place_name)
@@ -935,8 +942,11 @@ async def acquire(self, group_name, resource_name, place_name):
935942
async def release(self, group_name, resource_name):
936943
resource = self.groups.get(group_name, {}).get(resource_name)
937944
if resource is None:
938-
logging.error("release request for unknown resource %s/%s", group_name, resource_name)
939-
return
945+
logging.error()
946+
raise UnknownResourceError(f"release request for unknown resource {group_name}/{resource_name}")
947+
948+
if not resource.acquired:
949+
raise InvalidResourceRequestError(f"Resource {group_name}/{resource_name} is not acquired")
940950

941951
try:
942952
resource.release()

0 commit comments

Comments
 (0)