{"record":{"id":"0f9b606b09da2989","repo":"kovidgoyal/kitty","slug":"size-must-be-0","errorCode":null,"errorMessage":"'size' must be > 0","messagePattern":"'size' must be > 0","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"kitty/shm.py","lineNumber":69,"sourceCode":"        self,\n        name: str = '',\n        size: int = 0,\n        readonly: bool = False,\n        mode: int = stat.S_IREAD | stat.S_IWRITE,\n        prefix: str = 'kitty-',\n        unlink_on_exit: bool = False,\n        ignore_close_failure: bool = False,\n    ):\n        self.unlink_on_exit = unlink_on_exit\n        self.ignore_close_failure = ignore_close_failure\n        if size < 0:\n            raise TypeError(\"'size' must be a non-negative integer\")\n        if size and name:\n            raise TypeError('Cannot specify both name and size')\n        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","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kitty/shm.py#L51-L87","documentation":"Raised when constructing a new shared memory object without a name and with size == 0 (or omitted). Creating a segment requires a positive byte count to fallocate; without a name the constructor is in create mode and size is mandatory. A zero/missing size is rejected with TypeError before shm_open is attempted.","triggerScenarios":"SharedMemory() with no arguments, or SharedMemory(size=0). Note size<0 raises a different error first; exactly 0 with no name triggers this one.","commonSituations":"Computing size from a variable that evaluates to 0 (empty payload, len() of empty data); forgetting the size kwarg entirely when intending to create.","solutions":["Pass a positive size, e.g. SharedMemory(size=4096)","If size is computed, guard it: size = max(size, 1) or raise your own error when the payload is empty","If you meant to open an existing object, pass its name instead"],"exampleFix":"// before\nshm = SharedMemory(size=len(payload))  # len(payload) == 0\n// after\nshm = SharedMemory(size=max(1, len(payload)))","handlingStrategy":"validation","validationCode":"if creating and (size is None or size <= 0):\n    raise ValueError('size must be positive when creating a segment')","typeGuard":"def valid_create_size(size: int | None) -> bool:\n    return isinstance(size, int) and size > 0","tryCatchPattern":"try:\n    shm = SharedMemory(size=size)\nexcept TypeError as e:\n    if \"must be > 0\" in str(e):\n        shm = SharedMemory(size=4096)\n    else:\n        raise","preventionTips":["Never pass a computed size without checking it is >= 1","Treat empty payload as an application-level error before creating the segment"],"tags":["shared-memory","constructor","zero-size"],"backgroundTag":"zero-size-allocation","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}