From 5dfb3002ea64f9be0bb66d3df1fe1849a6f5ba57 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Sun, 9 Aug 2026 01:18:48 +0000 Subject: [PATCH] Fix --disable-mmap not bypassing safetensors mmap read load_torch_file only wrapped the tensor returned by safetensors.safe_open()/get_tensor() in a copy when --disable-mmap was set; the mmap-backed read that produces that tensor had already happened by then. On Windows that mmap-backed read of large safetensors files can crash the long-running ComfyUI process with an access violation (#15424). --disable-mmap now reads tensors with plain file I/O so no mmap of the file is created at all. --- comfy/utils.py | 34 ++++++++++++++++--- tests-unit/comfy_test/load_torch_file_test.py | 32 +++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 tests-unit/comfy_test/load_torch_file_test.py diff --git a/comfy/utils.py b/comfy/utils.py index 61c2a22dd..b159844c9 100644 --- a/comfy/utils.py +++ b/comfy/utils.py @@ -119,6 +119,31 @@ def load_safetensors(ckpt): return sd, header.get("__metadata__", {}), +def load_safetensors_no_mmap(ckpt, device): + # safetensors.safe_open()/get_tensor() reads tensor data through an mmap of + # the file. On Windows that mmap-backed read can crash with an access + # violation for large files in a long-running process. Read the tensors + # with plain file I/O instead so no mmap of the file is ever created. + sd = {} + with open(ckpt, "rb") as f: + header_size = struct.unpack("