From 209d561094d87ab4b916ce68f3ee27c6084e02e9 Mon Sep 17 00:00:00 2001 From: Johanna England Date: Fri, 30 Aug 2024 15:16:04 +0200 Subject: [PATCH] Use copy of state for state dumps to file --- changelog.d/+use-copy-for-statedump.fixed.md | 1 + src/zino/state.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelog.d/+use-copy-for-statedump.fixed.md diff --git a/changelog.d/+use-copy-for-statedump.fixed.md b/changelog.d/+use-copy-for-statedump.fixed.md new file mode 100644 index 00000000..470dedb4 --- /dev/null +++ b/changelog.d/+use-copy-for-statedump.fixed.md @@ -0,0 +1 @@ +Use a copy of the Zino state for saving it to a file to avoid errors due to changes to the state in the middle of saving diff --git a/src/zino/state.py b/src/zino/state.py index 29cd4d8f..bc877a4b 100644 --- a/src/zino/state.py +++ b/src/zino/state.py @@ -42,8 +42,9 @@ class ZinoState(BaseModel): def dump_state_to_file(self, filename: str): """Dumps the full state to a file in JSON format""" _log.debug("dumping state to %s", filename) + copied_state = self.model_copy() with open(filename, "w") as statefile: - statefile.write(self.model_dump_json(exclude_none=True, indent=2)) + statefile.write(copied_state.model_dump_json(exclude_none=True, indent=2)) @classmethod @log_time_spent()