From 397e432433d113bfa4a0c10aeab361d7eb5f6e89 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 9 Nov 2026 13:34:13 +0530 Subject: [PATCH] Ignore the paused rendering snapshot when rendering is not paused The snapshot of the screen taken for a synchronized update is kept after the update ends, so its cells prevented the style table from being emptied on hard resets and clearing of the scrollback, and kept their styles alive through garbage collection. It is unused when rendering is not paused and is overwritten entirely when rendering is next paused, so only consider it while rendering is paused. Also detect use of the formatting of stale line copies when comparing lines and in test_shape() and test_render_line(). --- kitty/fonts.c | 2 ++ kitty/line.c | 12 +++++++++++- kitty/screen.c | 15 ++++++++++++--- kitty_tests/styles.py | 39 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/kitty/fonts.c b/kitty/fonts.c index 520597be5..9b6f0b8c9 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -2101,6 +2101,7 @@ test_shape(PyObject UNUSED *self, PyObject *args) { char *path = NULL; int index = 0; if (!PyArg_ParseTuple(args, "O!|zi", &Line_Type, &line, &path, &index)) return NULL; + if (!line_styles_are_valid(line)) return NULL; index_type num = 0; const CPUCell *c; while (num < line->xnum && cell_has_text(line->cpu_cells + num)) { @@ -2631,6 +2632,7 @@ static PyObject * test_render_line(PyObject UNUSED *self, PyObject *args) { PyObject *line; if (!PyArg_ParseTuple(args, "O!", &Line_Type, &line)) return NULL; + if (!line_styles_are_valid((Line *)line)) return NULL; if (!num_font_groups) { PyErr_SetString(PyExc_RuntimeError, "must create font group first"); return NULL; diff --git a/kitty/line.c b/kitty/line.c index 887735cec..a5c82cd95 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -1235,7 +1235,17 @@ alloc_line(TextCache *tc) { return ans; } -RICHCMP(Line) +static PyObject * +richcmp(PyObject *obj1, PyObject *obj2, int op) { + // Like RICHCMP() but fails for copies whose formatting is no longer valid + if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED; + if (!PyObject_TypeCheck(obj1, &Line_Type) || !PyObject_TypeCheck(obj2, &Line_Type)) Py_RETURN_FALSE; + Line *a = (Line *)obj1, *b = (Line *)obj2; + if (!line_styles_are_valid(a) || !line_styles_are_valid(b)) return NULL; + const bool eq = __eq__(a, b); + return Py_NewRef((op == Py_EQ) == eq ? Py_True : Py_False); +} + INIT_TYPE(Line) // }}} diff --git a/kitty/screen.c b/kitty/screen.c index d99385566..86c3689ca 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1323,6 +1323,15 @@ style_gc_process_linebuf(CellStyleTable *t, StyleTableGC *gc, LineBuf *lb) { if (lb) style_gc_process_cells(t, gc, lb->cpu_cell_buf, (size_t)lb->ynum * lb->xnum, false); } +static LineBuf * +paused_linebuf_in_use(const Screen *self) { + // The snapshot of the screen taken when rendering was paused, NULL when + // rendering is not paused. The snapshot is kept after rendering resumes, + // but is unused then and entirely overwritten when rendering is next + // paused, so its cells must not keep styles alive. + return self->paused_rendering.expires_at ? self->paused_rendering.linebuf : NULL; +} + static bool garbage_collect_style_table(Screen *self, unsigned quantize_bits, bool degraded_if_full) { // Remap the style index of every live cell onto a fresh table, so styles @@ -1335,7 +1344,7 @@ garbage_collect_style_table(Screen *self, unsigned quantize_bits, bool degraded_ // Screen cells first, so they get exact styles even if the table overflows style_gc_process_linebuf(t, gc, self->main_linebuf); style_gc_process_linebuf(t, gc, self->alt_linebuf); - style_gc_process_linebuf(t, gc, self->paused_rendering.linebuf); + style_gc_process_linebuf(t, gc, paused_linebuf_in_use(self)); // The overlay buffers have columns cells, overlay_line.xnum is the length of the overlay text if (self->overlay_line.cpu_cells) style_gc_process_cells(t, gc, self->overlay_line.cpu_cells, self->columns, false); if (self->overlay_line.original_line.cpu_cells) style_gc_process_cells(t, gc, self->overlay_line.original_line.cpu_cells, self->columns, false); @@ -1364,7 +1373,7 @@ reset_style_table_if_unused(Screen *self) { // to be interned again and the style texture to be uploaded again every // frame. Small tables are not worth emptying for the same reason. if (self->historybuf->count || !style_table_is_worth_resetting(self->style_table)) return; - LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, self->paused_rendering.linebuf}; + LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, paused_linebuf_in_use(self)}; for (unsigned i = 0; i < arraysz(bufs); i++) { if (bufs[i] && cells_use_styles(bufs[i]->cpu_cell_buf, (size_t)bufs[i]->xnum * bufs[i]->ynum)) return; } @@ -7955,7 +7964,7 @@ test_verify_styles(Screen *self, PyObject *a UNUSED) { RAII_PyObject(ans, PyList_New(0)); if (!ans) return NULL; const CellStyleTable *t = self->style_table; - LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, self->paused_rendering.linebuf}; + LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, paused_linebuf_in_use(self)}; const char *names[3] = {"main", "alt", "paused"}; for (unsigned b = 0; b < arraysz(bufs); b++) { LineBuf *lb = bufs[b]; diff --git a/kitty_tests/styles.py b/kitty_tests/styles.py index f8ef4d37d..9acc0c617 100644 --- a/kitty_tests/styles.py +++ b/kitty_tests/styles.py @@ -3,7 +3,7 @@ import random -from kitty.fast_data_types import Cursor +from kitty.fast_data_types import Cursor, test_render_line, test_shape from .base import BaseTest, draw_multicell, parse_bytes @@ -379,6 +379,30 @@ class TestStyles(BaseTest): s.draw('d') self.assertTrue(s.line(0).cursor_from(0).bold) self.assert_styles_consistent(s) + # The snapshot of the screen taken by a synchronized update is kept + # after the update ends, but must not prevent a reset + parse_bytes(s, b'\x1bc') + add_styles() + parse_bytes(s, b'\x1b[?2026h\x1b[?2026l\x1b[2J') + s.erase_in_display(3) + self.ae(s.style_table_info()['count'], 1) + self.assert_styles_consistent(s) + # While the update is in progress, the snapshot keeps its styles alive + add_styles() + parse_bytes(s, b'\x1b[?2026h\x1b[2J') + s.erase_in_display(3) + self.ae(s.style_table_info()['count'], 6) + self.assert_styles_consistent(s) + parse_bytes(s, b'\x1b[?2026l') + s.erase_in_display(3) + self.ae(s.style_table_info()['count'], 1) + # A new update takes a fresh snapshot, with valid styles + add_styles() + parse_bytes(s, b'\x1b[?2026h') + s.test_gc_style_table() + self.ae(s.style_table_info()['count'], 6) + self.assert_styles_consistent(s) + parse_bytes(s, b'\x1b[?2026l') def test_style_of_line_copies_after_gc(self): s = self.create_screen(cols=6, lines=3, scrollback=2) @@ -391,7 +415,18 @@ class TestStyles(BaseTest): # Views into the screen are remapped by the collection self.assertTrue(view.cursor_from(0).bold) # Copies are not, so using their formatting is an error - for f in (lambda: copy.cursor_from(0), copy.as_ansi, lambda: copy.apply_cursor(Cursor(), 0, 1), lambda: copy.set_attribute('bold', 1)): + for f in ( + lambda: copy.cursor_from(0), + copy.as_ansi, + lambda: copy.apply_cursor(Cursor(), 0, 1), + lambda: copy.set_attribute('bold', 1), + lambda: copy == view, + lambda: view != copy, + lambda: copy.copy_char(0, view, 0), + lambda: view.copy_char(0, copy, 0), + lambda: test_render_line(copy), + lambda: test_shape(copy), + ): self.assertRaises(ValueError, f) self.ae(str(copy), 'abc') # the text is still usable # Copying a whole line into the copy makes its formatting valid again