libtmux icon indicating copy to clipboard operation
libtmux copied to clipboard

capture_pane hangs with `-S-`

Open deliciouslytyped opened this issue 6 years ago • 7 comments

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

deliciouslytyped avatar Dec 25 '19 12:12 deliciouslytyped

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

deliciouslytyped avatar Dec 25 '19 12:12 deliciouslytyped

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

deliciouslytyped avatar Dec 25 '19 12:12 deliciouslytyped

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))

deliciouslytyped avatar Dec 25 '19 12:12 deliciouslytyped

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

gil-obradors avatar Sep 13 '20 17:09 gil-obradors

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!)

tony avatar Oct 25 '20 16:10 tony

Many thanks @tony !

gil-obradors avatar Oct 25 '20 17:10 gil-obradors

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().

tony avatar Nov 21 '26 13:11 tony