Skip to content

WorkspaceBuilder._session not initialized to None in __init__ #1026

@tony

Description

@tony

Bug

WorkspaceBuilder._session is declared as a class-level type annotation (_session: Session | None at line 308 of src/tmuxp/workspace/builder.py) but is never initialized to None in __init__.

Problem

The attribute is only assigned conditionally inside __init__ (lines 377-387):

if self.server is not None and self.session_exists(
    session_name=self.session_config["session_name"],
):
    try:
        session = self.server.sessions.get(
            session_name=self.session_config["session_name"],
        )
        assert session is not None
        self._session = session
    except ObjectDoesNotExist:
        pass

If the session doesn't already exist (the normal case for a fresh tmuxp load), or if ObjectDoesNotExist is raised, _session is never set.

The session property (lines 389-394) expects _session to exist:

@property
def session(self) -> Session:
    if self._session is None:
        raise exc.SessionMissingWorkspaceException
    return self._session

Accessing self._session when it was never assigned raises AttributeError, not the intended SessionMissingWorkspaceException.

Why it hasn't crashed in practice

build() always assigns self._session before the property is accessed. But if any code path accesses self.session before build() completes, it gets AttributeError instead of the semantically correct exception.

Fix

Add self._session = None early in __init__, before the conditional block. Also resolve the existing TODO at lines 357-358:

TODO: Initialize :class:`libtmux.Session` from here, in ``self.session``.

Files

  • src/tmuxp/workspace/builder.py lines 308, 360-387, 389-394

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions