libtmux icon indicating copy to clipboard operation
libtmux copied to clipboard

KeyError on session_id

Open MaienM opened this issue 5 years ago • 1 comments

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'}]

MaienM avatar Nov 13 '19 06:11 MaienM

I'm also encountering this error when using tmuxp. But there aren't any newlines in my commands.

Here's my session file:

session_name: system
windows:
- window_name: monitoring
  layout: tiled
  panes:
    - sudo htop
- window_name: root
  layout: tiled
  panes:
    - sudo su
- window_name: bluetoothctl
  layout: tiled
  panes:
    - bluetoothctl

The error:

$ tmuxp load ~/.config/tmuxp/sessions/system.yml
[Loading] /home/charles-langlois/.config/tmuxp/sessions/system.yml
Traceback (most recent call last):
  File "/home/charles-langlois/.local/bin/tmuxp", line 8, in <module>
    sys.exit(cli.cli())
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/tmuxp/cli.py", line 814, in command_load
    load_workspace(config[-1], **tmux_options)
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/tmuxp/cli.py", line 536, in load_workspace
    builder.build()  # load tmux session via workspace builder
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/tmuxp/workspacebuilder.py", line 184, in build
    for p, pconf in self.iter_create_panes(w, wconf):
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/tmuxp/workspacebuilder.py", line 297, in iter_create_panes
    p = w.attached_pane
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/libtmux/window.py", line 490, in attached_pane
    for pane in self._panes:
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/libtmux/window.py", line 511, in _panes
    return self._list_panes()
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/libtmux/window.py", line 503, in _list_panes
    panes = [p for p in panes if p['session_id'] == self.get('session_id')]
  File "/home/charles-langlois/.local/lib/python3.7/site-packages/libtmux/window.py", line 503, in <listcomp>
    panes = [p for p in panes if p['session_id'] == self.get('session_id')]
KeyError: 'session_id'

It was working fine before today...

tmuxp debug-info
-------------------------
environment:
	dist: Linux-5.4.7-050407-generic-x86_64-with-Ubuntu-18.04-bionic
	arch: x86_64
	uname: Linux; R90R4K4C; 5.4.7-050407-generic
	version: #201912311234 SMP Tue Dec 31 17:38:50 UTC 2019
-------------------------
python version: 3.7.5 (default, Nov  7 2019, 10:50:52)  [GCC 8.3.0]
system PATH: /home/charles-langlois/bin:/home/charles-langlois/.local/bin:/home/charles-langlois/go/bin:/home/charles-langlois/.local/bin:/home/charles-langlois/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
tmux version: 2.6
libtmux version: 0.8.2
tmuxp version: 1.6.3
tmux path: /usr/bin/tmux
tmuxp path: /home/charles-langlois/.local/lib/python3.7/site-packages/tmuxp/__init__.py
shell: /bin/bash

DrPyser avatar Nov 23 '20 17:11 DrPyser