From b09e61f68d52b862291679f36b64f841601f31a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 14 May 2025 11:51:45 +0200 Subject: [PATCH] clan-app: decode function arguments ahead of launching a thread. If something goew wrong here we rather want to crash the app and get bug reports. --- .../clan-app/clan_app/deps/webview/webview.py | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/pkgs/clan-app/clan_app/deps/webview/webview.py b/pkgs/clan-app/clan_app/deps/webview/webview.py index 70ba12cfe..126a30fda 100644 --- a/pkgs/clan-app/clan_app/deps/webview/webview.py +++ b/pkgs/clan-app/clan_app/deps/webview/webview.py @@ -68,29 +68,28 @@ class Webview: arg: int, ) -> None: op_key = op_key_bytes.decode() + args = json.loads(request_data.decode()) + log.debug(f"Calling {method_name}({args[0]})") + + # Initialize dataclasses from the payload + reconciled_arguments = {} + for k, v in args[0].items(): + # Some functions expect to be called with dataclass instances + # But the js api returns dictionaries. + # Introspect the function and create the expected dataclass from dict dynamically + # Depending on the introspected argument_type + arg_class = api.get_method_argtype(method_name, k) + + # TODO: rename from_dict into something like construct_checked_value + # from_dict really takes Anything and returns an instance of the type/class + reconciled_arguments[k] = from_dict(arg_class, v) + + reconciled_arguments["op_key"] = op_key + # TODO: We could remove the wrapper in the MethodRegistry + # and just call the method directly def thread_task(stop_event: threading.Event) -> None: try: - args = json.loads(request_data.decode()) - - log.debug(f"Calling {method_name}({args[0]})") - # Initialize dataclasses from the payload - reconciled_arguments = {} - for k, v in args[0].items(): - # Some functions expect to be called with dataclass instances - # But the js api returns dictionaries. - # Introspect the function and create the expected dataclass from dict dynamically - # Depending on the introspected argument_type - arg_class = api.get_method_argtype(method_name, k) - - # TODO: rename from_dict into something like construct_checked_value - # from_dict really takes Anything and returns an instance of the type/class - reconciled_arguments[k] = from_dict(arg_class, v) - - reconciled_arguments["op_key"] = op_key - # TODO: We could remove the wrapper in the MethodRegistry - # and just call the method directly - set_should_cancel(lambda: stop_event.is_set()) result = wrap_method(**reconciled_arguments)