Skip to content

Commit

Permalink
Send SSH window change message on SIGWINCH when pty=True
Browse files Browse the repository at this point in the history
(ported to Fabric-1.x and added signal handler cleanup)
Co-Authored-By: Pierce Lopez <[email protected]>
  • Loading branch information
bitprophet authored and ploxiln committed Jan 1, 2022
1 parent 8cff965 commit 5d2ad3f
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions fabric/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
import six
import subprocess
import signal
import sys
import time
from glob import glob
Expand Down Expand Up @@ -764,9 +765,17 @@ def _execute(channel, command, pty=True, combine_stderr=None,
# Request pty with size params (default to 80x24, obtain real
# parameters if on POSIX platform)
if using_pty:
sigwinch_orig = None
rows, cols = _pty_size()
channel.get_pty(width=cols, height=rows)

def handle_window_change(signum, frame):
rows, cols = _pty_size()
channel.resize_pty(width=cols, height=rows)

if hasattr(signal, "SIGWINCH"):
sigwinch_orig = signal.signal(signal.SIGWINCH, handle_window_change)

# Use SSH agent forwarding from 'ssh' if enabled by user
config_agent = ssh_config().get('forwardagent', 'no').lower() == 'yes'
forward = None
Expand Down Expand Up @@ -796,21 +805,26 @@ def _execute(channel, command, pty=True, combine_stderr=None,
ThreadHandler('in', input_loop, channel, stdin, using_pty)
)

while True:
if channel.exit_status_ready():
break
else:
# Check for thread exceptions here so we can raise ASAP
# (without chance of getting blocked by, or hidden by an
# exception within, recv_exit_status())
for worker in workers:
worker.raise_if_needed()
try:
time.sleep(ssh.io_sleep)
except KeyboardInterrupt:
if not remote_interrupt:
raise
channel.send('\x03')
try:
while True:
if channel.exit_status_ready():
break
else:
# Check for thread exceptions here so we can raise ASAP
# (without chance of getting blocked by, or hidden by an
# exception within, recv_exit_status())
for worker in workers:
worker.raise_if_needed()
try:
time.sleep(ssh.io_sleep)
except KeyboardInterrupt:
if not remote_interrupt:
raise
channel.send('\x03')
finally:
if using_pty:
if sigwinch_orig is not None:
signal.signal(signal.SIGWINCH, sigwinch_orig)

# Obtain exit code of remote program now that we're done.
status = channel.recv_exit_status()
Expand Down

0 comments on commit 5d2ad3f

Please sign in to comment.