Skip to content

Commit 390940a

Browse files
authored
Revert "fix: cleanup pending route handlers on close (#1412)" (#1429)
1 parent 35b63a8 commit 390940a

10 files changed

+8
-118
lines changed

playwright/_impl/_browser_context.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
from playwright._impl._frame import Frame
4949
from playwright._impl._har_router import HarRouter
5050
from playwright._impl._helper import (
51-
BackgroundTaskTracker,
5251
HarRecordingMetadata,
5352
RouteFromHarNotFoundPolicy,
5453
RouteHandler,
@@ -104,7 +103,6 @@ def __init__(
104103
self._request: APIRequestContext = from_channel(
105104
initializer["APIRequestContext"]
106105
)
107-
self._background_task_tracker: BackgroundTaskTracker = BackgroundTaskTracker()
108106
self._channel.on(
109107
"bindingCall",
110108
lambda params: self._on_binding(from_channel(params["binding"])),
@@ -115,7 +113,7 @@ def __init__(
115113
)
116114
self._channel.on(
117115
"route",
118-
lambda params: self._background_task_tracker.create_task(
116+
lambda params: asyncio.create_task(
119117
self._on_route(
120118
from_channel(params.get("route")),
121119
from_channel(params.get("request")),
@@ -165,14 +163,8 @@ def __init__(
165163
),
166164
)
167165
self._closed_future: asyncio.Future = asyncio.Future()
168-
169-
def _on_close(_: Any) -> None:
170-
self._background_task_tracker.close()
171-
self._closed_future.set_result(True)
172-
173166
self.once(
174-
self.Events.Close,
175-
_on_close,
167+
self.Events.Close, lambda context: self._closed_future.set_result(True)
176168
)
177169

178170
def __repr__(self) -> str:
@@ -195,10 +187,7 @@ async def _on_route(self, route: Route, request: Request) -> None:
195187
handled = await route_handler.handle(route, request)
196188
finally:
197189
if len(self._routes) == 0:
198-
try:
199-
await self._disable_interception()
200-
except Exception:
201-
pass
190+
asyncio.create_task(self._disable_interception())
202191
if handled:
203192
return
204193
await route._internal_continue(is_internal=True)

playwright/_impl/_helper.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -362,19 +362,3 @@ def is_file_payload(value: Optional[Any]) -> bool:
362362
and "mimeType" in value
363363
and "buffer" in value
364364
)
365-
366-
367-
class BackgroundTaskTracker:
368-
def __init__(self) -> None:
369-
self._pending_tasks: List[asyncio.Task] = []
370-
371-
def create_task(self, coro: Coroutine) -> asyncio.Task:
372-
task = asyncio.create_task(coro)
373-
task.add_done_callback(lambda task: self._pending_tasks.remove(task))
374-
self._pending_tasks.append(task)
375-
return task
376-
377-
def close(self) -> None:
378-
for task in self._pending_tasks:
379-
if not task.done():
380-
task.cancel()

playwright/_impl/_network.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ def _report_handled(self, done: bool) -> None:
223223
chain = self._handling_future
224224
assert chain
225225
self._handling_future = None
226-
if not chain.done():
227-
chain.set_result(done)
226+
chain.set_result(done)
228227

229228
def _check_not_handled(self) -> None:
230229
if not self._handling_future:

playwright/_impl/_page.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
from playwright._impl._frame import Frame
5656
from playwright._impl._har_router import HarRouter
5757
from playwright._impl._helper import (
58-
BackgroundTaskTracker,
5958
ColorScheme,
6059
DocumentLoadState,
6160
ForcedColors,
@@ -152,7 +151,6 @@ def __init__(
152151
self._browser_context._timeout_settings
153152
)
154153
self._video: Optional[Video] = None
155-
self._background_task_tracker = BackgroundTaskTracker()
156154
self._opener = cast("Page", from_nullable_channel(initializer.get("opener")))
157155

158156
self._channel.on(
@@ -194,7 +192,7 @@ def __init__(
194192
)
195193
self._channel.on(
196194
"route",
197-
lambda params: self._browser_context._background_task_tracker.create_task(
195+
lambda params: asyncio.create_task(
198196
self._on_route(
199197
from_channel(params["route"]), from_channel(params["request"])
200198
)
@@ -248,10 +246,7 @@ async def _on_route(self, route: Route, request: Request) -> None:
248246
handled = await route_handler.handle(route, request)
249247
finally:
250248
if len(self._routes) == 0:
251-
try:
252-
await self._disable_interception()
253-
except Exception:
254-
pass
249+
asyncio.create_task(self._disable_interception())
255250
if handled:
256251
return
257252
await self._browser_context._on_route(route, request)

tests/async/test_browsercontext_request_intercept.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,3 @@ async def test_should_give_access_to_the_intercepted_response_body(
174174
route.fulfill(response=response),
175175
eval_task,
176176
)
177-
178-
179-
async def test_should_cleanup_route_handlers_after_context_close(
180-
context: BrowserContext, page: Page
181-
) -> None:
182-
async def handle(r: Route):
183-
pass
184-
185-
await context.route("**", handle)
186-
try:
187-
await page.goto("https://example.com", timeout=700)
188-
except Exception:
189-
pass
190-
await context.close()
191-
192-
for task in asyncio.all_tasks():
193-
assert "_on_route" not in str(task)

tests/async/test_har.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ async def test_should_round_trip_har_zip(
503503
await expect(page_2.locator("body")).to_have_css(
504504
"background-color", "rgb(255, 192, 203)"
505505
)
506-
await context_2.close()
507506

508507

509508
async def test_should_round_trip_har_with_post_data(
@@ -537,7 +536,6 @@ async def test_should_round_trip_har_with_post_data(
537536
assert await page_2.evaluate(fetch_function, "3") == "3"
538537
with pytest.raises(Exception):
539538
await page_2.evaluate(fetch_function, "4")
540-
await context_2.close()
541539

542540

543541
async def test_should_disambiguate_by_header(
@@ -580,7 +578,6 @@ async def test_should_disambiguate_by_header(
580578
assert await page_2.evaluate(fetch_function, "baz2") == "baz2"
581579
assert await page_2.evaluate(fetch_function, "baz3") == "baz3"
582580
assert await page_2.evaluate(fetch_function, "baz4") == "baz1"
583-
await context_2.close()
584581

585582

586583
async def test_should_produce_extracted_zip(
@@ -608,7 +605,6 @@ async def test_should_produce_extracted_zip(
608605
await expect(page_2.locator("body")).to_have_css(
609606
"background-color", "rgb(255, 192, 203)"
610607
)
611-
await context_2.close()
612608

613609

614610
async def test_should_update_har_zip_for_context(
@@ -631,7 +627,6 @@ async def test_should_update_har_zip_for_context(
631627
await expect(page_2.locator("body")).to_have_css(
632628
"background-color", "rgb(255, 192, 203)"
633629
)
634-
await context_2.close()
635630

636631

637632
async def test_should_update_har_zip_for_page(
@@ -654,7 +649,6 @@ async def test_should_update_har_zip_for_page(
654649
await expect(page_2.locator("body")).to_have_css(
655650
"background-color", "rgb(255, 192, 203)"
656651
)
657-
await context_2.close()
658652

659653

660654
async def test_should_update_extracted_har_zip_for_page(
@@ -681,4 +675,3 @@ async def test_should_update_extracted_har_zip_for_page(
681675
await expect(page_2.locator("body")).to_have_css(
682676
"background-color", "rgb(255, 192, 203)"
683677
)
684-
await context_2.close()

tests/async/test_request_intercept.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from twisted.web import http
1919

20-
from playwright.async_api import BrowserContext, Page, Route
20+
from playwright.async_api import Page, Route
2121
from tests.server import Server
2222

2323

@@ -168,20 +168,3 @@ async def test_should_give_access_to_the_intercepted_response_body(
168168
route.fulfill(response=response),
169169
eval_task,
170170
)
171-
172-
173-
async def test_should_cleanup_route_handlers_after_context_close(
174-
context: BrowserContext, page: Page
175-
) -> None:
176-
async def handle(r: Route):
177-
pass
178-
179-
await page.route("**", handle)
180-
try:
181-
await page.goto("https://example.com", timeout=700)
182-
except Exception:
183-
pass
184-
await context.close()
185-
186-
for task in asyncio.all_tasks():
187-
assert "_on_route" not in str(task)

tests/sync/test_browsercontext_request_intercept.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
1615
from pathlib import Path
1716

1817
from twisted.web import http
@@ -122,17 +121,3 @@ def handle_route(route: Route) -> None:
122121
assert request.uri.decode() == "/title.html"
123122
original = (assetdir / "title.html").read_text()
124123
assert response.text() == original
125-
126-
127-
def test_should_cleanup_route_handlers_after_context_close(
128-
context: BrowserContext, page: Page
129-
) -> None:
130-
context.route("**", lambda r: None)
131-
try:
132-
page.goto("https://example.com", timeout=700)
133-
except Exception:
134-
pass
135-
context.close()
136-
137-
for task in asyncio.all_tasks():
138-
assert "_on_route" not in str(task)

tests/sync/test_har.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ def test_should_round_trip_har_with_post_data(
471471
assert page_2.evaluate(fetch_function, "3") == "3"
472472
with pytest.raises(Exception):
473473
page_2.evaluate(fetch_function, "4")
474-
context_2.close()
475474

476475

477476
def test_should_disambiguate_by_header(
@@ -513,7 +512,6 @@ def test_should_disambiguate_by_header(
513512
assert page_2.evaluate(fetch_function, "baz2") == "baz2"
514513
assert page_2.evaluate(fetch_function, "baz3") == "baz3"
515514
assert page_2.evaluate(fetch_function, "baz4") == "baz1"
516-
context_2.close()
517515

518516

519517
def test_should_produce_extracted_zip(
@@ -539,7 +537,6 @@ def test_should_produce_extracted_zip(
539537
page_2.goto(server.PREFIX + "/one-style.html")
540538
assert "hello, world!" in page_2.content()
541539
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
542-
context_2.close()
543540

544541

545542
def test_should_update_har_zip_for_context(
@@ -560,7 +557,6 @@ def test_should_update_har_zip_for_context(
560557
page_2.goto(server.PREFIX + "/one-style.html")
561558
assert "hello, world!" in page_2.content()
562559
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
563-
context_2.close()
564560

565561

566562
def test_should_update_har_zip_for_page(
@@ -581,7 +577,6 @@ def test_should_update_har_zip_for_page(
581577
page_2.goto(server.PREFIX + "/one-style.html")
582578
assert "hello, world!" in page_2.content()
583579
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
584-
context_2.close()
585580

586581

587582
def test_should_update_extracted_har_zip_for_page(
@@ -606,4 +601,3 @@ def test_should_update_extracted_har_zip_for_page(
606601
page_2.goto(server.PREFIX + "/one-style.html")
607602
assert "hello, world!" in page_2.content()
608603
expect(page_2.locator("body")).to_have_css("background-color", "rgb(255, 192, 203)")
609-
context_2.close()

tests/sync/test_request_intercept.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import asyncio
1615
from pathlib import Path
1716

1817
from twisted.web import http
1918

20-
from playwright.sync_api import BrowserContext, Page, Route
19+
from playwright.sync_api import Page, Route
2120
from tests.server import Server
2221

2322

@@ -116,17 +115,3 @@ def handle_route(route: Route) -> None:
116115
assert request.uri.decode() == "/title.html"
117116
original = (assetdir / "title.html").read_text()
118117
assert response.text() == original
119-
120-
121-
def test_should_cleanup_route_handlers_after_context_close(
122-
context: BrowserContext, page: Page
123-
) -> None:
124-
page.route("**", lambda r: None)
125-
try:
126-
page.goto("https://example.com", timeout=700)
127-
except Exception:
128-
pass
129-
context.close()
130-
131-
for task in asyncio.all_tasks():
132-
assert "_on_route" not in str(task)

0 commit comments

Comments
 (0)