Bug 164946 - konsole hangs after inserting text
Summary: konsole hangs after inserting text
Status: RESOLVED FIXED
Alias: None
Product: konsole
Classification: Applications
Component: general (show other bugs)
Version: unspecified
Platform: openSUSE Linux
: NOR crash
Target Milestone: ---
Assignee: Konsole Developer
URL:
Keywords:
: 170007 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-06-25 17:22 UTC by P A S
Modified: 2008-12-26 16:48 UTC (History)
11 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
PTY master file descriptor non-blocking (1.73 KB, patch)
2008-11-11 22:13 UTC, Urs Fleisch
Details

Note You need to log in before you can comment on or make changes to this bug.
Description P A S 2008-06-25 17:22:19 UTC
Version:           2.1 (using KDE 4.0.83)
Installed from:    SuSE RPMs
OS:                Linux

I tried to copy the xorg.conf example from

http://forum.compiz-fusion.org/showthread.php?t=5502

in konsole.

After that, Konsole hangs forever.
Comment 1 Robert Knight 2008-06-25 17:29:30 UTC
> After that, Konsole hangs forever. 

If you leave Konsole for a couple of minutes or so does it respond?  This might be a duplicate of http://bugs.kde.org/show_bug.cgi?id=163445

Does running "killall knotify4" unfreeze Konsole?
Comment 2 David Palacio 2008-06-25 21:23:37 UTC
I tried to paste a very long command for compilation (12000 chars) and I suffer the same symptoms (22 minutes hanged at the momentof writing).

«killall knotify4» does not unfreeze Konsole.
Comment 3 David Palacio 2008-06-25 21:28:37 UTC
Also, I cannot open a new Konsole window from «Kickoff» of «Alt+F2» when it is hanged. I can from xterm typing «konsole».
Comment 4 P A S 2008-06-26 09:54:20 UTC
I waited now 10 minutes and nothing changed.
Also "killall knotify4" didn't help.
Comment 5 Robert Knight 2008-07-13 02:22:46 UTC
> I tried to copy the xorg.conf example from 

Copy it into vim or another terminal text editor you mean?
Comment 6 P A S 2008-07-13 09:03:17 UTC
Yes, for example. I tried to copy it in the mc text editor, but you can just copy it to the prompt.

Source is the opened page in a web browser (konqueror4 or firefox3).
Comment 7 Dario Andres 2008-07-25 04:24:56 UTC
Konsole Version 2.1 Using KDE 4.1.00 (KDE 4.0.99 (4.1 RC1+)) (KDEmod) in ArchLinux

Using the html code of the example URL (~2400 lines). 
Paste the text in nano editor inside Konsole took 7 seconds, but pasting the text into VIM inside Konsole seems to hangup all the Konsole windows

While loading the text into VIM:
At first, VIM process showed some CPU usage but later: Konsole, bash and VIM showed 0% cpu usage and its memory use remained the same

Inside gnome-terminal, pasting the text in nano took nearly the same time, and pasting in VIM took ~30secs (during that time, the VIM process was eating a lot of CPU)
Comment 8 Dario Andres 2008-07-30 18:59:53 UTC
Version 2.1 Using KDE 4.1.00 (KDE 4.1.0) final (KDEmod) in ArchLinux i686

Using all the html code of the example URL (~2400 lines)
Pasting the text in nano took 7 seconds. Pasting the text in VIM took ~1.5sec (I noticed that I have removed my vimrc).

