From 1b06e50f105b979c40a2e9c39d59c2134258aa4e Mon Sep 17 00:00:00 2001 From: darrenburns Date: Tue, 7 Mar 2023 13:01:06 +0000 Subject: [PATCH] Fix for DataTable race-condition crash (#1962) * Fix for DataTable race-condition crash * Update CHANGELOG.md --- CHANGELOG.md | 1 + src/textual/widgets/_data_table.py | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4296bb28c..d69a02dee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fixed bug that prevented app pilot to press some keys https://github.com/Textualize/textual/issues/1815 +- DataTable race condition that caused crash https://github.com/Textualize/textual/pull/1962 ## [0.13.0] - 2023-03-02 diff --git a/src/textual/widgets/_data_table.py b/src/textual/widgets/_data_table.py index 693334384..957aa1aaa 100644 --- a/src/textual/widgets/_data_table.py +++ b/src/textual/widgets/_data_table.py @@ -1069,6 +1069,12 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): console = self.app.console for row_key in new_rows: row_index = self._row_locations.get(row_key) + + # The row could have been removed before on_idle was called, so we + # need to be quite defensive here and don't assume that the row exists. + if row_index is None: + continue + row = self.rows.get(row_key) if row.label is not None: @@ -1080,9 +1086,6 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True): self._label_column.content_width, label_content_width ) - if row_index is None: - continue - for column, renderable in zip(self.ordered_columns, cells_in_row): content_width = measure(console, renderable, 1) column.content_width = max(column.content_width, content_width)