Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

anonymous_file: bypass mython for memfd_create (#17386)

rattus54a4e56ad1ca
diff --git a/comfy/anonymous_file.py b/comfy/anonymous_file.py
index f268f96a7..0aa187db8 100644
--- a/comfy/anonymous_file.py
+++ b/comfy/anonymous_file.py
@@ -1,3 +1,4 @@
+import ctypes
import operator
import os
import sys
@@ -5,7 +6,6 @@ import tempfile
import uuid
if os.name == "nt":
- import ctypes
import msvcrt
from ctypes import wintypes
@@ -15,6 +15,11 @@ if os.name == "nt":
_kernel32.DeviceIoControl.restype = wintypes.BOOL
_kernel32.SetFileInformationByHandle.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p, wintypes.DWORD]
_kernel32.SetFileInformationByHandle.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)
+ _libc.memfd_create.argtypes = [ctypes.c_char_p, ctypes.c_uint]
+ _libc.memfd_create.restype = ctypes.c_int
def create_anonymous_file(size=0):
@@ -36,8 +41,11 @@ def create_anonymous_file(size=0):
file = open(path, "x+b", buffering=0,
opener=lambda path, mode: os.open(path, mode | flags, 0o600))
elif sys.platform == "linux":
- file = open("aimdo", "w+b", buffering=0,
- opener=lambda name, mode: os.memfd_create(name, os.MFD_CLOEXEC))
+ fd = _libc.memfd_create(b"aimdo", 1) # MFD_CLOEXEC
+ if fd < 0:
+ errno = ctypes.get_errno()
+ raise OSError(errno, os.strerror(errno))
+ file = open(fd, "w+b", buffering=0)
else:
raise NotImplementedError("anonymous files require Linux or Windows")