{"record":{"id":"d05727c5eff7a1c4","repo":"kovidgoyal/kitty","slug":"failed-to-create-a-uniquely-named-shm-file-try-sh","errorCode":null,"errorMessage":"Failed to create a uniquely named SHM file, try shortening the prefix from: {prefix}","messagePattern":"Failed to create a uniquely named SHM file, try shortening the prefix from: (.+?)","errorType":"exception","errorClass":"OSError","httpStatus":null,"severity":"error","filePath":"kitty/shm.py","lineNumber":84,"sourceCode":"        if not name:\n            flags = os.O_CREAT | os.O_EXCL\n            if not size:\n                raise TypeError(\"'size' must be > 0\")\n        else:\n            flags = 0\n        flags |= os.O_RDONLY if readonly else os.O_RDWR\n\n        tries = 30\n        while not name and tries > 0:\n            tries -= 1\n            q = make_filename(prefix)\n            try:\n                self._fd = shm_open(q, flags, mode)\n                name = q\n            except FileExistsError:\n                continue\n        if tries <= 0:\n            raise OSError(f'Failed to create a uniquely named SHM file, try shortening the prefix from: {prefix}')\n        if self._fd < 0:\n            self._fd = shm_open(name, flags, mode)\n        self._name = name\n        try:\n            if flags & os.O_CREAT and size:\n                if hasattr(os, 'posix_fallocate'):\n                    os.posix_fallocate(self._fd, 0, size)\n                else:\n                    os.ftruncate(self._fd, size)\n            self.stats = os.fstat(self._fd)\n            size = self.stats.st_size\n            self._mmap = mmap.mmap(self._fd, size, access=mmap.ACCESS_READ if readonly else mmap.ACCESS_WRITE)\n        except OSError:\n            self.unlink()\n            raise\n\n        self._size = size\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kitty/shm.py#L66-L102","documentation":"After 30 attempts, the constructor could not find a unique unused name for a new shared memory segment using the given prefix. Each attempt uses shm_open with O_CREAT|O_EXCL and a random suffix; 30 consecutive FileExistsError collisions (or a name-format problem with the prefix) aborts with OSError. Long prefixes are the typical culprit since SHM names have a tight length limit (~31 bytes on macOS, 255 on Linux).","triggerScenarios":"Creating a new segment with a very long `prefix` argument; or a prefix containing '/' characters, which makes shm_open interpret the name as a path and fail repeatedly.","commonSituations":"Using a descriptive prefix like 'my-application-unique-segment-' that exceeds the platform SHM name limit, especially on macOS; embedding path separators in the prefix.","solutions":["Shorten the prefix (a few characters is plenty; randomness provides uniqueness)","Remove any '/' or path-like characters from the prefix","Check /dev/shm (Linux) for leaked segments from previous runs and clean them up"],"exampleFix":"// before\nshm = SharedMemory(prefix='kitty-glfw-shared-memory-segment-')\n// after\nshm = SharedMemory(prefix='kglfw-')","handlingStrategy":"retry","validationCode":"prefix = prefix[:8].replace('/', '-')\nassert '/' not in prefix and len(prefix) <= 16","typeGuard":null,"tryCatchPattern":"for p in ('kx-', 'shm-', 's-'):\n    try:\n        shm = SharedMemory(prefix=p)\n        break\n    except OSError as e:\n        if 'uniquely named' not in str(e):\n            raise\nelse:\n    raise","preventionTips":["Keep prefixes short (<= 8 chars) and free of '/'","Clean up leaked segments in /dev/shm between runs in tests"],"tags":["shared-memory","name-collision","resource-exhaustion"],"backgroundTag":"shm-name-too-long","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}