diff --git a/letta/monitoring/event_loop_watchdog.py b/letta/monitoring/event_loop_watchdog.py index b405bb2b..2d707753 100644 --- a/letta/monitoring/event_loop_watchdog.py +++ b/letta/monitoring/event_loop_watchdog.py @@ -230,6 +230,13 @@ class EventLoopWatchdog: idx = frame.f_code.co_filename.find("letta/") path = frame.f_code.co_filename[idx + 6 :] if idx != -1 else frame.f_code.co_filename location = f"{path}:{frame.f_lineno}:{frame.f_code.co_name}" + + # For bounded tasks, use wrapped coroutine location instead + if frame.f_code.co_name == "bounded_coro": + task_name = task.get_name() + if task_name and task_name.startswith("bounded["): + location = task_name[8:-1] # Extract "file:line:func" from "bounded[...]" + tasks_by_location[location].append((task, stack)) break except Exception: diff --git a/letta/utils.py b/letta/utils.py index c4f75539..6b77b5fc 100644 --- a/letta/utils.py +++ b/letta/utils.py @@ -1444,13 +1444,27 @@ async def bounded_gather(coros: list[Coroutine], max_concurrency: int = 10) -> l semaphore = asyncio.Semaphore(max_concurrency) - async def bounded_coro(index: int, coro: Coroutine): + async def bounded_coro(index: int, coro: Coroutine, coro_name: str): async with semaphore: + # Set task name for diagnostics + task = asyncio.current_task() + if task: + task.set_name(f"bounded[{coro_name}]") result = await coro return (index, result) - # Wrap all coroutines with semaphore control - tasks = [bounded_coro(i, coro) for i, coro in enumerate(coros)] + # Wrap all coroutines with semaphore control, extracting location for diagnostics + tasks = [] + for i, coro in enumerate(coros): + coro_code = getattr(coro, "cr_code", None) + if coro_code: + filename = coro_code.co_filename + idx = filename.find("letta/") + filename = filename[idx + 6 :] if idx != -1 else filename.split("/")[-1] + coro_name = f"{filename}:{coro_code.co_firstlineno}:{coro_code.co_name}" + else: + coro_name = "unknown" + tasks.append(bounded_coro(i, coro, coro_name)) indexed_results = await asyncio.gather(*tasks) # Sort by original index to preserve order