Bug 472548

Summary: Konsole is very slow to perform a title bar / tab bar update
Product: [Applications] konsole Reporter: Alain Knaff <kde>
Component: generalAssignee: Konsole Developer <konsole-devel>
Status: REPORTED ---    
Severity: normal CC: bugs.kde
Priority: NOR    
Version: 22.12.3   
Target Milestone: ---   
Platform: Debian stable   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Alain Knaff 2023-07-23 21:08:43 UTC
SUMMARY

Konsole waits almost 2 secods after receiving an ANSI sequence to change its title until it actually pushes that change to the X server

STEPS TO REPRODUCE
1. echo -ne '\033]0;test\007'

OBSERVED RESULT

it takes almost 2 seconds before tab name and title bar is updated to "test"

EXPECTED RESULT

it should update the tab bar and title bar immediately

ADDITIONAL INFORMATION

Strace shows the following:

80262 22:02:28.488754 ioctl(24, FIONREAD, [9]) = 0
80262 22:02:28.488918 read(24, "\33]0;test\7", 9) = 9
80262 22:02:28.489073 recvmsg(11, {msg_namelen=0}, 0) = -1 EAGAIN (Resource temporarily unavailable)
80262 22:02:28.489205 recvmsg(11, {msg_namelen=0}, 0) = -1 EAGAIN (Resource temporarily unavailable)
80262 22:02:28.489291 poll([{fd=5, events=POLLIN}, {fd=10, events=POLLIN}, {fd=11, events=POLLIN}, {fd=15, events=POLLIN}, {fd=24, events=POLLIN}, {fd=34, events=POLLIN}], 6, 10) = 0 (Timeout)
80262 22:02:28.499665 recvmsg(11, {msg_namelen=0}, 0) = -1 EAGAIN (Resource temporarily unavailable)
80262 22:02:28.499815 recvmsg(11, {msg_namelen=0}, 0) = -1 EAGAIN (Resource temporarily unavailable)
80262 22:02:28.499902 poll([{fd=5, events=POLLIN}, {fd=10, events=POLLIN}, {fd=11, events=POLLIN}, {fd=15, events=POLLIN}, {fd=24, events=POLLIN}, {fd=34, events=POLLIN}], 6, 10) = 0 (Timeout)
80262 22:02:28.510193 recvmsg(11, {msg_namelen=0}, 0) = -1 EAGAIN (Resource temporarily unavailable)
80262 22:02:28.510320 recvmsg(11, {msg_namelen=0}, 0) = -1 EAGAIN (Resource temporarily unavailable)
80262 22:02:28.510764 poll([{fd=5, events=POLLIN}, {fd=10, events=POLLIN}, {fd=11, events=POLLIN}, {fd=15, events=POLLIN}, {fd=24, events=POLLIN}, {fd=34, events=POLLIN}], 6, 1891) = 0 (Timeout)
80262 22:02:30.403850 ioctl(24, TIOCGPGRP, [80312]) = 0
80262 22:02:30.403957 ioctl(24, TIOCGPGRP, [80312]) = 0
80262 22:02:30.404046 readlink("/proc/80312/cwd", "/home/alain", 4096) = 11
80262 22:02:30.404196 poll([{fd=3, events=POLLIN|POLLOUT}], 1, -1) = 1 ([{fd=3, revents=POLLOUT}])
80262 22:02:30.404290 writev(3, [{iov_base="\22\0\n\0\r\0\0\6Y\1\0\0J\1\0\0\10\0\0\0\20\0\0\0test \342\200\224 Konsole", iov_len=40}], 1) = 40

So, basically, it sits on its handsfor two seconds, before finally pushing the update to the X server...
Comment 1 Alain Knaff 2023-07-25 21:57:53 UTC
I started investigating this in more detail, and here is what I found:

1. Session::setSessionAttribute gets invoked as soon as the ANSI sequence is received.
2. Session::setSessionAttribute copies the "caption" method parameter to the _userTitle object attribute, and sets local variable modified to true (if title did indeed change)
3. Near the end, it emits sessionAttributeChanged() if modified
4. SessionController::sessionAttributeChanged() indeed activates immediately (good), and attempts to set the title using ViewProperties::setTitle
5. However, it gets the "wrong" title to set. In step #2, session._userTitle was set, however, sessionAttributeChanged() gets session._displayName (via session()->title(Session::DisplayedTitleRole);)
6. 2 seconds later, _userTitle gets copied over to _displayTitle by Session::setTitle, and ViewProperties::setTitle gets invoked again, and this time succeeds in setting the correct (i.e. new) title.

=> The immediate invocation of step #4 shows that there is indeed an intent to perform the title update promptly. However, step #6 (or equivalent) should be invoked before it as well.

The following patch is a stop-gap measure which fixes the issue:
--- konsole-orig/konsole-22.12.3/src/session/Session.cpp        2023-02-27 05:02:33.000000000 +0100
+++ konsole-22.12.3/src/session/Session.cpp     2023-07-25 22:03:35.518283329 +0200
@@ -537,6 +537,7 @@
     if ((what == IconNameAndWindowTitle) || (what == WindowTitle)) {
         if (_userTitle != caption) {
             _userTitle = caption;
+            _displayTitle = caption;
             modified = true;
         }
     }
Comment 2 acmondor 2024-03-03 21:18:46 UTC
I can confirm this bug is real and still exists in 23.08.4 and it's been an irritant for a while. Although, what I see might be slightly different than was originally described by Alain. With the "Tab title format" setting set to %w, when the title setting ANSI sequence is sent Konsole immediately changes the title on it's main window, then after a delay of approximately 2 seconds it sets the title on the Tab that initiated the change.

I did some digging into the code before finding this bug report, and it seems that the delay may be tied to the _interactionTimer in SessionController.cpp. That timer has an interval of 2 seconds and it triggers SessionController::snapshot() which does some title manipulations. However, before going further down that path I tried a patch close to what Alain had and it worked for me, so I didn't go any  further.

Here's my patch:

diff -w -ruN konsole-23.08.4/src/session/Session.cpp konsole-23.08.4-fixed/src/session/Session.cpp
--- konsole-23.08.4/src/session/Session.cpp     2023-12-04 21:11:17.000000000 -0600
+++ konsole-23.08.4-fixed/src/session/Session.cpp       2024-03-03 14:25:26.251659531 -0600
@@ -613,6 +613,10 @@
             _userTitle = caption;
             modified = true;
         }
+        if (_displayTitle != caption) {
+            _displayTitle = caption;
+            modified = true;
+        }
     }
 
     if ((what == IconNameAndWindowTitle) || (what == IconName)) {
Comment 3 acmondor 2024-03-30 16:22:19 UTC
I dug into the konsole source code and isolated the 2 second delay issue to the following commit:

  https://invent.kde.org/utilities/konsole/-/commit/416941b7141ee6c81cc1f92592f24bcb426d51e1

What's happening is that SessionController::snapshot() is applying the actual tab title update and prior to the above commit the code was run every 500msec, which was fast enough to appear instantaneous. With the above commit the code is now only run every 2 seconds, which makes for the very noticeable and slow update.
Comment 4 acmondor 2024-03-30 16:30:59 UTC
I should have also mentioned that I see this delayed updated behaviour on Gentoo Linux, on which I'm currently running konsole 23.08.5.

My previously mentioned patch for 23.08.4, a slight variation on Alain's original patch, still works to resolve the issue in 23.08.5.