From 3e9a120adde240ba62abfe78c54ce367e32bfae9 Mon Sep 17 00:00:00 2001 From: Liangsheng Yin Date: Sat, 3 Oct 2026 14:02:24 -0700 Subject: [PATCH] [Fix] Make Hf3fsMockClient reads and writes thread-safe with pread/pwrite (#42425) --- python/sglang/srt/mem_cache/storage/hf3fs/hf3fs_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/sglang/srt/mem_cache/storage/hf3fs/hf3fs_client.py b/python/sglang/srt/mem_cache/storage/hf3fs/hf3fs_client.py index d789a20534..150611b354 100644 --- a/python/sglang/srt/mem_cache/storage/hf3fs/hf3fs_client.py +++ b/python/sglang/srt/mem_cache/storage/hf3fs/hf3fs_client.py @@ -87,8 +87,9 @@ class Hf3fsMockClient(Hf3fsClient): size = tensor.numel() * tensor.itemsize try: - os.lseek(self.file, offset, os.SEEK_SET) - bytes_read = os.read(self.file, size) + # pread/pwrite do not touch the shared fd offset, so concurrent + # reads and writes from different threads cannot interleave. + bytes_read = os.pread(self.file, size, offset) if len(bytes_read) == size: # Convert bytes to tensor and copy to target @@ -122,8 +123,7 @@ class Hf3fsMockClient(Hf3fsClient): tensor_bytes = tensor.contiguous().view(torch.uint8).flatten() data = tensor_bytes.numpy().tobytes() - os.lseek(self.file, offset, os.SEEK_SET) - bytes_written = os.write(self.file, data) + bytes_written = os.pwrite(self.file, data, offset) if bytes_written == size: results.append(size)