mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-25 17:52:02 +02:00
Improve disposal of garbage data on ssh failure
This commit is contained in:
@@ -497,23 +497,28 @@ def dcs_to_kitty(payload: Union[bytes, str], type: str = 'ssh') -> bytes:
|
|||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def drain_potential_tty_garbage(p: 'subprocess.Popen[bytes]', data_request: str) -> Iterator[None]:
|
def drain_potential_tty_garbage(p: 'subprocess.Popen[bytes]', data_request: str) -> Iterator[None]:
|
||||||
|
ssh_started_at = time.monotonic()
|
||||||
with open(os.open(os.ctermid(), os.O_CLOEXEC | os.O_RDWR | os.O_NOCTTY), 'wb') as tty:
|
with open(os.open(os.ctermid(), os.O_CLOEXEC | os.O_RDWR | os.O_NOCTTY), 'wb') as tty:
|
||||||
tty.write(dcs_to_kitty(data_request))
|
tty.write(dcs_to_kitty(data_request))
|
||||||
tty.flush()
|
tty.flush()
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
if p.returncode:
|
if p.returncode and time.monotonic() - ssh_started_at < 30:
|
||||||
# discard queued data on tty in case data transmission was
|
# discard queued input data on tty in case data transmission was
|
||||||
# interrupted due to SSH failure, avoids spewing garbage to
|
# interrupted due to SSH failure, avoids spewing garbage to
|
||||||
# screen
|
# screen
|
||||||
termios.tcflush(tty.fileno(), termios.TCIOFLUSH)
|
termios.tcflush(tty.fileno(), termios.TCIFLUSH)
|
||||||
|
data = b''
|
||||||
|
start = time.monotonic()
|
||||||
with open(tty.fileno(), 'rb', closefd=False) as tf:
|
with open(tty.fileno(), 'rb', closefd=False) as tf:
|
||||||
os.set_blocking(tf.fileno(), False)
|
os.set_blocking(tf.fileno(), False)
|
||||||
from tty import setraw
|
from tty import setraw
|
||||||
setraw(tf.fileno(), termios.TCSANOW)
|
setraw(tf.fileno(), termios.TCSANOW)
|
||||||
while select([tf], [], [], 0)[0]:
|
while time.monotonic() - start < 1 and select([tf], [], [], 0.075)[0]:
|
||||||
tf.read()
|
data += tf.read()
|
||||||
|
if b'KITTY_DATA_END' in data:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def run_ssh(ssh_args: List[str], server_args: List[str], found_extra_args: Tuple[str, ...], echo_on: bool) -> NoReturn:
|
def run_ssh(ssh_args: List[str], server_args: List[str], found_extra_args: Tuple[str, ...], echo_on: bool) -> NoReturn:
|
||||||
|
|||||||
Reference in New Issue
Block a user