Get stack traces for leaked tasks and event loop blocks
mainPyleak provides detailed stack traces to help locate the source of leaks or blocking:
Asyncio Tasks:
no_task_leaksprovides the current stack trace of the leaked task. You can also include the creation stack trace by settingenable_creation_tracking=True.- Warning:
enable_creation_trackingmonkey patchesasyncio.create_taskand is not recommended for production. - The
TaskLeakErrorexception has aleaked_tasksattribute containingLeakedTaskobjects.
- Warning:
Event Loop Blocks:
no_event_loop_blockingprovides the stack trace of the code that blocked the loop, along with the duration and timestamp.
# Example: Getting creation stack trace for tasks
async with no_task_leaks(action="raise", enable_creation_tracking=True):
await leaky_function()
# Example: Catching EventLoopBlockError
try:
async with no_event_loop_blocking(action="raise"):
await some_blocking_code()
except EventLoopBlockError as e:
print(e)