From f179808f259f1260e42f27ae55f787e6ffa431f5 Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Fri, 20 Dec 2024 15:38:13 +0100 Subject: [PATCH 1/2] Make rpc callback exception more explicit and wind up to show more infos --- src/plumpy/processes.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/plumpy/processes.py b/src/plumpy/processes.py index 0866ee41..a5f01603 100644 --- a/src/plumpy/processes.py +++ b/src/plumpy/processes.py @@ -1021,11 +1021,37 @@ def _schedule_rpc(self, callback: Callable[..., Any], *args: Any, **kwargs: Any) async def run_callback() -> None: with kiwipy.capture_exceptions(kiwi_future): - result = callback(*args, **kwargs) - while asyncio.isfuture(result): - result = await result + try: + result = callback(*args, **kwargs) + except Exception as exc: + import traceback + import inspect + + # Get traceback as a string + tb_str = ''.join(traceback.format_exception(type(exc), exc, exc.__traceback__)) + + # Attempt to get file and line number where the callback is defined + # Note: This might fail for certain built-in or dynamically generated functions. + # If it fails, just skip that part. + try: + source_file = inspect.getfile(callback) + # getsourcelines returns a tuple (list_of_source_lines, starting_line_number) + _, start_line = inspect.getsourcelines(callback) + callback_location = f'{source_file}:{start_line}' + except Exception: + callback_location = '' + + # Include the callback name, file/line info, and the full traceback in the message + raise RuntimeError( + f"Error invoking callback '{callback.__name__}' at {callback_location}.\n" + f'Exception: {type(exc).__name__}: {exc}\n\n' + f'Full Traceback:\n{tb_str}' + ) from exc + else: + while asyncio.isfuture(result): + result = await result - kiwi_future.set_result(result) + kiwi_future.set_result(result) # Schedule the task and give back a kiwi future asyncio.run_coroutine_threadsafe(run_callback(), self.loop) From d60440c3d8ce4ffa4f85922dd5b4c0435ebc5d5c Mon Sep 17 00:00:00 2001 From: Jusong Yu Date: Wed, 8 Jan 2025 12:36:53 +0100 Subject: [PATCH 2/2] pre-commit --- src/plumpy/processes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plumpy/processes.py b/src/plumpy/processes.py index 3f34c867..409374d0 100644 --- a/src/plumpy/processes.py +++ b/src/plumpy/processes.py @@ -1023,8 +1023,8 @@ async def run_callback() -> None: try: result = callback(*args, **kwargs) except Exception as exc: - import traceback import inspect + import traceback # Get traceback as a string tb_str = ''.join(traceback.format_exception(type(exc), exc, exc.__traceback__))