Skip to content

Commit

Permalink
Merge pull request #366 from johannaengland/bugfix/dump-to-temporary-…
Browse files Browse the repository at this point in the history
…file

Dump Zino state to temporary file and rename afterwards
  • Loading branch information
lunkwill42 authored Sep 3, 2024
2 parents 21f7f7d + f09521c commit fe2bd80
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 fe2bd80

Please sign in to comment.