Skip to content

Commit fe72041

Browse files
committed
refactor(Server): (attached_)sessions return list if server off
1 parent 399f2e6 commit fe72041

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/libtmux/server.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ def list_sessions(self) -> t.List[Session]:
207207
@property
208208
def sessions(self) -> t.List[Session]:
209209
"""Property / alias to return :meth:`~.list_sessions`."""
210-
return self.list_sessions()
210+
try:
211+
return self.list_sessions()
212+
except Exception:
213+
return []
211214

212215
#: Alias :attr:`sessions` for :class:`~libtmux.common.TmuxRelationalObject`
213216
children = sessions # type: ignore
@@ -348,7 +351,7 @@ def _update_panes(self) -> "Server":
348351
return self
349352

350353
@property
351-
def attached_sessions(self) -> t.Optional[t.List[Session]]:
354+
def attached_sessions(self) -> t.List[Session]:
352355
"""
353356
Return active :class:`Session` objects.
354357
@@ -357,19 +360,22 @@ def attached_sessions(self) -> t.Optional[t.List[Session]]:
357360
list of :class:`Session`
358361
"""
359362

360-
sessions = self._sessions
361-
attached_sessions = list()
363+
try:
364+
sessions = self._sessions
365+
attached_sessions = list()
362366

363-
for session in sessions:
364-
attached = session.get("session_attached")
365-
# for now session_active is a unicode
366-
if attached != "0":
367-
logger.debug("session %s attached", session.get("name"))
368-
attached_sessions.append(session)
369-
else:
370-
continue
367+
for session in sessions:
368+
attached = session.get("session_attached")
369+
# for now session_active is a unicode
370+
if attached != "0":
371+
logger.debug("session %s attached", session.get("name"))
372+
attached_sessions.append(session)
373+
else:
374+
continue
371375

372-
return [Session(server=self, **s) for s in attached_sessions] or None
376+
return [Session(server=self, **s) for s in attached_sessions] or []
377+
except Exception:
378+
return []
373379

374380
def has_session(self, target_session: str, exact: bool = True) -> bool:
375381
"""

0 commit comments

Comments
 (0)