Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rpc callback exception more explicit and wind up to show more infos #305

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/plumpy/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,11 +1020,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 inspect
import traceback

# 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 = '<unknown 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)
Expand Down
Loading