Skip to content

Commit 520610e

Browse files
John de Largentayetony
John de Largentaye
authored andcommitted
Make Server.has_session() use returncode
has_session() would erroneously return true if tmux returned an unexpected string, such as "no current session". Instead of adding yet another string to the list to check against, use the return code of the 'tmux has-session' command, which is documented to return 0 (Shell true) if targeted session exists, and return 1 (Shell false) in any other case.
1 parent 178c1af commit 520610e

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

libtmux/common.py

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def __init__(self, *args, **kwargs):
189189
self.process.stdout.close()
190190
stderr = self.process.stderr.read()
191191
self.process.stderr.close()
192+
returncode = self.process.returncode
192193
except Exception as e:
193194
logger.error(
194195
'Exception for %s: \n%s' % (
@@ -197,6 +198,8 @@ def __init__(self, *args, **kwargs):
197198
)
198199
)
199200

201+
self.returncode = returncode
202+
200203
self.stdout = console_to_str(stdout)
201204
self.stdout = self.stdout.split('\n')
202205
self.stdout = list(filter(None, self.stdout)) # filter empty values

libtmux/server.py

+3-16
Original file line numberDiff line numberDiff line change
@@ -334,24 +334,11 @@ def has_session(self, target_session, exact=True):
334334

335335
proc = self.cmd('has-session', '-t%s' % target_session)
336336

337-
if not proc.stdout:
338-
return True
339-
if any(
340-
x in proc.stdout for x in
341-
['failed to connect to server', 'error connecting to']
342-
):
343-
return False
344-
elif 'no server running' in proc.stdout: # tmux 2.0
345-
return False
346-
elif 'can\'t find session' in proc.stdout: # tmux 2.1
347-
return False
348-
elif 'bad session name' in proc.stdout: # tmux >= 1.9
349-
return False
350-
elif 'session not found' in proc.stdout:
351-
return False
352-
else:
337+
if not proc.returncode:
353338
return True
354339

340+
return False
341+
355342
def kill_server(self):
356343
"""``$ tmux kill-server``."""
357344
self.cmd('kill-server')

0 commit comments

Comments
 (0)