Skip to content

Commit d9919ee

Browse files
urwid: prevent memory leak
* Urwid has a cache (CanvasCache) that caches the render of each widget to speed up future render cycles. * Tui is a dynamic tool, it creates a new TuiNode for each update. * The old TuiNode and its child nodes were not being cleared out of the urwid cache preventing Python's garbage clearance from doing its job. * This change explicitly clears the urwid cache for each Tui update. * Preserving the cache post-update isn't really a performance boost for Tui as pretty much every node on the screen has changed anyway (besides the application header and footer). So this is likely of little consequence performance wise. The cache is still active between updates so things like expand/collapse operations will still benefit.
1 parent ae0eb0f commit d9919ee

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

changes.d/6722.fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a slow memory leak in Tui.

cylc/flow/tui/app.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
import re
2323

2424
import urwid
25-
try:
26-
from urwid.widget import SelectableIcon
27-
except ImportError:
28-
# BACK COMPAT: urwid.wimp
29-
# From: urwid 2.0
30-
# To: urwid 2.2
31-
from urwid.wimp import SelectableIcon
25+
from urwid.canvas import CanvasCache
26+
from urwid.widget import SelectableIcon
3227

3328
from cylc.flow.id import Tokens
3429
from cylc.flow.task_state import (
@@ -541,6 +536,11 @@ def update(self, *_):
541536
if self.loop:
542537
self.loop.set_alarm_in(self.UPDATE_INTERVAL, self.update)
543538

539+
# NOTE: prevent a memory leak by clearing out any caches that urwid
540+
# may have accumulated for the previous TuiNode instance
541+
# (and its child widgets)
542+
CanvasCache.clear()
543+
544544
return True
545545

546546
def filter_by_task_state(self, filtered_state=None):

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ install_requires =
7676
pyzmq>=22
7777
importlib_metadata>=5.0; python_version < "3.12"
7878
# NOTE: exclude two urwid versions that were not compatible with Tui
79-
urwid==2.*,!=2.6.2,!=2.6.3
79+
urwid>=2.2,!=2.6.2,!=2.6.3,<3
8080
# unpinned transient dependencies used for type checking
8181
rx
8282
promise

0 commit comments

Comments
 (0)