From 62696f837ac496dd9212a3ab8c2822ab1fac6da4 Mon Sep 17 00:00:00 2001 From: ememolero <37576277+ememolero@users.noreply.github.com> Date: Wed, 4 Nov 2026 17:49:15 +0100 Subject: [PATCH 1/3] Fix comfy/anonymous_file.py creation on volumes without sparse support FSCTL_SET_SPARSE fails with ERROR_INVALID_FUNCTION on exFAT, so create_anonymous_file raised when the temp directory was on a volume without sparse file support. Query FILE_SUPPORTS_SPARSE_FILES through the file handle and skip the ioctl when the volume lacks it; the file is then fully allocated. Tested on Windows 11 with the temp directory on NTFS and on exFAT. --- comfy/anonymous_file.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/comfy/anonymous_file.py b/comfy/anonymous_file.py index 0aa187db8eb..849692b6734 100644 --- a/comfy/anonymous_file.py +++ b/comfy/anonymous_file.py @@ -15,6 +15,9 @@ _kernel32.DeviceIoControl.restype = wintypes.BOOL _kernel32.SetFileInformationByHandle.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD] _kernel32.SetFileInformationByHandle.restype = wintypes.BOOL + _kernel32.GetVolumeInformationByHandleW.argtypes = [wintypes.HANDLE, wintypes.LPWSTR, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD), + ctypes.POINTER(wintypes.DWORD), ctypes.POINTER(wintypes.DWORD), wintypes.LPWSTR, wintypes.DWORD] + _kernel32.GetVolumeInformationByHandleW.restype = wintypes.BOOL elif sys.platform == "linux": # os.memfd_create is missing from Pythons built against glibc < 2.27. _libc = ctypes.CDLL(None, use_errno=True) @@ -27,9 +30,9 @@ def create_anonymous_file(size=0): Uses Linux memfd or a Windows temporary-cache/delete-on-close file. Windows may still write to disk; Linux memfd may be swapped. - Initial sizing is sparse with zero-filled holes; Windows requires a filesystem - supporting sparse files. Keep the file open through all I/O and close - mappings before closing it. No persistent pathname is provided. + Initial sizing is sparse with zero-filled holes where the filesystem supports + sparse files; otherwise it is fully allocated. Keep the file open through all + I/O and close mappings before closing it. No persistent pathname is provided. """ size = operator.index(size) if size < 0 or size > 0x7fffffffffffffff: @@ -53,9 +56,13 @@ def create_anonymous_file(size=0): if os.name == "nt": handle = msvcrt.get_osfhandle(file.fileno()) transferred = wintypes.DWORD() + fs_flags = wintypes.DWORD() + if not _kernel32.GetVolumeInformationByHandleW(handle, None, 0, None, None, ctypes.byref(fs_flags), None, 0): + raise ctypes.WinError(ctypes.get_last_error()) # FSCTL_SET_SPARSE: unwritten ranges stay unallocated and read as zeros. - if not _kernel32.DeviceIoControl(handle, 0x900c4, None, 0, None, 0, - ctypes.byref(transferred), None): + # FAT32 and exFAT lack FILE_SUPPORTS_SPARSE_FILES (0x40); the file is then fully allocated. + if fs_flags.value & 0x40 and not _kernel32.DeviceIoControl(handle, 0x900c4, None, 0, None, 0, + ctypes.byref(transferred), None): raise ctypes.WinError(ctypes.get_last_error()) # FileEndOfFileInfo avoids the CRT truncate implementation's zero-fill loop. end = ctypes.c_longlong(size) From 4e4bf36a4f87914f83375e09efd608e035884146 Mon Sep 17 00:00:00 2001 From: ememolero <37576277+ememolero@users.noreply.github.com> Date: Thu, 5 Nov 2026 13:49:36 +0100 Subject: [PATCH 2/3] Skip sparse setup when volume flags can't be queried GetVolumeInformationByHandleW is not supported on SMB 3.0 shares. Treat a failed query like a volume without sparse support, so the file is fully allocated instead of raising. Tested with the temp directory on exFAT and NTFS. --- comfy/anonymous_file.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/comfy/anonymous_file.py b/comfy/anonymous_file.py index 849692b6734..f826a8642f4 100644 --- a/comfy/anonymous_file.py +++ b/comfy/anonymous_file.py @@ -57,12 +57,12 @@ def create_anonymous_file(size=0): handle = msvcrt.get_osfhandle(file.fileno()) transferred = wintypes.DWORD() fs_flags = wintypes.DWORD() - if not _kernel32.GetVolumeInformationByHandleW(handle, None, 0, None, None, ctypes.byref(fs_flags), None, 0): - raise ctypes.WinError(ctypes.get_last_error()) + # SMB shares can't report volume flags; treat them like volumes without sparse support. + has_flags = _kernel32.GetVolumeInformationByHandleW(handle, None, 0, None, None, ctypes.byref(fs_flags), None, 0) # FSCTL_SET_SPARSE: unwritten ranges stay unallocated and read as zeros. # FAT32 and exFAT lack FILE_SUPPORTS_SPARSE_FILES (0x40); the file is then fully allocated. - if fs_flags.value & 0x40 and not _kernel32.DeviceIoControl(handle, 0x900c4, None, 0, None, 0, - ctypes.byref(transferred), None): + if has_flags and fs_flags.value & 0x40 and not _kernel32.DeviceIoControl(handle, 0x900c4, None, 0, None, 0, + ctypes.byref(transferred), None): raise ctypes.WinError(ctypes.get_last_error()) # FileEndOfFileInfo avoids the CRT truncate implementation's zero-fill loop. end = ctypes.c_longlong(size) From 26fbc45738d592c9c99c409681a6f42e211027c0 Mon Sep 17 00:00:00 2001 From: ememolero <37576277+ememolero@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:54:01 +0100 Subject: [PATCH 3/3] Ignore FSCTL_SET_SPARSE failures in create_anonymous_file Sparse allocation is only an optimization. Volumes without sparse support (exFAT, FAT32) reject the ioctl; let the file be fully allocated instead of raising. This also keeps SMB shares sparse, where GetVolumeInformationByHandleW is unsupported. Tested with the temp directory on exFAT (falls back, non-sparse) and NTFS (still sparse). --- comfy/anonymous_file.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/comfy/anonymous_file.py b/comfy/anonymous_file.py index f826a8642f4..906f6ccfbc9 100644 --- a/comfy/anonymous_file.py +++ b/comfy/anonymous_file.py @@ -15,9 +15,6 @@ _kernel32.DeviceIoControl.restype = wintypes.BOOL _kernel32.SetFileInformationByHandle.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD] _kernel32.SetFileInformationByHandle.restype = wintypes.BOOL - _kernel32.GetVolumeInformationByHandleW.argtypes = [wintypes.HANDLE, wintypes.LPWSTR, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD), - ctypes.POINTER(wintypes.DWORD), ctypes.POINTER(wintypes.DWORD), wintypes.LPWSTR, wintypes.DWORD] - _kernel32.GetVolumeInformationByHandleW.restype = wintypes.BOOL elif sys.platform == "linux": # os.memfd_create is missing from Pythons built against glibc < 2.27. _libc = ctypes.CDLL(None, use_errno=True) @@ -56,14 +53,9 @@ def create_anonymous_file(size=0): if os.name == "nt": handle = msvcrt.get_osfhandle(file.fileno()) transferred = wintypes.DWORD() - fs_flags = wintypes.DWORD() - # SMB shares can't report volume flags; treat them like volumes without sparse support. - has_flags = _kernel32.GetVolumeInformationByHandleW(handle, None, 0, None, None, ctypes.byref(fs_flags), None, 0) # FSCTL_SET_SPARSE: unwritten ranges stay unallocated and read as zeros. - # FAT32 and exFAT lack FILE_SUPPORTS_SPARSE_FILES (0x40); the file is then fully allocated. - if has_flags and fs_flags.value & 0x40 and not _kernel32.DeviceIoControl(handle, 0x900c4, None, 0, None, 0, - ctypes.byref(transferred), None): - raise ctypes.WinError(ctypes.get_last_error()) + # Not all volumes support it (FAT32, exFAT); the file is then fully allocated. + _kernel32.DeviceIoControl(handle, 0x900c4, None, 0, None, 0, ctypes.byref(transferred), None) # FileEndOfFileInfo avoids the CRT truncate implementation's zero-fill loop. end = ctypes.c_longlong(size) if not _kernel32.SetFileInformationByHandle(handle, 6,