Restoring my vimrc to my home dir, Konsole seems to hangup :(. 
So it seems to be a problem related to some vimrc option (at least in VIM)

My vimrc (taken from some web sample):

"Sample .vimrc
set nocompatible
set showmatch
set incsearch
set ignorecase
set smartcase
set history=100
set backspace=eol,start,indent
"set ruler
set tabstop=4
set shiftwidth=4
set expandtab
set virtualedit=all
set background=dark
set vb t_vg=
set mouse=v
set textwidth=79
set formatoptions=tcrq
set complete=.,t,i,b,w,k

-----

Pasting the XOrg.conf example of the test webpage directly in Konsole shell freezed the app. (while using 0% CPU). (29 minutes of hangup and counting, seems to be an infinite loop)
Comment 9 Vladimir Kulev 2008-08-13 04:28:46 UTC
Experience this too with KDE 4.1.0, Gentoo.
Comment 10 Dario Andres 2008-08-16 19:02:20 UTC
*** This bug has been confirmed by popular vote. ***
Comment 11 Torleif Markussen Lunde 2008-08-28 10:12:56 UTC
This also applies to Fedora 9 running KDE 4.1.0-1. Typically pasting more than 70 lines of code to konsole causes it to freeze. 'killall knotify4' does not unfreeze konsole. 
Comment 12 Davide Ferrari 2008-09-23 11:17:19 UTC
The bugs still exists in KDE 4.1.1 (Ubuntu packages). I was pasting a ~50k text and konsole got totally frozen. Please at least confirm the bug in Bugzilla.
Comment 13 Torleif Markussen Lunde 2008-09-23 22:49:06 UTC
Deleting ~/.kde solved the problem for me. However this made kmail (in kdepim 3.5.9-10) unable to send messages through smtp. 
Comment 14 S. Burmeister 2008-09-24 08:26:10 UTC
Because you just deleted all settings for your KDE3 application, if you were lucky and your distro uses .kde4 for KDE4 applications. Otherwise you just deleted all settings for all of your KDE applications. If your mail was stored at the default place, i.e. ~/.kde/share/apps/kmail it would be gone too.
Comment 15 Torleif Markussen Lunde 2008-09-24 08:51:23 UTC
Yes. That was my intention; to delete all settings for all kde-apps. It solved this bug, and dont be concerned; I have my email, and kmail is again up and running again. 
Comment 16 Davide Ferrari 2008-09-26 11:06:06 UTC
This happened in KDE 4.1.1 even with 130 lines pasted! This is a REALLY serious bug... please take a look at it because IMO it could be considered a showstopper.
Comment 17 Torleif Markussen Lunde 2008-09-26 11:09:36 UTC
Bug is back here. 
Comment 18 Davide Ferrari 2008-09-26 11:25:11 UTC
FYI I was pasting a shell script in a local bash prompt, no editors opened at all.
Comment 19 Robert Knight 2008-09-26 12:27:26 UTC
Hello,

I can confirm this.  Konsole is hanging when writing data to the terminal with write() in kdelibs/kpty/kptydevice.cpp:306 .  The write() call is blocking.  If I do something which causes signals to interrupt the write call, like resizing the terminal window, then the write process continues bit-by-bit.

Ossi - any ideas before I dig into this further?
Comment 20 Robert Knight 2008-09-26 14:00:34 UTC
Had a closer look - it seems that problems start happening when trying to write more than 4KB at once to the terminal, which is the chunk size for KPty's internal ring buffer.
Comment 21 Urs Fleisch 2008-11-10 21:48:53 UTC
I can confirm this. If more than 4095 bytes are pasted, konsole hangs. It seems to hang in the write system call of KPtyDevicePrivate::_k_canWrite(). I set the filedescriptor to non-blocking mode using "fcntl(q->masterFd(), F_SETFL, O_NONBLOCK)" before the write, and konsole did not hang anymore. Because I am not a pty expert, I do not claim that this is the solution (konsole could swallow some bytes), but maybe a starting point.
Comment 22 Urs Fleisch 2008-11-11 22:11:30 UTC
Hi,

The patch below sets the master PTY file descriptor to non-blocking mode and handles the EAGAIN errors which are then possible. With that patch applied I was able to paste large amounts of text into konsole without problems.

~~~~~~~~~
Index: kpty/kptydevice.cpp
===================================================================
--- kpty/kptydevice.cpp	(revision 882922)
+++ kpty/kptydevice.cpp	(working copy)
@@ -30,6 +30,7 @@
 #include <klocale.h>
 
 #include <unistd.h>
+#include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
 #include <termios.h>
@@ -273,7 +274,10 @@
         char *ptr = readBuffer.reserve(available);
         NO_INTR(readBytes, read(q->masterFd(), ptr, available));
         if (readBytes < 0) {
-            q->setErrorString(I18N_NOOP("Error reading from PTY"));
+            if (errno != EAGAIN) {
+                q->setErrorString(I18N_NOOP("Error reading from PTY"));
+            }
+            readBuffer.unreserve(available);
             return false;
         }
         readBuffer.unreserve(available - readBytes); // *should* be a no-op
@@ -307,7 +311,11 @@
             write(q->masterFd(),
                   writeBuffer.readPointer(), writeBuffer.readSize()));
     if (wroteBytes < 0) {
-        q->setErrorString(I18N_NOOP("Error writing to PTY"));
+        if (errno != EAGAIN) {
+            q->setErrorString(I18N_NOOP("Error writing to PTY"));
+        } else {
+            writeNotifier->setEnabled(true);
+        }
         return false;
     }
     writeBuffer.free(wroteBytes);
