From b4f47cec0e5f0e2185506e50f9e894a99f743b01 Mon Sep 17 00:00:00 2001 From: Atiqur Rahman Date: Tue, 28 Jul 2026 23:57:23 +0600 Subject: [PATCH] fix(sync): clear __pw_stack__ on task completion to prevent memory leak (fixes #3157) --- playwright/_impl/_sync_base.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/playwright/_impl/_sync_base.py b/playwright/_impl/_sync_base.py index e13680b8c..9a526be8b 100644 --- a/playwright/_impl/_sync_base.py +++ b/playwright/_impl/_sync_base.py @@ -108,7 +108,12 @@ def _sync( setattr(task, "__pw_stack__", _capture_stack_trace()) setattr(task, "__pw_stack_trace__", traceback.extract_stack(limit=10)) - task.add_done_callback(lambda _: g_self.switch()) + def _on_done(_: Any) -> None: + if hasattr(task, "__pw_stack__"): + delattr(task, "__pw_stack__") + g_self.switch() + + task.add_done_callback(_on_done) while not task.done(): self._dispatcher_fiber.switch() asyncio._set_running_loop(self._loop)