Repository navigation
Conversation
| 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: |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Description
Statistics, the session pool,
use_statevalues and other components that save their state onPERSIST_STATEevents 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 droppedKeyValueStore, stays cleared.Saves on
PERSIST_STATEevents of one component now run one at a time.Testing
PERSIST_STATEevent emitted right before the stop, and for a stop during a save in progress.✍️ Drafted by Claude Code