@@ -419,6 +427,7 @@
     readBuffer.clear();
     readNotifier = new QSocketNotifier(q->masterFd(), QSocketNotifier::Read, q);
     writeNotifier = new QSocketNotifier(q->masterFd(), QSocketNotifier::Write, q);
+    fcntl(q->masterFd(), F_SETFL, O_NONBLOCK);
     QObject::connect(readNotifier, SIGNAL(activated(int)), q, SLOT(_k_canRead()));
     QObject::connect(writeNotifier, SIGNAL(activated(int)), q, SLOT(_k_canWrite()));
     readNotifier->setEnabled(true);
~~~~~~~~~

Regards,
Urs
Comment 23 Urs Fleisch 2008-11-11 22:13:06 UTC
Created attachment 28499 [details]
PTY master file descriptor non-blocking
Comment 24 Allan Sandfeld 2008-11-27 11:02:37 UTC
I see this issue too, quite often. It freezes all konsole-windows when multiple are open.
Comment 25 andy 2008-11-29 17:10:55 UTC
I am new to Linux and get this bug, too.  How do I apply this patch (never done this before)?  My impression is that I have to navigate to the "kpty" folder but I cannot find it.  I am running Kubuntu.  Thanks so much.
Comment 26 Urs Fleisch 2008-11-30 10:15:20 UTC
KPty is in kdelibs. I have patched the sources from svn (kdesvn-build):

cd ~/kdesvn/kdelibs/
patch -p0 <pty_nonblocking.diff

To create a deb-package to be used with Kubuntu, get the kdelibs sources using "apt-get source" or download them and then extract them using "dpkg-source -x kde4libs-YOURVERSION.dsc". Then go into the extracted kde4libs directory, copy the patch into debian/patches and append it to the series file:

cp pty_nonblocking.diff debian/patches/
echo "pty_nonblocking.diff -p0" >>debian/patches/series

Now you can start the build using

dpkg-buildpackage -rfakeroot
Comment 27 Oswald Buddenhagen 2008-11-30 14:14:23 UTC
missed this somehow ...

yes, this is a classic deadlock: konsole writes to the child, the child responds with output and fills its end of the pty buffer, thus blocking at some point. as it is not accepting input from konsole any more, konsole's write will never finish.

the EAGAIN handling should be unnecessary, as the notifiers only fire when the error would not happen and we don't loop on the fd (except for the EINTR loop, but that should not matter, either).

i'll commit shortly.
Comment 28 Oswald Buddenhagen 2008-11-30 14:18:23 UTC
SVN commit 890839 by ossi:

make PTY non-blocking to avoid deadlock.
BUG: 164946


 M  +2 -0      kptydevice.cpp  


WebSVN link: http://websvn.kde.org/?view=rev&revision=890839
Comment 29 andy 2008-12-02 06:24:14 UTC
Thanks so much for the response. Since there is a "commit" and the status is "resolved", should I assume that an upgrade version of Konsole with the fix will be available soon that I will see using Adept? Sorry, I am sure these are really basic questions.  
Comment 30 Robert Knight 2008-12-03 23:58:53 UTC
> Since there is a "commit" and the status is "resolved", should I
> assume that an upgrade version of Konsole with the fix will be available
> soon that I will see using Adept? 

Not automatically I'm afraid.  Since the patch in comment #28 is really simple I expect that the Kubuntu team would be prepared to make it available as an update. 
Comment 31 Allan Sandfeld 2008-12-08 00:17:17 UTC
Has it been backported to KDE 4.1 branch?
Comment 32 Oswald Buddenhagen 2008-12-08 00:47:52 UTC
nope. feel free to do it.
Comment 33 Robert Knight 2008-12-08 12:20:08 UTC
Unfortunately I don't have a compile-able KDE 4.1 checkout of kdelibs at the moment - can another developer who does please backport this and CC this bug please?
Comment 34 Oswald Buddenhagen 2008-12-08 13:45:31 UTC
test-compiling is for wussies. committed it now. :D
Comment 35 Dario Andres 2008-12-26 16:48:37 UTC
*** Bug 170007 has been marked as a duplicate of this bug. ***