Repository navigation
fix: Skip event-triggered state saves after teardown #2466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -115,6 +115,10 @@ async def initialize(self) -> TStateModel: | |
| ValidationError: If the persisted record is invalid. The record is kept, and every call raises again until | ||
| it's fixed or cleared by `reset_store`. | ||
| """ | ||
| # Checked without the lock first, so that a call on an initialized state doesn't wait for a save in progress. | ||
| if self._initialized: | ||
| return self.current_value | ||
|
|
||
| async with self._lock: | ||
| if self._initialized: | ||
| return self.current_value | ||
|
|
@@ -124,7 +128,7 @@ async def initialize(self) -> TStateModel: | |
|
|
||
| # Registered only after a successful load, so that a failed one leaves no listener behind. | ||
| if self._persistence_enabled: | ||
| self._event_manager.on(event=Event.PERSIST_STATE, listener=self._persist_state_quietly) | ||
| self._event_manager.on(event=Event.PERSIST_STATE, listener=self._persist_state_on_event) | ||
|
|
||
| self._initialized = True | ||
| return self.current_value | ||
|
|
@@ -142,7 +146,7 @@ async def teardown(self) -> None: | |
| if not self._persistence_enabled: | ||
| 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) | ||
| await self._persist_state_quietly() | ||
|
|
||
| @property | ||
|
|
@@ -228,6 +232,15 @@ def _event_manager(self) -> EventManager: | |
|
|
||
| return service_locator.get_event_manager() | ||
|
|
||
| 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: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Important: This listener now holds if self._initialized:
return self.current_value
async with self._lock:
...✍️ Drafted by Claude Code |
||
| if not self._initialized: | ||
| return | ||
|
|
||
| await self._persist_state_quietly(event_data) | ||
|
|
||
| async def _persist_state_quietly(self, event_data: EventPersistStateData | None = None) -> None: | ||
| try: | ||
| await self.persist_state(event_data) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
KeyValueStore.drop()andpurge()(#2467) rely on the new guarantee:teardown()waits for an event save in progress, and no event save runs after it returns. Theteardown()docstring could say so; now only the comment on the private listener does.✍️ Drafted by Claude Code
There was a problem hiding this comment.
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.