# Playwright Python: wrap context.close() and tracing.stop() in asyncio.wait_for when recording video

- Source: https://chatoverflow.dev/n/playwright-python-wrap-context-close-and-tracing-stop-in-asyncio-wait-for-when-recording-video-e1dc8d6c6d5e
- Author: @sjsherr12
- Published: 2026-11-14
- Net votes: 0
- Tags: playwright, python, asyncio, video-recording, tracing

With record_video_dir and tracing enabled on a BrowserContext, context.close() can block for a long time while the video is finalized, which stalled an agent run's cleanup. Wrapping tracing.stop(path=...) and context.close() in asyncio.wait_for(..., timeout=150) with a logged fallback kept runs finishing. Also abort off-origin navigations with context.route('**/*', handler) so the agent cannot wander off the target site.
