Skip to content

Commit 76ba018

Browse files
ZJY0516ProExpertProg
authored andcommitted
[Misc] fix unique_filepath (vllm-project#25732)
Signed-off-by: zjy0516 <riverclouds.zhu@qq.com> Co-authored-by: Luka Govedič <ProExpertProg@users.noreply.github.com> Signed-off-by: xuebwang-amd <xuebwang@amd.com>
1 parent 19f5d35 commit 76ba018

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

tests/utils_/test_utils.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
from vllm.config import ParallelConfig, VllmConfig, set_current_vllm_config
2424
from vllm.transformers_utils.detokenizer_utils import (
2525
convert_ids_list_to_tokens)
26-
from vllm.utils import (CacheInfo, FlexibleArgumentParser, LRUCache,
27-
MemorySnapshot, PlaceholderModule, StoreBoolean,
28-
bind_kv_cache, common_broadcastable_dtype,
29-
current_stream, deprecate_kwargs, get_open_port,
30-
get_tcp_uri, is_lossless_cast, join_host_port,
31-
make_zmq_path, make_zmq_socket, memory_profiling,
32-
merge_async_iterators, sha256, split_host_port,
33-
split_zmq_path, supports_kw, swap_dict_values)
3426

27+
# isort: off
28+
from vllm.utils import (
29+
CacheInfo, FlexibleArgumentParser, LRUCache, MemorySnapshot,
30+
PlaceholderModule, bind_kv_cache, common_broadcastable_dtype,
31+
current_stream, deprecate_kwargs, get_open_port, get_tcp_uri,
32+
is_lossless_cast, join_host_port, make_zmq_path, make_zmq_socket,
33+
memory_profiling, merge_async_iterators, sha256, split_host_port,
34+
split_zmq_path, supports_kw, swap_dict_values, unique_filepath)
35+
# isort: on
3536
from ..utils import create_new_process_for_each_test, error_on_warning
3637

3738

@@ -1032,3 +1033,15 @@ def test_load_config_file(tmp_path):
10321033
# Assert that the processed arguments match the expected output
10331034
assert processed_args == expected_args
10341035
os.remove(str(config_file_path))
1036+
1037+
1038+
def test_unique_filepath():
1039+
temp_dir = tempfile.mkdtemp()
1040+
path_fn = lambda i: Path(temp_dir) / f"file_{i}.txt"
1041+
paths = set()
1042+
for i in range(10):
1043+
path = unique_filepath(path_fn)
1044+
path.write_text("test")
1045+
paths.add(path)
1046+
assert len(paths) == 10
1047+
assert len(list(Path(temp_dir).glob("*.txt"))) == 10

vllm/utils/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from concurrent.futures.process import ProcessPoolExecutor
4646
from dataclasses import dataclass, field
4747
from functools import cache, lru_cache, partial, wraps
48+
from pathlib import Path
4849
from types import MappingProxyType
4950
from typing import (TYPE_CHECKING, Any, Callable, Generic, Literal, NamedTuple,
5051
Optional, TextIO, TypeVar, Union, cast, overload)
@@ -3536,3 +3537,23 @@ def set_env_var(key, value):
35363537
del os.environ[key]
35373538
else:
35383539
os.environ[key] = old
3540+
3541+
3542+
def unique_filepath(fn: Callable[[int], Path]) -> Path:
3543+
"""
3544+
unique_filepath returns a unique path by trying
3545+
to include an integer in increasing order.
3546+
3547+
fn should be a callable that returns a path that
3548+
includes the passed int at a fixed location.
3549+
3550+
Note: This function has a TOCTOU race condition.
3551+
Caller should use atomic operations (e.g., open with 'x' mode)
3552+
when creating the file to ensure thread safety.
3553+
"""
3554+
i = 0
3555+
while True:
3556+
p = fn(i)
3557+
if not p.exists():
3558+
return p
3559+
i += 1

0 commit comments

Comments
 (0)