Skip to content

Commit 1269d56

Browse files
committed
fix: file contention when multiple libero instances init
When multiple libero instances run for the first time, they all try to create the config directory and file, causing contention. This commit adds a file lock to prevent contention Signed-off-by: Hao Lin <[email protected]>
1 parent 811c049 commit 1269d56

File tree

1 file changed

+11
-29
lines changed

1 file changed

+11
-29
lines changed

libero/libero/__init__.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
import yaml
3+
from filelock import FileLock
34

45
# This is a default path for localizing all the benchmark related files
56
libero_config_path = os.environ.get(
67
"LIBERO_CONFIG_PATH", os.path.expanduser("~/.libero")
78
)
89
config_file = os.path.join(libero_config_path, "config.yaml")
10+
config_file_lock = os.path.join(libero_config_path, ".config_lock")
911

1012

1113
def get_default_path_dict(custom_location=None):
@@ -52,45 +54,25 @@ def get_libero_path(query_key):
5254

5355
def set_libero_default_path(custom_location=os.path.dirname(os.path.abspath(__file__))):
5456
print(
55-
f"[Warning] You are changing the default path for Libero config. This will affect all the paths in the config file."
57+
"[Warning] You are changing the default path for Libero config. This will affect all the paths in the config file."
5658
)
5759
new_config = get_default_path_dict(custom_location)
5860
with open(config_file, "w") as f:
5961
yaml.dump(new_config, f)
6062

61-
62-
if not os.path.exists(libero_config_path):
63-
os.makedirs(libero_config_path)
63+
with FileLock(config_file_lock):
64+
if not os.path.exists(libero_config_path):
65+
os.makedirs(libero_config_path)
6466

6567
if not os.path.exists(config_file):
6668
# Create a default config file
6769

6870
default_path_dict = get_default_path_dict()
69-
answer = input(
70-
"Do you want to specify a custom path for the dataset folder? (Y/N): "
71-
).lower()
72-
if answer == "y":
73-
# If the user wants to specify a custom storage path, prompt them to enter it
74-
custom_dataset_path = input(
75-
"Enter the path where you want to store the datasets: "
76-
)
77-
full_custom_dataset_path = os.path.join(
78-
os.path.abspath(os.path.expanduser(custom_dataset_path)), "datasets"
79-
)
80-
# Check if the custom storage path exists, and create if it doesn't
81-
82-
print("The full path of the custom storage path you entered is:")
83-
print(full_custom_dataset_path)
84-
print("Do you want to continue? (Y/N)")
85-
confirm_answer = input().lower()
86-
if confirm_answer == "y":
87-
if not os.path.exists(full_custom_dataset_path):
88-
os.makedirs(full_custom_dataset_path)
89-
default_path_dict["datasets"] = full_custom_dataset_path
9071
print("Initializing the default config file...")
9172
print(f"The following information is stored in the config file: {config_file}")
9273
# write all the paths into a yaml file
93-
with open(config_file, "w") as f:
94-
yaml.dump(default_path_dict, f)
95-
for key, value in default_path_dict.items():
96-
print(f"{key}: {value}")
74+
with FileLock(config_file_lock):
75+
with open(config_file, "w") as f:
76+
yaml.dump(default_path_dict, f)
77+
for key, value in default_path_dict.items():
78+
print(f"{key}: {value}")

0 commit comments

Comments
 (0)