capture_pane hangs with `-S-`
I'm trying to expand the capture_pane functionality.
I'm using libtmux master on python3.7 with tmux 3.0 -
The following snippet hangs on some panes, while the same command works when run from bash;
>>> import libtmux; s = libtmux.Server(); s.children[0].children[0].children[1].cmd("capture-pane", '-p', '-J', '-e', '-E-', '-S-').stdout
<omitted command noise>
DEBUG: ['/etc/profiles/per-user/myuser/bin/tmux', 'capture-pane', '-t', '%4', '-p', '-J', '-e', '-E-', '-S-']
<no output till ^C>
(I added some simple print debugging.)
list-panes gives the following:
$ tmux list-panes -t 0:
0: [135x17] [history 1952/2000, 939964 bytes] %0
1: [135x17] [history 928/2000, 431099 bytes] %4
2: [135x21] [history 1869/2000, 1194568 bytes] %5
3: [135x21] [history 0/2000, 0 bytes] %7
4: [271x13] [history 1816/2000, 269415 bytes] %1 (active)
5: [271x12] [history 6/2000, 1537 bytes] %132
The hang only seems to happen if I pass the -S- argument.
P.S.: previously at https://github.com/tmux-python/tmuxp/issues/529
I took a run at it with the debugger and it seems to hang in https://github.com/tmux-python/libtmux/blob/1331743bcd4ecd6f952ee056570d1462734678b9/libtmux/common.py#L208
Is this possibly the problem? https://stackoverflow.com/questions/1445627/how-can-i-find-out-why-subprocess-popen-wait-waits-forever-if-stdout-pipe
Some more stuff https://stackoverflow.com/questions/30982217/python-popen-wait-vs-communicate-vs-calledprocesserror
The following patch appears to work
diff --git a/libtmux/common.py b/libtmux/common.py
index 9681d3e..64f4313 100644
--- a/libtmux/common.py
+++ b/libtmux/common.py
@@ -205,11 +205,7 @@ class tmux_cmd(object):
self.process = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
- self.process.wait()
- stdout = self.process.stdout.read()
- self.process.stdout.close()
- stderr = self.process.stderr.read()
- self.process.stderr.close()
+ stdout, stderr = self.process.communicate()
returncode = self.process.returncode
except Exception as e:
logger.error('Exception for %s: \n%s' % (subprocess.list2cmdline(cmd), e))
I have the same problem, reading a history of 1800 lines with buffer.
I will test for a days, and I will create a PR
Seems to work , justified improvement
Merged #297. Fix is in 0.8.5
https://libtmux.git-pull.com/history.html#libtmux-0-8-5-2020-10-25
pip install --user -U libtmux
Better now?
@gil-obradors @deliciouslytyped
(assuming you are still using libtmux, @deliciouslytyped, I apologize for the delay!)
Many thanks @tony !
Fixed in v0.8.5 (2020-10-25) via #297: tmux_cmd drains stdout and stderr via process.communicate() instead of deadlocking on full OS pipes in process.wait().
- API docs:
Pane.capture_pane()reference andtmux_cmdreference - Source:
src/libtmux/common.pyline 339