Skip to content

fix: Skip event-triggered state saves after teardown - #2466

Open
Mantisus wants to merge 2 commits into
apify:v2from
Mantisus:recoverable-state-stale-listener
Open

Mantisus wants to merge 2 commits into
apify:v2from
Mantisus:recoverable-state-stale-listener

Conversation

@Mantisus

Copy link
Copy Markdown
Collaborator

Description

Statistics, the session pool, use_state values and other components that save their state on PERSIST_STATE events don't save it anymore once they stop. A save that an event already started finishes before the stop completes, and a save that hasn't started yet is skipped. A record cleared right after the stop, such as the autosaved values of a dropped KeyValueStore, stays cleared.

Saves on PERSIST_STATE events of one component now run one at a time.

Testing

  • Unit tests for a PERSIST_STATE event emitted right before the stop, and for a stop during a save in progress.

✍️ Drafted by Claude Code

@Mantisus Mantisus self-assigned this Nov 17, 2026
@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 17, 2026
@Mantisus
Mantisus requested a review from vdusek November 17, 2026 22:58
@vdusek vdusek changed the title fix: Keep event-triggered state saves from running after teardown fix: Skip event-triggered state saves after teardown Nov 18, 2026
async def _persist_state_on_event(self, event_data: EventPersistStateData) -> None:
# An event emitted before `teardown` can run this listener after it, when the record may already be cleared.
# The lock makes `teardown` wait for a save in progress, and the check skips a save that starts later.
async with self._lock:

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.

Important: This listener now holds self._lock for the whole save, and initialize() takes the same lock before it checks _initialized. KeyValueStore.get_auto_saved_value() calls initialize() on every call, so every use_state() call now waits for a periodic save in progress. With a 1 s set_value, get_auto_saved_value() took 1.96 s here and 0 s on v2. A fast path at the top of initialize() fixes it:

if self._initialized:
    return self.current_value

async with self._lock:
    ...

✍️ Drafted by Claude Code

return

self._event_manager.off(event=Event.PERSIST_STATE, listener=self._persist_state_quietly)
self._event_manager.off(event=Event.PERSIST_STATE, listener=self._persist_state_on_event)

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.

Nit: KeyValueStore.drop() and purge() (#2467) rely on the new guarantee: teardown() waits for an event save in progress, and no event save runs after it returns. The teardown() docstring could say so; now only the comment on the private listener does.

✍️ 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.

I'd keep it in the comment. The teardown() docstring already says it persists the state one last time, so no save follows it. Waiting for a save in progress is how that's kept, and a caller doesn't act on it.

@Mantisus
Mantisus requested a review from vdusek November 18, 2026 13:30

This branch has not been deployed

No deployments
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.

2 participants