From d40bdcb1ff409828d94e14b1658da94d5d0237c3 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 062201b8..82580c33 100644 --- a/src/zino/state.py +++ b/src/zino/state.py @@ -43,9 +43,10 @@ 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() temp_file = f"{filename}.tmp" with open(temp_file, "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)) os.replace(src=temp_file, dst=filename) @classmethod