Skip to content

Commit 7159257

Browse files
committed
Ignore errors in various finalizers during exit
This was needed for tui loop to exit cleanly when terminal i/o breaks
1 parent 17a48f6 commit 7159257

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

kittens/tui/handler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44

55
from collections import deque
6+
from contextlib import suppress
67
from time import monotonic
78
from types import TracebackType
89
from typing import (
@@ -94,9 +95,10 @@ def __enter__(self) -> None:
9495

9596
def __exit__(self, etype: type, value: Exception, tb: TracebackType) -> None:
9697
del self.debug.fobj
97-
self.finalize()
98-
if self._image_manager is not None:
99-
self._image_manager.__exit__(etype, value, tb)
98+
with suppress(Exception):
99+
self.finalize()
100+
if self._image_manager is not None:
101+
self._image_manager.__exit__(etype, value, tb)
100102

101103
def initialize(self) -> None:
102104
pass

kittens/tui/loop.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import signal
1111
import sys
1212
import termios
13-
from contextlib import contextmanager
13+
from contextlib import contextmanager, suppress
1414
from enum import Enum, IntFlag, auto
1515
from functools import partial
1616
from typing import Any, Callable, Dict, Generator, List, NamedTuple, Optional
@@ -102,9 +102,10 @@ def __enter__(self) -> 'TermManager':
102102
return self
103103

104104
def __exit__(self, *a: object) -> None:
105-
self.reset_state_to_original()
106-
close_tty(self.tty_fd, self.original_termios)
107-
del self.tty_fd, self.original_termios
105+
with suppress(Exception):
106+
self.reset_state_to_original()
107+
close_tty(self.tty_fd, self.original_termios)
108+
del self.tty_fd, self.original_termios
108109

109110

110111
class MouseButton(IntFlag):
@@ -463,7 +464,8 @@ def _on_sigwinch() -> None:
463464
term_manager.extra_finalize = b''.join(self.write_buf).decode('utf-8')
464465
if tb is not None:
465466
self.return_code = 1
466-
self._report_error_loop(tb, term_manager)
467+
if not handler.terminal_io_ended:
468+
self._report_error_loop(tb, term_manager)
467469

468470
def _report_error_loop(self, tb: str, term_manager: TermManager) -> None:
469471
self.loop_impl(UnhandledException(tb), term_manager)

0 commit comments

Comments
 (0)