tmux-python / tmux-python/tmuxp

WorkspaceBuilder._session not initialized to None in __init__

Open Beginner friendly
#1,026 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.6k
Forks
248
Avg merge
2h 13m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/tmuxp/workspace/builder.py, especially init, the _session declaration, and the session property around the cited lines. Trace the fresh-session and ObjectDoesNotExist paths, then verify that accessing session before build produces the intended SessionMissingWorkspaceException and that the TODO is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.