Skip to content

Commit 1254d4d

Browse files
authored
In rare cases where os.replace fails, retry using shutil.move. (#8589)
* In rare cases where os.replace() fails, retry using shutil.move. Seen on some Windows installs. * Catch the PermissionError specifically
1 parent a70082f commit 1254d4d

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

chia/util/config.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import os
3+
import shutil
34
import sys
45
from pathlib import Path
56
from typing import Any, Callable, Dict, Optional, Union
@@ -22,7 +23,10 @@ def create_default_chia_config(root_path: Path, filenames=["config.yaml"]) -> No
2223
mkdir(path.parent)
2324
with open(tmp_path, "w") as f:
2425
f.write(default_config_file_data)
25-
os.replace(str(tmp_path), str(path))
26+
try:
27+
os.replace(str(tmp_path), str(path))
28+
except PermissionError:
29+
shutil.move(str(tmp_path), str(path))
2630

2731

2832
def config_path_for_filename(root_path: Path, filename: Union[str, Path]) -> Path:
@@ -37,7 +41,10 @@ def save_config(root_path: Path, filename: Union[str, Path], config_data: Any):
3741
tmp_path: Path = path.with_suffix("." + str(os.getpid()))
3842
with open(tmp_path, "w") as f:
3943
yaml.safe_dump(config_data, f)
40-
os.replace(str(tmp_path), path)
44+
try:
45+
os.replace(str(tmp_path), path)
46+
except PermissionError:
47+
shutil.move(str(tmp_path), str(path))
4148

4249

4350
def load_config(

chia/util/file_keyring.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import fasteners
33
import os
4+
import shutil
45
import sys
56
import threading
67
import yaml
@@ -432,7 +433,10 @@ def write_data_to_keyring(self, data):
432433
temp_path: Path = self.keyring_path.with_suffix("." + str(os.getpid()))
433434
with open(os.open(str(temp_path), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600), "w") as f:
434435
_ = yaml.safe_dump(data, f)
435-
os.replace(str(temp_path), self.keyring_path)
436+
try:
437+
os.replace(str(temp_path), self.keyring_path)
438+
except PermissionError:
439+
shutil.move(str(temp_path), str(self.keyring_path))
436440

437441
def prepare_for_migration(self):
438442
if not self.payload_cache:

0 commit comments

Comments
 (0)