Skip to content

fix: Defer page object cleanup to make it accessible in error handlers#1814

Open
janbuchar wants to merge 1 commit intomasterfrom
deferred-page-object-cleanup
Open

fix: Defer page object cleanup to make it accessible in error handlers#1814
janbuchar wants to merge 1 commit intomasterfrom
deferred-page-object-cleanup

Conversation

@janbuchar
Copy link
Copy Markdown
Collaborator

@janbuchar janbuchar added the t-tooling Issues with this label are in the ownership of the tooling team. label Mar 27, 2026
@janbuchar janbuchar requested review from Pijukatel and vdusek March 27, 2026 14:42
@github-actions github-actions bot added this to the 137th sprint - Tooling team milestone Mar 27, 2026
@github-actions github-actions bot added the tested Temporary label used only programatically for some analytics. label Mar 27, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 27, 2026

Codecov Report

❌ Patch coverage is 87.87879% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.26%. Comparing base (6f2ac13) to head (e220649).
⚠️ Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
src/crawlee/crawlers/_basic/_basic_crawler.py 66.66% 2 Missing ⚠️
...rawlee/crawlers/_playwright/_playwright_crawler.py 92.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1814   +/-   ##
=======================================
  Coverage   92.25%   92.26%           
=======================================
  Files         157      157           
  Lines       10847    10895   +48     
=======================================
+ Hits        10007    10052   +45     
- Misses        840      843    +3     
Flag Coverage Δ
unit 92.26% <87.87%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Collaborator

@vdusek vdusek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments/questions, otherwise LGTM

for cleanup in deferred_cleanup:
try:
await cleanup()
except Exception: # noqa: PERF203
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we put the entire loop into the try-except block as the lint rule suggests?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean that if one cleanup hook fails, all those that follow it will, too. I wouldn't worry about performance too much here, there won't be more than one for 99% of users.

Comment on lines +306 to +307
with contextlib.suppress(Exception):
await context.page.__aexit__(None, None, None)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really suppress all exceptions completely? Maybe we could at least log them? In the BasicCrawler cleanup loop, we're already catching and logging exceptions, so we could even let them propagate to that level.

Just an idea, maybe there is a good reason to completely suppress all exceptions on this level.

log: logging.Logger
"""Logger instance."""

register_deferred_cleanup: Callable[[Callable[[], Coroutine[None, None, None]]], None]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is huge, maybe we could utilize a type alias DeferredCleanupCallback?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PlaywrightCrawler error_handler cannot access Page object

3 participants