kovidgoyal/kitty · error · TypeError

'size' must be > 0

Error message

'size' must be > 0

What it means

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.

Source

Thrown at kitty/shm.py:69

        self,
        name: str = '',
        size: int = 0,
        readonly: bool = False,
        mode: int = stat.S_IREAD | stat.S_IWRITE,
        prefix: str = 'kitty-',
        unlink_on_exit: bool = False,
        ignore_close_failure: bool = False,
    ):
        self.unlink_on_exit = unlink_on_exit
        self.ignore_close_failure = ignore_close_failure
        if size < 0:
            raise TypeError("'size' must be a non-negative integer")
        if size and name:
            raise TypeError('Cannot specify both name and size')
        if not name:
            flags = os.O_CREAT | os.O_EXCL
            if not size:
                raise TypeError("'size' must be > 0")
        else:
            flags = 0
        flags |= os.O_RDONLY if readonly else os.O_RDWR

        tries = 30
        while not name and tries > 0:
            tries -= 1
            q = make_filename(prefix)
            try:
                self._fd = shm_open(q, flags, mode)
                name = q
            except FileExistsError:
                continue
        if tries <= 0:
            raise OSError(f'Failed to create a uniquely named SHM file, try shortening the prefix from: {prefix}')
        if self._fd < 0:
            self._fd = shm_open(name, flags, mode)
        self._name = name

View on GitHub (pinned to 6d5d0c4406)

Solutions

  1. Pass a positive size, e.g. SharedMemory(size=4096)
  2. If size is computed, guard it: size = max(size, 1) or raise your own error when the payload is empty
  3. If you meant to open an existing object, pass its name instead

Example fix

// before
shm = SharedMemory(size=len(payload))  # len(payload) == 0
// after
shm = SharedMemory(size=max(1, len(payload)))
Defensive patterns

Strategy: validation

Validate before calling

if creating and (size is None or size <= 0):
    raise ValueError('size must be positive when creating a segment')

Type guard

def valid_create_size(size: int | None) -> bool:
    return isinstance(size, int) and size > 0

Try / catch

try:
    shm = SharedMemory(size=size)
except TypeError as e:
    if "must be > 0" in str(e):
        shm = SharedMemory(size=4096)
    else:
        raise

Prevention

When it happens

Trigger: SharedMemory() with no arguments, or SharedMemory(size=0). Note size<0 raises a different error first; exactly 0 with no name triggers this one.

Common situations: Computing size from a variable that evaluates to 0 (empty payload, len() of empty data); forgetting the size kwarg entirely when intending to create.

Related errors


AI-assisted analysis of kovidgoyal/kitty@6d5d0c4406 (2026-08-27). Data as JSON: /api/errors/0f9b606b09da2989. Report an issue: GitHub.