Skip to content

list_sessions: collapse the per-session N+1 and add exc_info to swallowed-exception logs #629

Description

@tedswinyar

Follow-up to #497, which deferred these two by agreement rather than folding them into a fix PR. Filing so they are not lost now that #497 has merged.

Both live in services/session_service.py and touch the same function, so they are cheap to do together.

1. N+1 in list_sessions

_enrich_session_ownership calls list_terminals_by_session() once per tmux session (session_service.py:132). On a shared cao-server with many sessions and frequent polling (web UI, ops tooling) that is per-session DB churn on a hot read path.

Suggested fix: fetch once via list_all_terminals() and group by tmux_session before enriching.

Raised by @gutosantos82 and by Copilot during the #497 review. Note #497 reduced the subprocess half of this — the persisted working_directory means the pane-cwd fallback now only fires for pre-migration rows — but the per-session query remains.

2. exc_info on the two swallowed-exception logs

session_service.py:134 and :160 log recoverable failures with an f-string and no exc_info, so the traceback is dropped and a DB or backend problem cannot be diagnosed without reproducing locally. The surrounding code deliberately swallows these to keep list_sessions from blanking, which is right — but the diagnostic should survive.

Suggested fix: lazy formatting plus exc_info=True, matching the repo's logging style for recoverable errors.

Raised by Copilot during the #497 review.

Happy to take both if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions