Bug 323929

Summary: kmail high cpu load, seems stuck looping on fetching mails from akonadi
Product: [Applications] kmail2 Reporter: Marc Cousin <cousinmarc>
Component: generalAssignee: kdepim bugs <kdepim-bugs>
Status: RESOLVED WORKSFORME    
Severity: major CC: bughunt, cfeck, cousinmarc, dvratil, fsfbugs, gt, kde, kmm, knallio, lukas.schneiderbauer, maarten, Martin, mollekopf, peter.fink126, rgfernandes
Priority: NOR Keywords: triaged
Version: 4.11   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: akonadiconsole logfile
Backtrace of KMail replacing one "inbox" with another
Patch to try to fix the 100% of cpu bug

Description Marc Cousin 2013-08-23 16:03:10 UTC
Hi,

I have been having this problem for quite a while, in fact, but I just have taken the time to diagnose it.

Kmail eats a lot of CPU on my machine, specifically when one account is activated.

If I use the akonadi console, I see it doing 

KMail Kernel ETM (0x104c840) * 9518 FETCH (UID 9518 REV 0 REMOTEID "3" MIMETYPE "message/rfc822" COLLECTIONID 23 SIZE 5571 FLAGS (\ANSWERED \SEEN) ANCESTORS ((23 ".dlb-admin") (14 "imap://marc.cousin@dalibo.com@mail.dalibo.com/") (0 "")) PLD:ENVELOPE[1] {299} ("Thu, 04 Oct 2012 00:34:18 +0200" "Reboot telemaque" (("damien clochard" NIL "damien.clochard" "dalibo.com")) ((NIL NIL "dlb-admin-bounces" "listes.dalibo.com")) ((NIL NIL "dlb-admin" "listes.dalibo.com")) ((NIL NIL "dlb-admin" "listes.dalibo.com")) NIL NIL NIL "<506CBD6A.9010108@dalibo.com>" NIL)) 
KMail Kernel ETM (0x104c840) * 9517 FETCH (UID 9517 REV 0 REMOTEID "2" MIMETYPE "message/rfc822" COLLECTIONID 23 SIZE 7224 FLAGS (\SEEN) ANCESTORS ((23 ".dlb-admin") (14 "imap://marc.cousin@dalibo.com@mail.dalibo.com/") (0 "")) PLD:ENVELOPE[1] {382} ("Wed, 03 Oct 2012 18:17:10 +0200" "DONE : Monitorer le yang collector" (("Nicolas Thauvin" NIL "nicolas.thauvin" "dalibo.com")) ((NIL NIL "dlb-admin-bounces" "listes.dalibo.com")) ((NIL NIL "dlb-admin" "listes.dalibo.com")) ((NIL NIL "dlb-admin" "listes.dalibo.com")) NIL NIL "<506BFB16.9060107@dalibo.info>" "<20121003181710.712f77f8@dalibo.com>" "<506BFB16.9060107@dalibo.info>")) 
KMail Kernel ETM (0x104c840) * 9516 FETCH (UID 9516 REV 0 REMOTEID "1" MIMETYPE "message/rfc822" COLLECTIONID 23 SIZE 5486 FLAGS (\SEEN) ANCESTORS ((23 ".dlb-admin") (14 "imap://marc.cousin@dalibo.com@mail.dalibo.com/") (0 "")) PLD:ENVELOPE[1] {310} ("Wed, 03 Oct 2012 10:45:10 +0200" "TODO : Monitorer le yang collector" (("damien clochard" NIL "damien" "dalibo.info")) ((NIL NIL "dlb-admin-bounces" "listes.dalibo.com")) ((NIL NIL "dlb-admin" "listes.dalibo.com")) ((NIL NIL "dlb-admin" "listes.dalibo.com")) NIL NIL NIL "<506BFB16.9060107@dalibo.info>" NIL)) 
KMail Kernel ETM (0x104c840) 1986 OK FETCH completed 

all the time, with tens of thousands of messages. The akonadi console cannot keep up… I have to activate debug only for one second, then it scrolls for at least 10 seconds. These messages never stop. And if I try to capture for a minute or so, akonadiconsole gets out of RAM.

I guess there is something it doesn't like in an imap folder... It's been doing this on this account for at least 2 years. I have tried Kmail2 again at each KDE release, and switched back because of that. I know I should have given it a look before... 

I tried to let it run for 24 hours, hoping it would settle. But it doesn't and always comes back to fetching all emails in these few folders (always the same 3 or 4 collectionids).

I have 3 other accounts with no problem. Can I provide some more information ? A log , a debug trace, whatever may help you solve this problem ?
Comment 1 arcanis 2013-08-24 15:29:03 UTC
Confirm this bug. I have two imap accounts in kmail. Disabling indexing mail does not work. Also kmail over time uses a lot of memory - about 3GB
Comment 2 Marc Cousin 2013-08-24 15:30:26 UTC
Yes, it doesn't feel like it is an indexing problem. More that kmail is asking again and again what is in a few folders.
Comment 3 PeterF 2013-08-24 17:10:45 UTC
I can also confirm this bug (Kde 4.11 on Kubuntu 12.10). It takes forever to retrieve an email item be it an IMAP or local maildir item.
Kmail2 takes about 31% of one cpu core and continues to consume RAM by the second. This behaviour does not stop even when exiting the program, you have to kill the process explicitely for it to stop.
I can also confirm the huge error load on the akonadi console, I able to post fragments of a saved log file if needed.
Comment 4 PeterF 2013-08-24 17:42:03 UTC
Created attachment 81894 [details]
akonadiconsole logfile
Comment 5 PeterF 2013-08-25 10:23:42 UTC
Today I started with a new nepomuk DB. Then I gradually added my POP and one IMAP account and everything worked fine.
The problems reappeared when I added my second IMAP account and I had the same symptoms again (high CPU load of kontact at around 31% + high RAM, emails take forever to be retrieved).
So I would also say it is more an akonadi problem than of kontact.
At this point I am a bit lost on how to pinpoint the culprit. I would appreciate the help of an akonadi DEV at this point.
Comment 6 arcanis 2013-08-25 18:40:34 UTC
Who gets this bug try:
mv ~/.local/share/akonadi/ ~/.local/share/akonadi_old
akonadictl restart
And try to start kmail. It works for me.
Comment 7 PeterF 2013-08-25 19:39:50 UTC
Hello. Thanks for the workaround, it worked also for me. I am crossing my fingers that this will be a permanent solution.
Comment 8 Marc Cousin 2013-08-25 20:31:10 UTC
It doesn't work for me. I destroyed my akonadi configuration and database several times, and the problem always came back.
Comment 9 Marc Cousin 2013-08-26 09:25:55 UTC
It seems we are not having exactly the same problem, PeterF: my kmail isn't leaking memory.

So I think it would be better for you to create another bug report, if you have this behaviour again.
Comment 10 PeterF 2013-08-26 11:13:28 UTC
I have restarted my machine twice since I applied the workaround and kontact is back to a useable state. Just hoping that Murphy's law doesn't kick in after this post.
Comment 11 knallio 2013-08-26 16:18:26 UTC
If think I have the same problem. Moving ~/.local/share/akonadi/ helped for a while, and then the problem reappeared. In addition kontact is filling the job tracker with endless CollectionFetchJobs. I am not sure if https://bugs.kde.org/show_bug.cgi?id=323068 is related or not.
Comment 12 knallio 2013-08-26 16:21:47 UTC
Just one addition: I can see and read all Mails in the akonadieconsole browser, the problem only appears when I start kontact.
Comment 13 Marc Cousin 2013-08-26 16:22:51 UTC
knallio: It is exactly the same for me. That was what made me suspect that the problem was in kmail, not in akonadi.
Comment 14 Maarten ter Huurne 2013-08-28 18:21:59 UTC
I had a similar problem and managed to fix it. In my local maildirs, I had both "inbox" (the usual inbox) and "{archive}/inbox" (containing old mails moved out of my inbox). Somehow, KMail considered "{archive}/inbox" to be a special folder as well, and it kept replacing one inbox with the other in its special folders collection.

The fix was to create a new folder "{archive}/inb2", move the contents of "{archive}/inbox" there and then remove "{archive}/inbox". I did this in the Browser tab of akonadiconsole, since KMail was not responsive.

Some details from the debugging session:

(gdb) frame 5
#5  0x00007fa6f0828428 in Akonadi::SpecialCollections::registerCollection (this=0xb39820, type=..., 
    collection=...) at /usr/src/debug/kdepimlibs-4.11.0/akonadi/specialcollections.cpp:238
238         d->emitChanged( resourceId );
(gdb) printq4string resourceId
akonadi_mixedmaildir_resource_0
(gdb) print collection.id()
$1 = 30
(gdb) print oldCollection.id()
$2 = 64

Both these collections (30 and 64) had the same remoteId ("inbox"), but a different parentId.

I'll add the full backtrace as an attachment.
Comment 15 Maarten ter Huurne 2013-08-28 18:23:47 UTC
Created attachment 81999 [details]
Backtrace of KMail replacing one "inbox" with another
Comment 16 Marc Cousin 2013-08-28 23:39:30 UTC
Not the problem I am having either… no two boxes with the same name.
Comment 17 Tamás Gere 2013-08-29 14:52:11 UTC
I can confirm too. I upgraded with 3 imap mailboxes. Seems akonadi is fine, since everything normal (fetches email properly, notifications are ok, feeds nepomuk, etc.) until kmail (or kontact with kmail module enabled) started up.

After startup, kmail starts cycle over all e-mails in all mailbox and never finish. Akonadi console shows that kmail requesting "LSUB 0 INF (MIMETYPE (message/rfc822))" on all mailbox and processing all messsage. When finished, the whole process starts again. 

This endless process eats my cpu by akonadiserver and mysql (i use mysql as backend). It's impossible to work with kmail, because each mail fetching takes 3-10mins.

Since I'm using kde (and kmail) for work, I tried find solution. After a lot of search, I found some suggestion:
- search for same named resources which probably a leftover of bad migration process (didn't have any)
- removed all imap accounts, and added only one.
- removed all data in .local/share/akonadi/
- deleted all data in mysql database

None of above worked for me. Since I have to work, I given up and downgraded all kdepim related ebuilds (I'm using gentoo) to 4.10.5. I didn't downgraded akonadi-server, it's still at 1.10.2-r1.
Now I have some filter and organizer crash, akonadiconsole crashes on startup as well, but at least I can work with kmail.

It's interesting what Maarten ter Huurne wrote. I don't have subfolder named "inbox", but all of my imap mailboxes has "INBOX." prefix. Therefore all of my other special folders (sent, trash, drafts, templates) are subfolders under inbox. That means I have special folder in special folder too. So maybe I'm affected by that "replace and re-parse" bug as well 

Now I think I'll upgrade again and play with these special folders.
Comment 18 km 2013-08-29 23:02:09 UTC
*** This bug has been confirmed by popular vote. ***
Comment 19 Tamás Gere 2013-09-19 13:46:00 UTC
Hello!

4.11.1 upgrade solved all problems for me.

Thanks.
Comment 20 Marc Cousin 2013-09-20 06:53:23 UTC
It seems to have solved the problem here too… but I had to destroy and recreate my akonadi database from scratch.
Comment 21 Marc Cousin 2013-09-20 07:17:42 UTC
Wow, I think I have found what happened here. The problem only occurs when I start putting a lot of folders in my favorites. It just happened right now. After 24 hours of kmail working flawlessly (after I destroyed akonadi AND kmail's configuration) I started to add back my favorites to the top-left area, and the problem reappeared after I added one too much (it seems to start at 12 here). Can someone try this ?
Comment 22 Daniel Vrátil 2013-09-25 18:21:03 UTC
I can confirm that adding more than 11 favorite folders in KMail will triggers fetching items from Akonadi. It's extremely weird and I'll try to see what's going on.
Comment 23 Marc Cousin 2013-10-12 07:32:44 UTC
I haven't had this bug for 3 weeks now. I just kept 10 favorites.
Comment 24 Raul Fernandes 2013-12-03 21:33:19 UTC
Created attachment 83913 [details]
Patch to try to fix the 100% of cpu bug

I think the problem is the repeated connect calls to same signal/slot.
With UniqueConnection, it shouldn't happen but it can be a qt bug.
Comment 25 Raul Fernandes 2013-12-03 21:34:16 UTC
I have a similar problem here.
Can you test this patch for me??
It fixed the problem I had.
If this fix the problem to you I will send the patch to kmail developers.
Comment 27 Raul Fernandes 2013-12-03 21:40:38 UTC
Duplicate too
https://bugs.kde.org/show_bug.cgi?id=325043
https://bugs.kde.org/show_bug.cgi?id=285097 (probably)
Comment 28 Raul Fernandes 2013-12-04 21:30:51 UTC
Nobody from kmail??
I will commit it if nobody say anything because I think it is a showstopper bug and it is lasting too long.
Comment 29 Marc Cousin 2013-12-04 23:43:39 UTC
Sorry, I had to stop using kmail, so I cannot test it… I had something like 1 million files in my akonadi's mysql directory, so it was becoming unusable once more.
Comment 30 Daniel Vrátil 2013-12-06 09:43:55 UTC
If you have a patch. please upload it to reviewboard (git.reviewboard.kde.org), where more developers can check and test it.
Comment 31 Raul Fernandes 2013-12-07 11:01:14 UTC
Submitted to reviewboard.
https://git.reviewboard.kde.org/r/114341/
Comment 32 fsfbugs 2013-12-10 22:18:32 UTC
Just a bit of feedback. I've built the current head of the 4.11 branch (including Raul Fernandes's patch). Solves the problem completely for me; thanks, Raul!
Comment 33 Christian Mollekopf 2013-12-12 10:38:36 UTC
Isn't this a duplicate of https://bugs.kde.org/show_bug.cgi?id=323068 ? It seems to be the same codepath.
Comment 34 Christian Mollekopf 2013-12-12 10:47:15 UTC
I'm not sure the applied fix actually fixed the actual problem, since the use of Qt::UniqueConnection was IMO just fine. The applied patch just disconnects  slotDefaultCollectionsChanged, which it probably shouldn't as we want to get updates for changed default collections. It of course works around the problem of the loop, but probably introduces a new bug.

Here's the analysis from thiago from 323068:
1) when KMail starts, I suppose MailCommon::Kernel::initFolders is called to initialise the folder listing
2) MailCommon::Kernel::initFolders creates a Akonadi::SpecialMailCollectionsDiscoveryJob job (derived from Akonadi::SpecialCollectionsDiscoveryJob)
3) when Akonadi::SpecialCollectionsDiscoveryJob finishes, Akonadi::SpecialCollectionsDiscoveryJob::slotResult is called
4) Akonadi::SpecialCollectionsDiscoveryJob::slotResult iterates over the result (201 folders). For two folders, it calls:
68                  d->mSpecialCollections->registerCollection(collection.attribute<SpecialCollectionAttribute>()->collectionType(), collection);
  The two folders have mRemoteId = "outbox" and mRemoteId = "caixa de saída" (don't ask me why one is localised and the other isn't).
5) because of those calls, Akonadi::SpecialCollectionsPrivate::emitChanged gets called
6) since those two folders have resourceId == mDefaultResourceId, the defaultCollectionsChanged() signal gets called, which ends up calling MailCommon::Kernel::initFolders
7) repeat ad nauseam
Comment 35 Raul Fernandes 2013-12-12 11:21:40 UTC
I agree with you.
It is not the right way to fix it but it is a workaround.
At least, it stops the behavior.
The solution is avoid the infinite loop through initFolders().
We are talking about it in reviewboard.
https://git.reviewboard.kde.org/r/114341/
Comment 36 Con Kolivas 2013-12-24 21:51:15 UTC
I opened akonadiconsole browser and somehow found I had both a trash and wastebin folder in my local folders (despite using only imap and not the local folders). After deleting the trash folder, restarting the akonadi server and killing off a stuck kmail instance, all the hanging and high cpu usage mysqld process went away. Seems in my case the problem was due to there being multiple wastebins under different names.
Comment 37 Martin Steigerwald 2015-05-04 14:08:36 UTC
Thank you for reporting the bugs and for all your comments and research.

Does this still happen with KDEPIM 4.14.6/7? Daniel, Christian, do you still see this?
Comment 38 PeterF 2015-05-04 14:23:32 UTC
Sorry, I am still on KDE 4.14.2, but I have not experienced any problems with high CPU load for a long time now.
Comment 39 David Tonhofer 2016-01-26 22:38:29 UTC
I am seeing aknoadiserver going to 100% forever once kmail is started.

Even with kmail off, akonadi keeps working on the IMAP folders like a madman:

/usr/bin/akonadi_control
 \_ akonadiserver
 |   \_ /usr/libexec/mysqld --defaults-file=/home/hobbes/.local/share/akonadi/mysql.conf --datadir=/home/hobbes/.local/share/akonadi/db_data/ --socket=/tmp/akonadi-hobbes.aB9Ele/mysql.socket
 \_ /usr/bin/akonadi_agent_launcher akonadi_akonotes_resource akonadi_akonotes_resource_0
 \_ /usr/bin/akonadi_archivemail_agent --identifier akonadi_archivemail_agent
 \_ /usr/bin/akonadi_baloo_indexer --identifier akonadi_baloo_indexer
 \_ /usr/bin/akonadi_birthdays_resource --identifier akonadi_birthdays_resource
 \_ /usr/bin/akonadi_agent_launcher akonadi_contacts_resource akonadi_contacts_resource_0
 \_ /usr/bin/akonadi_followupreminder_agent --identifier akonadi_followupreminder_agent
 \_ /usr/bin/akonadi_agent_launcher akonadi_ical_resource akonadi_ical_resource_0
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_10
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_11
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_12
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_13
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_14
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_15
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_16
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_17
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_18
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_19
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_2
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_20
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_21
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_3
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_4
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_5
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_6
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_7
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_8
 \_ /usr/bin/akonadi_imap_resource --identifier akonadi_imap_resource_9
 \_ /usr/bin/akonadi_agent_launcher akonadi_maildir_resource akonadi_maildir_resource_0
 \_ /usr/bin/akonadi_maildispatcher_agent --identifier akonadi_maildispatcher_agent
 \_ /usr/bin/akonadi_mailfilter_agent --identifier akonadi_mailfilter_agent
 \_ /usr/bin/akonadi_migration_agent --identifier akonadi_migration_agent
 \_ /usr/bin/akonadi_newmailnotifier_agent --identifier akonadi_newmailnotifier_agent
 \_ /usr/bin/akonadi_notes_agent --identifier akonadi_notes_agent
 \_ /usr/bin/akonadi_sendlater_agent --identifier akonadi_sendlater_agent

This is Fedora 22:

- akonadi-1.13.0-16
- kdepim-runtime-libs-4.14.10-5

How do I confirm the problem?
Comment 40 Martin Steigerwald 2016-01-27 07:59:23 UTC
Please retest with most recent KDEPIM + Akonadi 15.12.

It has a ton of performance fixes that are not going to be backported due to shift from Akonadi from Qt4 to Qt5.
Comment 41 Martin Steigerwald 2016-01-27 08:37:55 UTC
Also please carefully check whether the issue you see is the same as this issue. I already have the impression that this bug contains more than one issue. If at doubt, please rather open a new issue.

Is Akonadi actually fetching mails? If in doubt please open a new mail and include more information. The processlist does not do much to aid in debugging. See for example:

https://techbase.kde.org/Projects/PIM/Akonadi/Development_Tools

At least some information on what Akonadi is actually doing during high load (debugger) would be interesting.

And in case of IMAP issues (you seem to have a huge lot of IMAP accounts configured):
https://techbase.kde.org/Projects/PIM/Akonadi/Debug_IMAP

As performance fixes are unlikely to be backported, I suggest doing these debugging steps only if the issue persists with KDEPIM 15.12 or later.
Comment 42 Andrew Crouthamel 2018-09-25 21:41:54 UTC
Dear Bug Submitter,

This bug has been in NEEDSINFO status with no change for at least 15 days. Please provide the requested information as soon as possible and set the bug status as REPORTED. Due to regular bug tracker maintenance, if the bug is still in NEEDSINFO status with no change in 30 days, the bug will be closed as RESOLVED > WORKSFORME due to lack of needed information.

For more information about our bug triaging procedures please read the wiki located here: https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging

If you have already provided the requested information, please set the bug status as REPORTED so that the KDE team knows that the bug is ready to be confirmed.

Thank you for helping us make KDE software even better for everyone!
Comment 43 Andrew Crouthamel 2018-10-27 03:49:30 UTC
Dear Bug Submitter,

This bug has been in NEEDSINFO status with no change for at least 30 days. The bug is now closed as RESOLVED > WORKSFORME due to lack of needed information.

For more information about our bug triaging procedures please read the wiki located here: https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging

Thank you for helping us make KDE software even better for everyone!