Skip to content

Commit

Permalink
Dump state to temp file before renaming to correct file
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Sep 3, 2024
1 parent 21f7f7d commit f09521c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/364.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid potential state corruption issues by saving the running state to a temporary file before overwriting the existing state file
5 changes: 4 additions & 1 deletion src/zino/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import logging
import os
from typing import Dict, Optional

from pydantic import BaseModel, Field
Expand Down Expand Up @@ -42,8 +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)
with open(filename, "w") as statefile:
temp_file = f"{filename}.tmp"
with open(temp_file, "w") as statefile:
statefile.write(self.model_dump_json(exclude_none=True, indent=2))
os.replace(src=temp_file, dst=filename)

@classmethod
@log_time_spent()
Expand Down

0 comments on commit f09521c

Please sign in to comment.