Ignore errors in various finalizers during exit

This was needed for tui loop to exit cleanly when terminal i/o breaks
This commit is contained in:
Kovid Goyal 2021-11-17 15:43:26 +05:30
parent 17a48f6b9f
commit 715925795f
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 8 deletions

View file

@ -3,6 +3,7 @@
from collections import deque
from contextlib import suppress
from time import monotonic
from types import TracebackType
from typing import (
@ -94,9 +95,10 @@ def __enter__(self) -> None:
def __exit__(self, etype: type, value: Exception, tb: TracebackType) -> None:
del self.debug.fobj
self.finalize()
if self._image_manager is not None:
self._image_manager.__exit__(etype, value, tb)
with suppress(Exception):
self.finalize()
if self._image_manager is not None:
self._image_manager.__exit__(etype, value, tb)
def initialize(self) -> None:
pass

View file

@ -10,7 +10,7 @@
import signal
import sys
import termios
from contextlib import contextmanager
from contextlib import contextmanager, suppress
from enum import Enum, IntFlag, auto
from functools import partial
from typing import Any, Callable, Dict, Generator, List, NamedTuple, Optional
@ -102,9 +102,10 @@ def __enter__(self) -> 'TermManager':
return self
def __exit__(self, *a: object) -> None:
self.reset_state_to_original()
close_tty(self.tty_fd, self.original_termios)
del self.tty_fd, self.original_termios
with suppress(Exception):
self.reset_state_to_original()
close_tty(self.tty_fd, self.original_termios)
del self.tty_fd, self.original_termios
class MouseButton(IntFlag):
@ -463,7 +464,8 @@ def _on_sigwinch() -> None:
term_manager.extra_finalize = b''.join(self.write_buf).decode('utf-8')
if tb is not None:
self.return_code = 1
self._report_error_loop(tb, term_manager)
if not handler.terminal_io_ended:
self._report_error_loop(tb, term_manager)
def _report_error_loop(self, tb: str, term_manager: TermManager) -> None:
self.loop_impl(UnhandledException(tb), term_manager)