Skip to content

feat!: Persist sessions, statistics and sitemap state by default - #2539

Merged
vdusek merged 2 commits into
apify:v2from
Mantisus:default-persistence-v2
Nov 30, 2026
Merged

vdusek merged 2 commits into
apify:v2from
Mantisus:default-persistence-v2

Conversation

@Mantisus

@Mantisus Mantisus commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Statistics, SessionPool, DefaultRenderingTypePredictor and SitemapRequestLoader persist their state by default. The session pool and the predictor that a crawler creates, and the default statistics of AdaptivePlaywrightCrawler, persist to the crawler's key-value store, like its default statistics. For that, SessionPool and DefaultRenderingTypePredictor take a new persist_state_kvs_factory. The default statistics of AdaptivePlaywrightCrawler now also get the id you pass to the crawler, the same as those of the other crawlers. SitemapRequestLoader stores its state under CRAWLEE_SITEMAP_REQUEST_LOADER_STATE unless it gets a persist_state_key, and persistence_enabled=False turns persistence off.

Merge after #2536 reaches v2, which fixes restoring the predictor's state.

Issues

Testing

  • Unit tests that each component persists by default, that the session pool, the predictor and the adaptive statistics a crawler creates persist to the crawler's key-value store, that the adaptive statistics take the crawler's id, and that the statistics of the sub crawlers don't persist.

✍️ Drafted by Claude Code

@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 28, 2026
@Mantisus Mantisus self-assigned this Nov 28, 2026
@Mantisus
Mantisus requested a review from vdusek November 28, 2026 12:52
@Mantisus Mantisus changed the title persist state by default feat!: Persist state by default Nov 28, 2026
@vdusek vdusek linked an issue Nov 29, 2026 that may be closed by this pull request
@vdusek vdusek changed the title feat!: Persist state by default feat!: Persist sessions, statistics and sitemap state by default Nov 29, 2026
Comment thread src/crawlee/statistics/_statistics.py Resolved
Comment thread src/crawlee/sessions/_session_pool.py Resolved
max_buffer_size: int = 200,
persist_state_key: str | None = None,
persistence_enabled: bool = True,
persist_state_key: str = 'CRAWLEE_SITEMAP_REQUEST_LOADER_STATE',

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.

Suggestion: persist_state_key=None meant no persistence in v1. Now it reaches the storage layer, and persist_state() fails with TypeError: quote_from_bytes() expected bytes, which doesn't point to the cause. Raise a ValueError for None that points to persistence_enabled=False.

✍️ Drafted by Claude Code

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.

It's covered in the upgrade guide, and a type checker flags the wrong type right away. I don't think it's worth adding a runtime error.

self._request_manager = request_manager
self._session_pool = self._own_or_borrow(session_pool, SessionPool)
self._session_pool = self._own_or_borrow(
session_pool, lambda: SessionPool(persist_state_kvs_factory=self.get_key_value_store)

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.

Note: _pause_by_event (_basic_crawler.py:1064) saves only the statistics after the migration pause. Session changes made by requests that finish during the pause are lost, and every crawler's pool now persists. Crawlee JS has the same gap, so this can be a follow-up issue in both repos.

✍️ Drafted by Claude Code

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.

Yes, let's do this in a separate PR.

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.

Opened #2552 for it.

✍️ Drafted by Claude Code

@vdusek vdusek left a comment

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.

LGTM

@vdusek
vdusek merged commit 43eb24e into apify:v2 Nov 30, 2026
42 checks passed
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable SessionPool persistence by default

2 participants