kovidgoyal/kitty · error · TypeError

Cannot specify both name and size

Error message

Cannot specify both name and size

What it means

Raised by SharedMemory's __init__ when both a `name` and a `size` are passed. Opening an existing named SHM object takes a name; creating a new one takes a size — doing both is contradictory because the size of an existing object is already fixed. The library enforces this with a TypeError before attempting shm_open.

Source

Thrown at kitty/shm.py:65

    size_fmt = '!I'
    num_bytes_for_size = struct.calcsize(size_fmt)

    def __init__(
        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:

View on GitHub (pinned to 6d5d0c4406)

Solutions

  1. Pass only `name` to open an existing segment, or only `size` to create a new anonymous one
  2. If you want to resize, delete and recreate the segment instead

Example fix

// before
shm = SharedMemory(name='kitty-shm-1', size=4096)
// after
shm = SharedMemory(name='kitty-shm-1')  # existing
# or
shm = SharedMemory(size=4096)  # create new
Defensive patterns

Strategy: validation

Validate before calling

def open_shm(name=None, size=0):
    if name and size:
        raise ValueError('pass either name or size, not both')
    return SharedMemory(name=name, size=size)

Type guard

def is_create_kwargs(kwargs: dict) -> bool:
    return bool(kwargs.get('name')) != bool(kwargs.get('size'))

Try / catch

try:
    shm = SharedMemory(name=n, size=s)
except TypeError as e:
    if 'both name and size' in str(e):
        shm = SharedMemory(name=n)  # fallback: open existing
    else:
        raise

Prevention

When it happens

Trigger: Calling SharedMemory(name='foo', size=4096) — any constructor invocation where both arguments are truthy.

Common situations: Copy-pasting code that opens an existing segment and then adding a size 'to be safe'; refactoring a create call into an open call without removing the size kwarg.

Related errors


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