tmux-python / tmux-python/libtmux

KeyError on session_id

Open
#240 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I ran into the issue described in #36, and I've managed to track down the cause/a reproduction scenario. I initially posted this as a reply to that issue, but later noticed that it is preferred to create a new issue instead (according to the stale bot), so I've moved it here.

I started a new tmux server instance, and from the first window, I launched a new-window with a command with a newline in it: tmux new-window -t :2 -k $'echo foo\nread'. I then switched back to the first window, and ran the following in Python:

>>> import libtmux
>>> libtmux.__version__
'0.8.2'
>>> tmux = libtmux.Server()
>>> tmux.find_where({'session_name': '0'}).find_where({'window_name': 'python'}).attached_pane
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/maienm/.asdf/installs/python/utils/lib/python3.8/site-packages/libtmux/window.py", line 490, in attached_pane
    for pane in self._panes:
  File "/home/maienm/.asdf/installs/python/utils/lib/python3.8/site-packages/libtmux/window.py", line 514, in _panes
    return self._list_panes()
  File "/home/maienm/.asdf/installs/python/utils/lib/python3.8/site-packages/libtmux/window.py", line 506, in _list_panes
    panes = [p for p in panes if p['session_id'] == self.get('session_id')]
  File "/home/maienm/.asdf/installs/python/utils/lib/python3.8/site-packages/libtmux/window.py", line 506, in <listcomp>
    panes = [p for p in panes if p['session_id'] == self.get('session_id')]
KeyError: 'session_id'
>>> tmux._update_panes()._panes
[{'session_name': '0', 'session_id': '$0', 'window_index': '1', 'window_id': '@0', 'window_name': 'python', 'history_size': '137', 'history_limit': '100000', 'history_bytes': '100636', 'pane_index': '1', 'pane_width': '240', 'pane_height': '66', 'pane_title': 'HOSTNAME', 'pane_id': '%0', 'pane_active': '1', 'pane_dead': '0', 'pane_in_mode': '0', 'pane_synchronized': '0', 'pane_tty': '/dev/pts/41', 'pane_pid': '337481', 'pane_current_path': '/home/maienm', 'pane_current_command': 'python', 'cursor_x': '0', 'cursor_y': '65', 'scroll_region_upper': '0', 'scroll_region_lower': '65', 'alternate_on': '0', 'alternate_saved_x': '0', 'alternate_saved_y': '0', 'cursor_flag': '1', 'insert_flag': '0', 'keypad_cursor_flag': '0', 'keypad_flag': '0', 'wrap_flag': '1', 'mouse_standard_flag': '0', 'mouse_button_flag': '0', 'mouse_any_flag': '0'}, {'session_name': '0', 'session_id': '$0', 'window_index': '2', 'window_id': '@2', 'window_name': 'zsh', 'history_size': '0', 'history_limit': '100000', 'history_bytes': '0', 'pane_index': '1', 'pane_width': '240', 'pane_height': '66', 'pane_title': 'HOSTNAME', 'pane_id': '%2', 'pane_active': '1', 'pane_dead': '0', 'pane_in_mode': '0', 'pane_synchronized': '0', 'pane_tty': '/dev/pts/42', 'pane_pid': '338918', 'pane_start_command': 'echo foo'}, {'session_name': 'read', 'window_index': '/home/maienm', 'window_id': 'zsh', 'window_name': '0', 'history_size': '1', 'history_limit': '0', 'history_bytes': '65', 'pane_height': '0', 'pane_title': '0', 'pane_id': '0', 'pane_active': '1', 'pane_dead': '0', 'pane_in_mode': '0', 'pane_synchronized': '0', 'pane_tty': '1', 'pane_pid': '0', 'pane_start_command': '0', 'pane_start_path': '0', 'pane_current_path': ''}]

It looks like the newline in pane_start_command causes the information for the second window to be broken into two dictionaries, each with part of the information.

I also tried this with a directory that contains a newline, which didn't result in an error, but did result in two panes with incorrect information (there was only one pane in this case):

[{'session_name': '0', 'session_id': '$0', 'window_index': '1', 'window_id': '@0', 'window_name': 'python', 'history_size': '3', 'history_limit': '100000', 'history_bytes': '2832', 'pane_index': '1', 'pane_width': '240', 'pane_height': '66', 'pane_title': 'HOSTNAME', 'pane_id': '%0', 'pane_active': '1', 'pane_dead': '0', 'pane_in_mode': '0', 'pane_synchronized': '0', 'pane_tty': '/dev/pts/41', 'pane_pid': '18834', 'pane_current_path': '/home/maienm/foo'}, {'session_name': 'bar', 'session_id': 'python', 'window_index': '0', 'window_id': '10', 'window_name': '0', 'history_size': '65', 'pane_index': '0', 'pane_width': '0', 'pane_height': '0', 'pane_title': '1', 'pane_id': '0', 'pane_active': '0', 'pane_dead': '0', 'pane_in_mode': '1', 'pane_synchronized': '0', 'pane_tty': '0', 'pane_pid': '0'}]

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 libtmux/window.py, especially _list_panes and the _update_panes()._panes path, and reproduce the issue with a tmux command containing a newline in pane_start_command. Check how the resulting pane records are parsed and filtered; done means the reproduction no longer raises KeyError and records are not split into incorrect dictionaries.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.