Skip to content

Commit e250b0b

Browse files
authored
Merge pull request #3305 from mdboom/atomic-write-notebook-json
Write notebook.json file atomically
2 parents 364cd03 + dd5778b commit e250b0b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

notebook/config_manager.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,16 @@ def set(self, section_name, data):
9191
filename = self.file_name(section_name)
9292
self.ensure_config_dir_exists()
9393

94+
# Generate the JSON up front, since it could raise an exception,
95+
# in order to avoid writing half-finished corrupted data to disk.
96+
json_content = json.dumps(data, indent=2)
97+
9498
if PY3:
9599
f = io.open(filename, 'w', encoding='utf-8')
96100
else:
97101
f = open(filename, 'wb')
98102
with f:
99-
json.dump(data, f, indent=2)
103+
f.write(json_content)
100104

101105
def update(self, section_name, new_data):
102106
"""Modify the config section by recursively updating it with new_data.

0 commit comments

Comments
 (0)