Bug 338658

Summary: GMail, Novell Groupwise, other IMAP: "Multiple merge candidates, aborting"
Product: [Frameworks and Libraries] Akonadi Reporter: Paul Gideon Dann <pdgiddie+kde>
Component: libakonadiAssignee: kdepim bugs <kdepim-bugs>
Status: RESOLVED FIXED    
Severity: grave CC: aaronw, ach, amantia, arne_bab, aron.heinecke, asturm, bluelightning, bugs.kde.org, bugs.kde, christian.saemann, cmdln, crglasoe, dav1dblunk3tt, diego.ml, dschridde+kde, dubois.michel, dvratil, egdfree, ehakanduran, eric.brunet, felixonmars, gaaf, graham, halla, igilama, jani-matti.hatinen, jsardid, jstammi, justin, kare.sars, kde-bugs, kde.kfoar, kde, KDE, kdebugs, kdenis, kdepim-bugs, keplicz, kevin, kfunk, kos.na.bordel, kretz, kuba, lacsilva, lari.natri, lpetersen, m.krasilnikov, m.seifert, mail, mel, moviuro+kde, ndcunliffe, netwiz, perezmeyer, peter, piorek, plucinski.mariusz, public, ralsa, rasasi78, rdieter, renard, rjwgnr27, rkitover, sebastian.schrader, sgh, simonandric5, slett, stephan.diestelhorst, stillcompiling, thatskinnywhiteguy+kde, thuryn1, vkrause, wellendorf
Priority: HI    
Version: 5.7.2   
Target Milestone: ---   
Platform: unspecified   
OS: Linux   
Latest Commit: Version Fixed In: 5.12.0
Attachments: Backtrace shown in Konsole for akonadi no longer showing mails from within maildir
attachment-21853-0.html
experimental patch

Description Paul Gideon Dann 2014-08-29 15:19:54 UTC
After upgrading to Akonadi 1.13.0 / Kontact 4.14.0 I've been getting quite a few notifications saying:

"Multiple merge candidates, aborting"

I'm getting this on both of the GMail accounts I've set up with. I've tried removing the akonadi database at ~/.local/share/akonadi. This greatly reduced the number of messages I was receiving, but once Akonadi had finished caching what it needed in the database, these messages started occurring regularly again (though only one at a time now), presumably when it polls for new mail.

I've tried shutting down Akonadi and running "akonadictl fsck", but that doesn't have any effect.

What can I do to help diagnose the issue?
Comment 1 Paul Gideon Dann 2014-08-29 15:21:18 UTC
Sorry, I intended to specify "...on both of the GMail accounts I've set up with *IMAP*"
Comment 2 Ralsa 2014-08-30 12:57:32 UTC
I have the problem into Kubuntu 14.04, too.
Comment 3 mel 2014-09-01 08:31:02 UTC
Since my previous system upgrade of Debian Testing (Jessie) on Friday, I'm seeing this too. Versions are the same as the original bug reporter.

Specifically, it's when syncing my Google IMAP "All Mail" folder, which still shows several unread messages, despite me having read them in another folder. Before this most recent update, the "unread" messages would eventually disappear from my All Mail folder on their own (ie. at the next automatic sync of that folder).
Comment 4 Paul Gideon Dann 2014-09-01 08:53:59 UTC
Agreed: I have also noticed that whereas previously when mail was marked as Read in one folder, it would eventually be marked as Read in another, this no longer happens. I'm sure that's no coincidence :p
Comment 5 km 2014-09-12 16:20:05 UTC
I can confirm both problems on opensuse, same versions.
Comment 6 Thomas Gideon 2014-09-17 21:23:17 UTC
I *just* started seeing this in Kontact 4.14 with kdepim-runtime pinned to 4.13.3 after having some problems with 4.14 of kdepim-runtime. I am *not* using Gmail, but IMAP against a courier-imap server. I was having some issues with messages reappearing after deletion or moving our of my inbox and after three or four times repeating the delete/move, I now get this message when viewing my inbox.
Comment 7 flyos 2014-09-23 10:32:05 UTC
I can confirm this bug in Kubuntu 14.04 using kdepim-runtime 4.14.0 and akonadi server 1.12.91.

My e-mail account is NOT a Gmail one. So the problem is not Gmail exclusive.
Comment 8 Achim Bohnet 2014-10-03 16:48:46 UTC
Same here: Akonadi 1.2.91 IMAP resource (server is dovecot) & kmail from 4.14.0 and 4.14.1

Maybe related:  Sometimes this resource stops syncing, e.g. at 54 or 75% and hangs.  I've to stop/start akonadi to get new mail again.

Achim
Comment 9 Paul Gideon Dann 2014-10-06 07:55:19 UTC
Yes, I'm getting the same thing: the imap resources get stuck, and I have to "killall axonadi_imap_resource", and then restart them via the Akonadi Console in order to un-stick them. I've pretty much abandoned KMail for now as I can't use it for more than 10mins before it gets stuck. I've had to fall back to the GMail web app until this is resolved.
Comment 10 Achim Bohnet 2014-10-06 14:33:40 UTC
FWIW a head up info:  developers discussing the 'multiple merge candidats' on kde-pim@kde.org.

 http://lists.kde.org/?l=kde-pim&m=141241776131472&w=2

So despite the silence here, there's aktivity to get the bug fixed.

Achim
Comment 11 Robert G. Siebeck 2014-11-24 10:15:47 UTC
*** This bug has been confirmed by popular vote. ***
Comment 12 Peter Wu 2014-11-24 23:16:49 UTC
I experience the same error which results in a burning CPU for (a) minute(s), increased data usage, inresponsive KMail (as the full mail boxes have to be retrieved).

Affected IMAP folders:
 - GMail "All Mail" (possible changes: removed message (to Trash), read/unread state, Archive (move to All Mail))
 - Dovecot mail server: 12k mailing list archive, 7k mailing list archive. Read/unread status, possibly KMail auto-archiving messages older than X days.

Any workaround/fix for this productivity killer? I have been holding back akonadi/kdepim updates on Arch Linux since September, but recently boost updated and I couldn't keep the old combination working anymore.
Comment 13 Paul Gideon Dann 2014-11-25 08:56:43 UTC
Sadly, I've had to abandon KMail for the time being, as either this issue or something related to it is causing the Akonadi agents for my GMail IMAP to stop responding. I can retrieve e-mail if I kill and restart them, but they only last for a couple of minutes before freezing up again. I've had to resort to the GMail web interface for several months now :(
Comment 14 Andrew Udvare 2014-12-08 08:57:12 UTC
Same boat as all of you. I hate the Gmail web interface (even Inbox). I have multiple IMAP clients going (iPhone, iPad, Mail on Mac) and those work, but KMail almost never does as of late. Only recently it started to get really bad and basically always fails to sync the 'All Mail' directory. This stops everything from working pretty much. It gets to 99% and stays there forever. Restarting the Akonadi server does not help very much. I have one Gmail account with emails dating back to 2004, other IMAP clients have no problem viewing that account's 'All Mail' folder back to the first entry.

Continuously, my accounts in Akonadi Console say 'Connection established.' as opposed to 'Ready'. This is completely random. Right-clicking on that one and choosing 'Restart agent' does nothing. Neither does Toggle offline/online.

I tried to set everything to live mode as well, where content is downloaded on an as-needed basis. This has only resulted in me getting the 'Retrieving Folder Contents' message, and it never goes away.

Really not sure what to do. KMail was great in KDE 3. I've tried to live with Akonadi integration in KDE 4 and even tuned my PostgreSQL server for it. Even with debug tools like Akonadi Console and enabling the PostgreSQL general log, I cannot tell what the problem is. Every once in a while I see that Google denied the connection, and that makes sense. But my other IMAP clients do not generally get this issue and they even have check intervals of 30 minutes.

I really hope this is figured out. Although KMail lacks a 'Archive' feature like in Mail.app on Mac, it still is my favourite e-mail client on Linux.
Comment 15 Iven Hsu 2014-12-30 02:15:49 UTC
I'm also getting this. I'm not using GMail, but Outlook Web App.
Comment 16 Moviuro 2015-01-12 17:40:19 UTC
I confirm the issue on Archlinux with Akonadi 1.13.0, though it is a new issue to me.
Comment 17 E. Hakan Duran 2015-02-07 21:40:48 UTC
Same here with Fedora 21, and akonadi-1.13.0-8.
Comment 18 Andreas K. Huettel 2015-02-09 14:44:00 UTC
Same here observed with Novell Groupwise server. Gentoo, kmail 4.14.3, akonadi 1.13.0
Comment 19 rjwgnr27 2015-02-18 19:39:44 UTC
I get this using Kontact 4.14.4, but seen it since several updates.

I get this against my work e-mail against Exchange imap server. The trigger seems to be when I delete some messages from my Android phone, then delete some on my desktop via KMail.

Emptying the trash folder on the exchange account seems to clear this up, until the next time I mix deletes. I notice that once I empty the trash, the messages stop, but previously deleted emails show up in the trash folder.
Comment 20 Piotr 2015-02-20 18:19:15 UTC
Anybody knows what is the status of this one? There are no new mails in the linked mailing list thread.
Comment 21 Peter Wu 2015-02-20 18:57:25 UTC
No idea, I have meanwhile tried Thunderbird and am now using mutt. All of the three mail clients have their issues.
Comment 22 Paul Eggleton 2015-02-24 14:03:58 UTC
I have at least figured out how to fix the database in order to get directories syncing again using the Akonadi Console. (Disclaimer: this worked for me, may not work for you, you might lose data if you aren't careful, etc.)

First I searched to find the duplicate keys using the "DB Console" tab with the following query:

SELECT pimitemtable.*, collectiontable.name FROM pimitemtable
INNER JOIN pimitemtable dup ON pimitemtable.id != dup.id and pimitemtable.remoteId = dup.remoteId and pimitemtable.collectionId = dup.collectionId
INNER JOIN collectiontable ON pimitemtable.collectionId = collectiontable.id
ORDER by pimitemtable.remoteId

Then I looked for the items specifically in the folders that I knew were failing to sync with the "Multiple merge candidates, aborting" error, then used the Id to track down one of the duplicate items in the "Browser" tab and then deleted it there from the context menu. (There are still others with duplicate ids, but I have not deleted those since they don't appear to be causing any problems.) I then restarted the akonadi server, and then reloaded the folder in KMail and voila, the folder now synchronises successfully.
Comment 23 gajdos.mirek 2015-03-09 11:35:58 UTC
Still affected with akonadi 1.13.0-3, workaround by Paul worked for me.
Comment 24 Christo 2015-03-16 22:42:12 UTC
also affected

Paul: your query sadly didnt work for me :-( 

my versions are: (OpenSuse 13.2)
akonadi-runtime-1.13.0-2.2.3.x86_64
akonadi-4.14.5-12.5.x86_64
Comment 25 Jani-Matti Hätinen 2015-03-20 02:40:21 UTC
Also affected.

Will dig this out (have to) and will find out the retard who wrote this piece of shit code.
Comment 26 Jani-Matti Hätinen 2015-03-20 02:48:06 UTC
The root cause is that if you move an e-mail (or any node basically) into a folder, which already has the same identical e-mail/node, this error pops up.

So instead of doing the only sane thing imaginable (i.e. seeing that that the e-mails/nodes are identical copies) this piece of shit errors out without even giving the user an idea where problem is. Whoever wrote this should never touch a keyboard again.
Comment 27 Christo 2015-03-20 04:34:17 UTC
the heartbleed-guy didit ;-)
Comment 28 Jani-Matti Hätinen 2015-03-20 04:47:30 UTC
(In reply to Christoph from comment #27)
> the heartbleed-guy didit ;-)

Sorry, but that's just ignorant. Even with the smiley.
Comment 29 Stephan Diestelhorst 2015-05-05 08:59:59 UTC
I just found a way to reproduce this bug.  I use GMail and have filters in there that automatically add tags to email, but still leave that email in the inbox.  If I then use KMail to sort through my inbox, I sometimes end up moving the message to the folder corresponding to the tag that is already present.
This seems to toggle the issue of having multiple merge candidates, GMail has the same id for the email, while KMail / Akonadi think they are two different IDs who just happen to share the same remote ID.
I am not an IMAP / Akonadi expert, but it seems to me that these cases could be merged relatively easily.  Both messages have the same remote ID, and they should not be different in the views of the Akonadi cache.
Comment 30 Diego 2015-07-15 10:18:26 UTC
einar77 on IRC helped me to go around this problem by resetting Akonadi cache:
akonadiconsole -> browser -> right click on folder / account, "clear akonadi cache", but this will prompt a full download again of the mail

The above suggestion is not a solution for the problem, but at least it mitigates the problem by providing a manual fix.
Comment 31 Justin Zane Chudgar 2015-07-30 04:02:22 UTC
I'm rather shocked that a "GRAVE" bug that has been confirmed and assigned seems to be completely ignored. Would the assignee please update the bug so that all those of us interested can be apprised of progress made or help needed.
Comment 32 gajdos.mirek 2015-07-30 04:08:24 UTC
Workaround: if you need to not be disturbed by errors issued when checking emails automatically every few minutes, go to offline mode in kmail. Then it laments only once. (sigh)
Comment 33 Igilama 2015-08-18 09:18:05 UTC
I have the same problem - used the fix suggested by  Paul Eggleton and it worked. I can't understand how this issue can be reported for over a year without single comment by any developer.

This really doesn't show KDE team in the best light, and the fix seems to be quite trivial.

I love KDE, so these kinds of thing are quite a disappointing.
Comment 34 Igilama 2015-08-24 12:47:38 UTC
OK, I was too quick to assume the problem was fixed  - the workaround worked for a single instance of the problem but it is occurring again semi-regularly.

Kontact
Version 4.14.7

I'm using filters, it's possible that the filter engine copies the message to another folder but fails to delete it from the original one ... just guessing here, I'm willing to debug the problem with a developer, is anybody willing to take this on?

Thanks
Comment 35 Andreas Sturmlechner 2015-08-25 10:02:45 UTC
Paul, thanks to your manual I just fixed the issue on my system. Never received an error message, kmail just stopped syncing.

Time to test this bug on kdepim-5, who gets it first?
Comment 36 Jakub Caban 2015-08-25 10:28:44 UTC
I'm on it for like 2 weeks now and this problem haven't happened so far.

But that doesn't mean it's gone due to random occurring :(
Comment 37 Andreas Sturmlechner 2015-08-25 11:40:49 UTC
It's not random, read comment #29 how to reproduce.
Comment 38 Jakub Caban 2015-08-25 16:34:15 UTC
If I understand correctly - I've just move message from GMail inbox to the tag folder. Now I have 2 identical message in the tag folder and no error message was shown.
Comment 39 Daniel Vrátil 2015-09-13 14:02:03 UTC
Hi all,

fix has been pushed to old KDE/4.14 branches of kdepimlibs and kdepim-runtime as well as Applications/15.08 branches (the current KF5-based stable branches) and master. KDE/4.14 won't see any more releases, but if your distribution does not ship KDE Applications 15.08 yet, please poke them to get following commits:

kdepimlibs.git: 442961918d8f3d9ff94fd4593c9b09d1e89d6bc0
kdepimlibs.giit: d8b5da7bb16bfd3652e83200d851af3a21816469
kdepim-runtime.git: 93a2baac05a325b688aea2cc12d9714d6b186f69

If you distribution ships KDE Applications 15.08 the update will arrive as part of KDE Applications 15.08.2 (unfortunately we missed the 15.08.1 tagging). You can ask your distributions to add following patches:

kdepimlibs.git: 43d5659a88a6ebb3423c6228986f0bd1e1f496f7
kdepimlibs.git: ffa20e75f388bfa87530e113641f0830a5a58ec4
kdepim-runtime:git 038c604aba0cac22275e03c3497672cd254c2568

I'm really sorry it took so long to pin this issue down and fix it.
Comment 40 Michael Seifert 2015-09-13 21:18:33 UTC
Hi Daniel,

that's great news!
It's good to see this issue finally sorted out. Thanks to your pointers, the Gentoo Devs already integrated the fixes for KDE Applications 15.08 and the patches for KDE 4.14.10 are soon to come.

Thanks for caring!
Comment 41 Igilama 2015-09-14 09:54:07 UTC
Thank you very much, I've added ppa:kubuntu-ci/stable in hopes it will be fixed there first, so I'll observe for a while whether the error stops ;)

Thank you again, it's great to see bugs being fixed.
Comment 42 Arne Babenhauserheide 2015-09-22 22:48:32 UTC
Hi Daniel,

Thank you very much!

I only now discovered the error message after wondering for 3 months why I got no updates, and thanks to you searching for the solution found the best answer: “fixed two weeks ago, will be included in the next release”.

Best wishes,
Arne
Comment 43 Alvise 2015-10-17 17:15:43 UTC
Nah.

Just upgraded to 15.08.2 but still get the following message:
AgentBase(akonadi_imap_resource_1): Multiple merge candidates, aborting
Comment 44 Michael Seifert 2015-10-17 17:20:59 UTC
Have you tried readding the IMAP accounts to kmail?
I had to do this before the issue was gone.
Comment 45 Witko 2016-02-04 14:54:49 UTC
Happens to me on akonadi version 15.12.1. Also recreated the IMAP resources week or two ago.
Comment 46 Ovidiu-Florin BOGDAN 2016-02-09 14:13:35 UTC
I've kind of found a workaround for this issue and I've blogged about it here: http://ovidiu.geekaliens.com/en/2016/01/05/multiple-merge-candidates-aborting-workaround/

I hope it helps.
Comment 47 Witko 2016-02-09 14:16:53 UTC
Yes i was already thinking about linking it here. The issue is that i got this failure mostly on MS Exchange IMAP. Cos i have 2 gmail accounts and one MS Exchange. All IMAP.
On the MS Exchange i have multiple folders + kmail filtering(moving from Inbox to folder)
Im not sure how to move it on MS Exchange. Wil try to find out.
Comment 48 Chris 2016-03-25 14:27:29 UTC
(In reply to Paul Eggleton from comment #22)
> I have at least figured out how to fix the database in order to get
> directories syncing again using the Akonadi Console. (Disclaimer: this
> worked for me, may not work for you, you might lose data if you aren't
> careful, etc.)
> 
> First I searched to find the duplicate keys using the "DB Console" tab with
> the following query:
> 
> SELECT pimitemtable.*, collectiontable.name FROM pimitemtable
> INNER JOIN pimitemtable dup ON pimitemtable.id != dup.id and
> pimitemtable.remoteId = dup.remoteId and pimitemtable.collectionId =
> dup.collectionId
> INNER JOIN collectiontable ON pimitemtable.collectionId = collectiontable.id
> ORDER by pimitemtable.remoteId
> 
> Then I looked for the items specifically in the folders that I knew were
> failing to sync with the "Multiple merge candidates, aborting" error, then
> used the Id to track down one of the duplicate items in the "Browser" tab
> and then deleted it there from the context menu. (There are still others
> with duplicate ids, but I have not deleted those since they don't appear to
> be causing any problems.) I then restarted the akonadi server, and then
> reloaded the folder in KMail and voila, the folder now synchronises
> successfully.

This does work in Kmail5.  Has anyone tried the remove duplicate email option in the folder?
Comment 49 Chris 2016-03-25 14:31:22 UTC
(In reply to Ovidiu-Florin BOGDAN from comment #46)
> I've kind of found a workaround for this issue and I've blogged about it
> here:
> http://ovidiu.geekaliens.com/en/2016/01/05/multiple-merge-candidates-
> aborting-workaround/
> 
> I hope it helps.

This also works in Kmail5.  Paul Eggleton posted a work around using Akonadiconsole which works as well.  I'll ask the same question here as well.  Does the remove duplicate emails function in "folders" accomplish the same result?

I haven't seen this question asked (may have missed it) but, does filtering in Kmail conflict with filtering in Gmail?
Comment 50 Daniel Vrátil 2016-03-30 23:57:51 UTC
The only fix for the "Multiple merge candidates" error is to run Akonadi Console, go to "Browser" tab, right-click the broken folder and select "Clear Akonadi cache". Then restart Akonadi and it will re-fetch content of the broken folder from the IMAP server.
Comment 51 Andreas K. Huettel 2016-03-31 15:41:59 UTC
(In reply to Daniel Vrátil from comment #50)
> The only fix for the "Multiple merge candidates" error is to run Akonadi
> Console, go to "Browser" tab, right-click the broken folder and select
> "Clear Akonadi cache". Then restart Akonadi and it will re-fetch content of
> the broken folder from the IMAP server.

Dan, if I've been working on the folder in the meantime locally, in particular moving e-mails into that folder, are these mails lost then?
Comment 52 Kåre Särs 2016-04-14 07:37:24 UTC
I'm using Kubuntu 16.04 (beta) and the neon repository: KMail 5.1.1, Akonadi 15.12.1 and Frameworks 5.21

If I by mistake leave KMail running at work when I go home and start KMail at home I get this problem 9 times out of 10 when I get back to work. I use local filtering both at work and at home.

I guess the problem is that the filtering is moving the message to the filtered folder twice because the move is not atomic.

It would be better to show a dialog explaining the problem and ask the user what version to use than just failing if we don't want to potentially destroy data. If I don't enable the more detailed progress information I don't even get informed about the failure.
Comment 53 Andreas Sturmlechner 2016-06-05 21:30:56 UTC
I've been wrestling with akonadi during the past months (15.12.0 - 16.04.1) using `akonadictl fsck` and `akonadictl restart` whenever I clearly knew there really was new mail on the (gmail) server while kmail kept silent - which could happen several times a day. Today, by chance I've met an old friend again, "Multiple merge candidates, aborting", so maybe it had been that problem all the time. Above SQL query though revealed _a lot_ of duplicates, so maybe it is the result of all the times akonadi goes on a 'several thousand new mails in random subfolder' frenzy (also can happen a couple o' times a day). Either way, I wouldn't delete them one by one, so I came up with another SQL statement:

DELETE FROM pimitemtable
WHERE pimitemtable.id in (
  SELECT id
  FROM pimitemtable
  LEFT OUTER JOIN (
    SELECT MIN(pimitemtable.id) as RowId, pimitemtable.remoteId, pimitemtable.collectionId
    FROM pimitemtable
    GROUP by pimitemtable.remoteId, pimitemtable.collectionId
  ) as KeepRows ON pimitemtable.id = KeepRows.RowId
  WHERE KeepRows.RowId IS NULL
)


Beware, this might kill n kittens if you ran it on your system.
Comment 54 Stephan Diestelhorst 2016-06-10 20:28:11 UTC
For me mysql complained about "You can't specify target for update in FROM clause".  Wrapping things into another layer of SELECT did the trick:

DELETE FROM pimitemtable WHERE pimitemtable.id in (
  SELECT id FROM (
    SELECT id FROM pimitemtable
    LEFT OUTER JOIN (
      SELECT MIN(pimitemtable.id) as RowId, pimitemtable.remoteId, pimitemtable.collectionId FROM pimitemtable GROUP by pimitemtable.remoteId, pimitemtable.collectionId )
    as KeepRows ON pimitemtable.id = KeepRows.RowId
    WHERE KeepRows.RowId IS NULL)
  AS foo)
Comment 55 Andreas Sturmlechner 2016-07-03 21:30:28 UTC
(In reply to Stephan Diestelhorst from comment #54)
> For me mysql complained about "You can't specify target for update in FROM clause".
Yep, my above solution was done with PostgreSQL.

And today I had to use it again. 6 fresh duplicates after 16.04.2 had been really working well for a week or two.
Comment 56 Luis Silva 2016-08-03 19:04:16 UTC
*** Bug 355498 has been marked as a duplicate of this bug. ***
Comment 57 Luis Silva 2016-08-03 19:08:06 UTC
This bug is affecting all my imap accounts both at home and at work to the point of making kmail useless. 

New duplicates often appear after after I move mails between folders.
Comment 58 Luis Silva 2016-08-03 19:09:39 UTC
*** Bug 335776 has been marked as a duplicate of this bug. ***
Comment 59 nicholas 2016-12-30 08:46:42 UTC
is this ever going to get fixed? Reporting bugs to KDE is heartbreaking. You spend time tracking things down, file a report which you might as well have sent to /dev/null.
Comment 60 Daniel Vrátil 2017-01-07 11:39:21 UTC
Git commit 66b87061f523e392236d73dea668ddf6772eb545 by Daniel Vrátil.
Committed on 07/01/2017 at 11:07.
Pushed by dvratil into branch 'Applications/16.12'.

Reset Item RID on both inter- and intra-resource MOVE

This should significantly help to mitigate the "Multiple Merge
Candidates" error during sync. One of the major reasons why MMC
happens is when user moves an Item belonging to an IMAP resource
to another Collection. On IMAP RID corresponds to IMAP UID. If
you move such an Item into another Collection which has IMAP
UIDNEXT greater than the source Collection, then there's a high
chance for RID conflict until the Resource replays the change
and updates the RID of the moved Item.

This change reset RID of all moves Items and only emits the RID
as part of the change notification. The Monitor then retrieves the
Item without the RID and restores the RID from the Notification,
so this is absolutely transparent to Resources.
FIXED-IN: 5.4.1

M  +7    -0    src/core/monitor_p.cpp
M  +13   -19   src/server/handler/move.cpp

https://commits.kde.org/akonadi/66b87061f523e392236d73dea668ddf6772eb545
Comment 61 Daniel Vrátil 2017-01-07 12:18:56 UTC
*** Bug 352249 has been marked as a duplicate of this bug. ***
Comment 62 jstammi 2017-01-12 13:56:48 UTC
For me this does not happen for IMAP folders but for local, maildir, ones. Actually again after each deletion of an email.

Does this sound related?

Running an up-to-date ARCH installation actually containing akonadi 16.12.0-1.

Some more details can be found here: https://bbs.archlinux.org/viewtopic.php?pid=1682923#p1682923
Comment 63 jstammi 2017-01-13 10:38:03 UTC
Today the 5.4.1 arrived for my ARCH installation. The problem is not gone unfortunately - at least the one that my mail contents/folder contents do no longer show up after some deletion(s). Symptoms seem to have changed a little bit as it no longer happens immediately after each deletion. But after doing some more I still have to restart the akonadi server to get mails displayed again.

The Debugger tab of the Akonadi Console shows in the lower part some error messages, that arised after last re-start of the akonadi server until kmail being blocked:

AgentBase(akonadi_maildir_resource_0): Multiple merge candidates, aborting
AgentBase(akonadi_maildir_resource_0): Multiple merge candidates, aborting

The Konsole window having restarted the akonadi server from the last time () shows backtrace that I will attach in a moment.
Comment 64 jstammi 2017-01-13 10:40:39 UTC
Created attachment 103395 [details]
Backtrace shown in Konsole for akonadi no longer showing mails from within maildir
Comment 65 Dennis Schridde 2017-02-09 18:20:50 UTC
I also see this with KDE Applications 16.12.1 and KDE Frameworks 5.30.1 (and KDE Plasma 5.9.1, but that should be unrelated).

In this bug it says "Version Fixed In: 5.4.1" - version 5.4.1 of what exactly? I would think that I am well beyond 5.4.1 in both areas...
Comment 66 Ovidiu-Florin BOGDAN 2017-02-09 18:23:47 UTC
(In reply to Dennis Schridde from comment #65)
> I also see this with KDE Applications 16.12.1 and KDE Frameworks 5.30.1 (and
> KDE Plasma 5.9.1, but that should be unrelated).
> 
> In this bug it says "Version Fixed In: 5.4.1" - version 5.4.1 of what
> exactly? I would think that I am well beyond 5.4.1 in both areas...

Of KDE PIM. AFAIK it has its own versioning besides the KDE applications.
Comment 67 Dennis Schridde 2017-02-09 18:26:32 UTC
The workaround is also documented here: https://wiki.meurisse.org/wiki/Workarounds#Kmail:_Multiple_merge_candidates.2C_aborting
Comment 68 Dennis Schridde 2017-02-09 18:28:10 UTC
(In reply to Ovidiu-Florin BOGDAN from comment #66)
> (In reply to Dennis Schridde from comment #65)
> > I also see this with KDE Applications 16.12.1 and KDE Frameworks 5.30.1 (and
> > KDE Plasma 5.9.1, but that should be unrelated).
> > 
> > In this bug it says "Version Fixed In: 5.4.1" - version 5.4.1 of what
> > exactly? I would think that I am well beyond 5.4.1 in both areas...
> 
> Of KDE PIM. AFAIK it has its own versioning besides the KDE applications.

I'm on Gentoo and have kde-apps/libkdepim-16.12.1 installed. There is no version 5.4.1 available.
Comment 69 Achim Bohnet 2017-02-10 12:54:15 UTC
On Donnerstag, 9. Februar 2017 18:28:10 CET you wrote:
> https://bugs.kde.org/show_bug.cgi?id=338658
> 
> I'm on Gentoo and have kde-apps/libkdepim-16.12.1 installed. There is no
> version 5.4.1 available.

Don't confuse the KDE Applications Release version with the  Program
version.  It's often the same but not always.

kde applications 16.12.1 release contains 5.4.1: Kmail -> Help -> About Kmail, or

$ kmail --version
kmail2 5.4.1

$ dpkg -l kmail\* | tail -1
ii  kmail          4:16.12.1-0neon+16.04+build7 amd64        full featured graphical email client


Some (most?) PIM maintainers don't want to set the version of their Program
to the same version as the KDE Applications Release.  IMHO confusing for users.

Achim
Comment 70 Dennis Schridde 2017-02-11 08:40:44 UTC
Then I request to reopen this, please.

I saw the message "Multiple merge candidates, aborting" while using KMail 5.4.1 (KDE Applications 16.12.1) as mentioned before (comment #65) and I still see it in KMail 5.4.2 (KDE Applications 16.12.2). KMail also regularly refused to sync the IMAP account where this happens until I restart Akonadi.
Comment 71 Christoph Feck 2017-02-16 03:12:51 UTC
Reopening.

Are there certain steps or specific setups needed to reproduce?
Comment 72 Dennis Schridde 2017-02-16 09:13:38 UTC
(In reply to Christoph Feck from comment #71)
> Are there certain steps or specific setups needed to reproduce?

I have no clue how to gather additional information about the issue. I don't even know what messages or folders cause the error, because there is no error message.

I will recap my setup and symptoms, though:

Setup:
* Server
  - Gentoo Linux
  - Dovecot 2.2.27
    - dbox mailboxes
  - Dovecot Pigeonhole 0.4.16
    - Heavy use of Sieve scripts to sort mailing lists and spam
  - Dovecot Antispam 2.0_pre20130429
    - My spam and ham folders should be monitored and acted upon by this plugin, but it appears to not do anything at all.
* Client 1
  - Gentoo Linux
  - KMail 5.4.2
  - KDE Apps 16.12.2
  - KDE Frameworks 5.31.0
  - KDE Plasma 5.9.2
  - No client side filtering. I use Sieve exclusively.
  - The error appears here.
  - I use this client on a daily basis, when I am at home.
* Client 2
  - Arch Linux
  - KMail 5.4.2
  - KDE Apps 16.12.2
  - KDE Frameworks 5.31.0
  - KDE Plasma 5.9.2
  - No client side filtering. I use Sieve exclusively.
  - The error does not appear here.
  - I use this client only very very seldomly. The machine mostly stays powered off.
* Client 3
  - Roundcube 1.2.3
  - No client side filtering. I use Sieve exclusively.
  - I use this client on a daily basis, when I am not at home.
* Client 4
  - Android 6.0.1 / Cyanogenmod 13
  - Whatever the default mail client is there...
  - I use this client on a daily basis, throughout the whole day.

The mail account itself consists of a lot of (ca. 200 [1]) nested mailboxes (up to 4 levels: /1/2/3/4). Some of these are rather large (10 contain >5k mails, the largest being at 25k [2]).

The following was observed on "Client 1":

* KMail syncs the "sent" folder (7k mails) every time, which takes a few seconds. All other folders just flash by so quickly I can barely read their name.
* After the multiple-merge-candidates error appeared, KMail sometimes continues working (i.e. further clicks on "Check Mail" start a sync), but sometimes this account will not appear on the sync details list (behind up-arrow at bottom right of screen) again, until I run "akonadictl restart".
* There is no visible error message (not counting the one displayed in the sync details list, because it is hidden behind a button, only displayed very briefly and without an indication that it is an error).
* There is no hint what mail or mailbox is causing this.
* KMail never recovers from this error. I.e. it does not merge the mails automatically or ask me for help.

[1]: doveadm -f flow fetch -u $USER mailbox-guid all | sort -u | wc -l
[2]: doveadm -f flow fetch -u $USER mailbox-guid all | sort | uniq -c | sort -n
Comment 73 Dennis Schridde 2017-02-16 09:47:40 UTC
(In reply to Dennis Schridde from comment #72)
> (In reply to Christoph Feck from comment #71)
> > Are there certain steps or specific setups needed to reproduce?

If that's of any help: While writing comment #72, mail was syncing fine. But when I returned to my computer just now, it had stopped and I had to run "akonadictl restart" again. Already the mail emitted by bugzilla for comment #72 did not arrive at my client anymore.
Comment 74 Dennis Schridde 2017-06-25 18:57:03 UTC
I am at KDE Applications 17.04.2 now and the problem appeared again:
```
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Unknown error."
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Creating/updating items 
from the akonadi database failed: "Multiple merge candidates, aborting"
akonadi_imap_resource[12572]: org.kde.pim.akonadicore: Error during ItemSync:  
"Multiple merge candidates, aborting"
```
(All messages appear in the log within the same second.)

I also see this a little bit later, but it is probably not related?
```
kmail[5475]: org.kde.pim.kmanagersieve: void KManageSieve::SessionThread::slotSocketError() "Unknown error"                                                     
kmail[5475]: org.kde.pim.kmanagersieve: "session3" No job for reporting this error message! "Could not connect to host Unknown error."                          
```
Comment 75 Dennis Schridde 2017-06-25 19:00:00 UTC
(In reply to Dennis Schridde from comment #74)

The workaround still works: https://wiki.meurisse.org/wiki/Workarounds#Kmail:_Multiple_merge_candidates.2C_aborting

Could this please be automated?
Comment 76 Steven Haigh 2017-06-29 04:54:19 UTC
For what its worth, I still see this with Fedora 26.

 rpm -qa | grep kmail | sort
kf5-kmailtransport-17.04.1-1.fc26.x86_64
kf5-kmailtransport-akonadi-17.04.1-1.fc26.x86_64
kmail-17.04.1-3.fc26.x86_64
kmail-account-wizard-17.04.1-1.fc26.x86_64
kmail-libs-17.04.1-3.fc26.x86_64

$ rpm -qa | grep akonadi | sort
akonadi-calendar-tools-17.04.1-1.fc26.x86_64
akonadiconsole-17.04.1-1.fc26.x86_64
akonadi-import-wizard-17.04.1-1.fc26.x86_64
kf5-akonadi-calendar-17.04.1-1.fc26.x86_64
kf5-akonadi-contacts-17.04.1-1.fc26.x86_64
kf5-akonadi-mime-17.04.1-1.fc26.x86_64
kf5-akonadi-notes-17.04.1-1.fc26.x86_64
kf5-akonadi-search-17.04.1-1.fc26.x86_64
kf5-akonadi-server-17.04.1-3.fc26.x86_64
kf5-akonadi-server-mysql-17.04.1-3.fc26.x86_64
kf5-kmailtransport-akonadi-17.04.1-1.fc26.x86_64
kf5-libkdepim-akonadi-17.04.1-1.fc26.x86_64
kf5-mailimporter-akonadi-17.04.1-1.fc26.x86_64
kf5-pimcommon-akonadi-17.04.1-1.fc26.x86_64
Comment 77 Sebastien Renard 2017-08-05 15:57:25 UTC
I confirmed this issue with kmail 5.5.2 (openuse 42.3). It also happened with local folders (maildir). Akonadi restart does not work. 
Two workarounds works :
- clear akonadi cache on the folder and restart akonadi and kmail
- connect to mysql to delete items (see comment 75)

It happened multiple times per day and make kmail amost unusable...
Comment 78 Piotr Keplicz 2017-08-17 10:03:51 UTC
It happens in KMail 5.5.3 (KDE Neon) on a regular basis in a local folder for a mailing list. The mails are moved to this folder using a standard mailing list filter on incoming mail from a POP3 account.
Comment 79 Francis Herne 2017-09-15 13:18:22 UTC
Still reproducible on Arch with KMail 5.6.1 and Akonadi 17.08.

Akonadi hangs after each "Check Mail" and must be restarted to receive any more mail. The steps described here https://www.dvratil.cz/2017/01/kmail-multiple-merge-candidates-error-and-how-to-fix-it/ don't permanently fix the problem, it recurs soon after.
Comment 80 Steven Haigh 2017-12-18 14:54:08 UTC
Just to give this a nudge, I still see this with 17.12 on Fedora 27.
Comment 81 Dennis Schridde 2018-01-23 20:42:35 UTC
(In reply to andreas.sturmlechner from comment #53)
I am trying to execute the following query:
DELETE FROM pimitemtable
WHERE pimitemtable.id in (
  SELECT id FROM (
    SELECT id
    FROM pimitemtable
    LEFT OUTER JOIN (
      SELECT MIN(pimitemtable.id) as RowId, pimitemtable.remoteId, pimitemtable.collectionId
      FROM pimitemtable
      GROUP by pimitemtable.remoteId, pimitemtable.collectionId)
    AS KeepRows ON pimitemtable.id = KeepRows.RowId
    WHERE KeepRows.RowId IS NULL)
  AS _)

But I get this error:
Lock wait timeout exceeded; try restarting transaction QMYSQL: Unable to execute query

I am using KDE Applications 17.12.1, KDE Frameworks 5.42.0, Qt 5.9.3, MariaDB 10.2.12.
Comment 82 Francis Herne 2018-02-18 00:19:30 UTC
This is still broken in 5.7.2.

I have had to move away from KMail for normal use due to this bug; silently failing to recieve emails (until I manually poke the database, no less) every few hours isn't a behaviour I can cope with.
Comment 83 Steven Haigh 2018-02-18 01:21:01 UTC
Heh - funny you should mention this - I've recently had to do the same for my work gmail based mail. Thunderbird is soooo slow by comparison - kmail is clearly better - but having mail break a dozen times a day isn't usable.

I still use kmail for my home dovecot IMAP mail - and its perfect - but akonadi really doesn't like gmail at all.
Comment 84 Francis Herne 2018-02-18 10:13:52 UTC
That's odd, the problematic account in my case is on a Dovecot server (2.2.7 now).
Comment 85 Steven Haigh 2018-02-18 14:45:33 UTC
That is interesting.

I am currently using Fedora 27 - which includes dovecot 2.2.33 without issues. In fact, I'm not sure I have ever seen this issue with dovecot as the IMAP server.

My problems are purely with the gmail integration. I was assuming that this problem only affects gmail.
Comment 86 avlas 2018-02-18 14:54:06 UTC
mmm, gmail works fine in my system (up-t-date KDE neon User Edition), so I guess it's not an entirely general issue with gmail.

do you have problems in receiving, in sending or both?

for the case it helps:

my settings for receiving emails are set automatically (I guess this is sth that changed recently, it wasn't like that before).

my sending settings are: TLS 587 LOGIN
Comment 87 Steven Haigh 2018-02-18 15:07:07 UTC
I never had problems sending - only receiving...

I've often wondered if the 'IMAP' connection to gmail goes away unexpectedly that it causes this problem - but have had no real way to prove this.

Just leaving kmail open for a couple of hours with the auto-check for every 10-15 minutes is enough to cause the problem. It seems to get worse when using mail filters - as I have quite a few.

In fact, thinking out loud, I wonder if the mail filters have any contribution to this?
Comment 88 Dennis Schridde 2018-02-18 16:12:56 UTC
(In reply to Steven Haigh from comment #87)
> In fact, thinking out loud, I wonder if the mail filters have any
> contribution to this?

I hit the same with no local filters -- all my filter is a server-side Sieve script.

My mailserver is running Dovecot 2.2.33.2, but given the various reports about other providers, I doubt that's related.
Comment 89 Steven Haigh 2018-03-02 01:03:24 UTC
Well, me being a sucker for punishment - with a new laptop - I set up kmail on a fresh install of Fedora 27.

Sync'ed my gmail account to it.

The very next day, hit by this problem.

From the console:
org.kde.pim.akonadicore: Creating/updating items from the akonadi database failed: "Unknown error."
org.kde.pim.akonadicore: Creating/updating items from the akonadi database failed: "Unknown error."
org.kde.pim.akonadicore: Creating/updating items from the akonadi database failed: "Unknown error."
org.kde.pim.akonadicore: Creating/updating items from the akonadi database failed: "Unknown error."
org.kde.pim.akonadicore: Creating/updating items from the akonadi database failed: "Multiple merge candidates, aborting"

So yeah - this is still an issue.
Comment 90 Steven Haigh 2018-03-18 03:23:08 UTC
So as we're now looking at nearly 4 years on with this issue, are we any closer to looking for a final resolution?

I see new commits on the kmail git repo, but it looks like there's only one person looking after it?
Comment 91 Francis Herne 2018-03-18 10:18:24 UTC
FWIW, I removed all local filters from the affected account (in favour of Sieve) and so far the issue hasn't recurred using Dovecot. Given that it previously happened multiple times per day, filters are clearly one possible cause of this.


(Some people above reported problems even without filters, so perhaps there's more than one trigger with similar symptoms.  Debugging could be "fun"...)
Comment 92 Dennis Schridde 2018-03-18 12:16:11 UTC
(In reply to Francis Herne from comment #91)
> FWIW, I removed all local filters from the affected account (in favour of
> Sieve) and so far the issue hasn't recurred using Dovecot. Given that it
> previously happened multiple times per day, filters are clearly one possible
> cause of this.
> 
> 
> (Some people above reported problems even without filters, so perhaps
> there's more than one trigger with similar symptoms.  Debugging could be
> "fun"...)

I also use Dovecot + Sieve and am running into the issue occasionally.
Comment 93 Steven Haigh 2018-03-18 12:30:44 UTC
I use kmail with Dovecot + Sieve at home - and I never seem to have an issue.

I use kmail with Gmail at work with local filters, and it dies multiple times a day. Every time it requires the documented (in this report anyway) SQL query to fix and a restart of the akonadi services via akonadictl restart.

Same distro, same kmail / framework versions.

I'm in the middle of setting up a local set of folders and syncing the gmail account with offlineimap to see if that suffers the same problem - but I think this is going to be the only workaround that is somewhat reliable...
Comment 94 Steven Haigh 2018-03-19 06:56:46 UTC
Interestingly, I disabled the local filters on my gmail account today from within Kmail and haven't had to restart akonadi all day.

Maybe there is something within the local filters that causes things to die?
Comment 95 Joshua Clayton 2018-03-20 13:22:37 UTC
Created attachment 111525 [details]
attachment-21853-0.html

The bug is this:
emails contain an ID field in the header, usually a combination of a
timestamp + source ip or email address
kmail assumes that this ID is unique, but does a query that can return
multiple results.
It pukes every time duplicate emails are returned and it never recovers
from this status.

I proposed a fairly simple patch to fix this, just select the first result
from the query, and be done with it.
Sadly, the maintainer was unwilling to accept it, and the bug remains open

Usually these duplicates are multiple references to the same email, via
filters, or similar things.
(I have accidentally generated different emails with the same id, while
messing with git-send-email, but that is
an extremely rare edge case and I was doing wrong things out of ignorance
at the time)


I can no longer find which bug report my was attached to. It has been too
long, Sorry.

On Sun, Mar 18, 2018 at 11:56 PM, Steven Haigh <bugzilla_noreply@kde.org>
wrote:

> https://bugs.kde.org/show_bug.cgi?id=338658
>
> --- Comment #94 from Steven Haigh <netwiz@crc.id.au> ---
> Interestingly, I disabled the local filters on my gmail account today from
> within Kmail and haven't had to restart akonadi all day.
>
> Maybe there is something within the local filters that causes things to
> die?
>
> --
> You are receiving this mail because:
> You are on the CC list for the bug.
>
Comment 96 Steven Haigh 2018-03-20 14:12:46 UTC
Thanks for the additional info - would it be too much to ask to find the previous patch?

I guess the idea was along the lines of a LIMIT 1 in scenarios where more than one result would kill the world.

Ideally, this could be a runtime fix - and the fsck ability of akondactl could remove duplicate entries.

I still use this code in the SQL tab of akonadiconsole:
DELETE FROM pimitemtable WHERE pimitemtable.id in (
    SELECT id FROM (
        SELECT id FROM pimitemtable
        LEFT OUTER JOIN (
            SELECT MIN(pimitemtable.id) as RowId, pimitemtable.remoteId, pimitemtable.collectionId FROM pimitemtable GROUP by pimitemtable.remoteId, pimitemtable.collectionId
        )
        as KeepRows ON pimitemtable.id = KeepRows.RowId
        WHERE KeepRows.RowId IS NULL
    ) AS foo
)

Source:
https://wiki.meurisse.org/wiki/Workarounds#Kmail:_Multiple_merge_candidates.2C_aborting

This seems to resurrect things until it occurs again.

Still annoying and really needs to be fixed.
Comment 97 Joshua Clayton 2018-03-20 14:58:31 UTC
I looked really hard for it and didn't find it yesterday... but somehow when I looked again this morning it is the second result.
My patch was attached to 

https://bugs.kde.org/show_bug.cgi?id=376808
Comment 98 Steven Haigh 2018-03-20 15:02:58 UTC
Thanks for the link.

@Daniel Vrátil - Can you please advise on the thoughts to resolve this?

Seems to be a long standing / high impact issue with no progress.
Comment 99 Rolf Eike Beer 2018-06-09 09:06:33 UTC
*** Bug 376808 has been marked as a duplicate of this bug. ***
Comment 100 Rolf Eike Beer 2018-06-09 09:06:46 UTC
*** Bug 382253 has been marked as a duplicate of this bug. ***
Comment 101 Rolf Eike Beer 2018-06-09 09:06:59 UTC
*** Bug 393795 has been marked as a duplicate of this bug. ***
Comment 102 Allan Sandfeld 2018-06-17 08:25:01 UTC
I also see this on a regular basis, though it doesn't always completely stall imap account, or at least didn't with my last version, but now does so again.
Comment 103 davidblunkett 2018-06-25 08:45:29 UTC
Is there a way to change the number of akonadi_imap restarts allowed? 

org.kde.pim.akonadicontrol: Application '/usr/bin/akonadi_imap_resource' crashed! 1 restarts left.

I'd just like to run a cronjob to kill akonadi_imap silently every hour so I wasn't bugged with this problem.
Comment 104 Michel 2018-06-25 08:51:49 UTC
Hello, 

Is it the same PB I opened  : Bug 395417 - I've to restart akonadi several times during day for get Google IMAP access refreshing
Comment 105 Steven Haigh 2018-06-25 10:21:24 UTC
Probably the same issue. Its been around for ever and I'm not sure anyone cares...
Comment 106 davidblunkett 2018-07-06 08:52:44 UTC
FWIW I've tried the suggestions that this is related to kmail filters by deleting all my filters and I can report that this has _not_ fixed the problem because an hour later kmail has stopped (yet again) collecting mail.

This is a ridiculous state of affairs - a major flavour of linux's flagship email system cannot receive email, numerous bug reports and apparently SFA going on.

What I do know is older versions of kmail do not have this problem even now. So I suspect that the recent duplicates added here are not duplicates of the original problem.
Comment 107 Christophe Marin 2018-07-06 09:32:14 UTC
*** Bug 382253 has been marked as a duplicate of this bug. ***
Comment 108 Rafael Kitover 2018-08-24 01:06:31 UTC
*** Bug 397756 has been marked as a duplicate of this bug. ***
Comment 109 Rafael Kitover 2018-08-24 01:08:40 UTC
*** Bug 397755 has been marked as a duplicate of this bug. ***
Comment 110 davidblunkett 2018-08-27 17:08:14 UTC
I doubt my problem was ever this bug but, FWIW it appears to be working again in 5.7.3
Comment 111 Aaron Williams 2018-09-13 22:57:45 UTC
I too am still experiencing this problem with 17.12.3 in OpenSuSE 15.0, both with my home IMAP server (Cyrus) and work (Office365). I configured it to use Postgresql.
Comment 112 Halla Rempt 2018-09-28 07:54:58 UTC
And this happened again today, and yesterday, all in all about five or six times this week. Except that running Stephan's sql statement still doesn't make kmail download mail on this laptop. Maybe we should just spam this bug report every singe time this happens? That might might show how _bad_ the situation.
Comment 113 Allan Sandfeld 2018-09-28 17:05:06 UTC
Created attachment 115298 [details]
experimental patch

I have been using this patch for months to keep akonadi working on my machine.
Comment 114 Halla Rempt 2018-09-28 19:01:10 UTC
I'm not building akonadi myself, so I cannot test the patch, but if you've been using it for months... It probably should be good, shouldn't it?
Comment 115 Allan Sandfeld 2018-09-28 21:28:50 UTC
I think it is a safe and it works, but it doesn't solve the underlying issue, it just makes kmail keep working when the data is wrong.
Comment 116 Allan Sandfeld 2018-09-28 21:28:58 UTC
I think it is a safe and it works, but it doesn't solve the underlying issue, it just makes kmail keep working when the data is wrong.
Comment 117 Halla Rempt 2018-10-01 10:45:06 UTC
Well, that's important too. I mean... Releasing a mail client that without any message to the user just stops working is a bit problematic. Better keep it working, while the underlying bug gets fixed.
Comment 118 m.eik michalke 2018-10-29 12:03:31 UTC
i too am affected by this. i have to restart akonadi at least once a day. right now one of the configured IMAP resources hangs with a subfolder being fetched at 116% (guess it would also confuse me if i tried to fetch more than there is).
Comment 119 Allan Sandfeld 2018-10-30 19:21:08 UTC
I pushed my patch for review. One side effect I have noticed though, is that affected folders ends up being redownloaded frequently as the number of messages doesn't match upstream, but the duplicates are not deleted during the sync, so it happens again and again. That should probably be fixed, at least a full sync from remote should get rid of the duplicate.
Comment 120 Søren Holm 2019-02-02 12:33:53 UTC
This still happens in KDE 5.54 every one og two months. The only real solution is to recreate the mail account. I use dovecot as imap server.

While this is still a problem, could'nt you a a functionality that just recreates the account. Just so we are not forced to enter the credentials and setup folders and such?
Comment 121 Matthias Kretz 2019-02-05 10:06:23 UTC
I'm fed up with wiping my Akonadi data every other month so I'm trying to once again to understand the cause or find a workaround.

Anyway, I was following the SQL hints on debugging the state of my Akonadi data and found three instances of duplicated (well, rather "multiplicated") pimitemtable.remoteIds, on the order of hundreds of different pimitemtable.ids. I then tried to find the corresponding data in parttable via INNER JOIN parttable ON pimitemtable.id = parttable.pimItemId. Interestingly enough there's a single pimitemtable.id per pimitemtable.remoteId left (with three different parttable.id per pimitemtable.id, which seems to be correct for normal entries). So it seems there are stale entries in pimitemtable. The query

SELECT pimitemtable.*, collectiontable.name FROM pimitemtable
INNER JOIN collectiontable ON pimitemtable.collectionId = collectiontable.id
LEFT JOIN parttable ON pimitemtable.id = parttable.pimItemId
WHERE parttable.id IS NULL

lists the same remoteIds that the query looking for dups found, minus the ones that have a match in parttable. Useful information: there are no additional stale entries in pimitemtable than the dups.

Next step, I tried to remove the stale entries in pimitemtable with the following query:

DELETE pimitemtable FROM pimitemtable
LEFT JOIN parttable ON pimitemtable.id = parttable.pimItemId
WHERE parttable.id IS NULL

However, the query hangs akonadiconsole for ~1 minute and then returns "Lock wait timeout exceeded; try restarting transaction QMYSQL: Unable to execute query". An `akonadictl restart` in this case did the trick and the query then got through. All dups gone. In any case the hangs on reading mails in KMail that I experienced since yesterday are gone. Let's see whether it helps.

Maybe the DELETE query is a useful addition to `akonadictl fsck` until the root cause can be fixed?
Comment 122 Allan Sandfeld 2019-02-05 10:14:34 UTC
With my patch in place the duplicates can be removed by simply calling "remove duplicates" from KMail.
Comment 123 Matthias Kretz 2019-02-05 10:18:02 UTC
(In reply to Allan Sandfeld from comment #122)
> With my patch in place the duplicates can be removed by simply calling
> "remove duplicates" from KMail.

"Folder -> Remove Duplicates"? If that removes stales in the pimitemtable it's somewhat misleadingly named. I was expecting it to search for copies of the same mail (i.e. different local & remote ID but equal payload data).
Comment 124 Allan Sandfeld 2019-02-05 10:20:31 UTC
(In reply to Matthias Kretz from comment #123)
> (In reply to Allan Sandfeld from comment #122)
> > With my patch in place the duplicates can be removed by simply calling
> > "remove duplicates" from KMail.
> 
> "Folder -> Remove Duplicates"? If that removes stales in the pimitemtable
> it's somewhat misleadingly named. I was expecting it to search for copies of
> the same mail (i.e. different local & remote ID but equal payload data).

It is not what it is supposed to do, but those exact copies of a single remote entry also match as if they are just normal copies of the same email.
Comment 125 Allan Sandfeld 2019-02-05 10:22:20 UTC
Btw. I believe the underlying cause is the same that causes emails you are currently reading to be marked unread again while you are reading it. A sync is often started in parallel with other actions, so for instance you click on a folder it starts syncing, and you are reading an email and it is marked read. You mark it read, but the sync then marks it unread (race condition). 

The same can happen when moving emails, it might be moved, only to be recreated by the parallel sync, and then moved again, causing two exact copies to exist in the target folder.

The transactional atomicy of Akonadi is somewhat questionable with several jobs that consists of multiple database transactions, or is combined with syncing remote servers.

I tried looking at the move job, but it is pretty messy.
Comment 126 Halla Rempt 2019-02-05 10:54:13 UTC
That sounds very likely -- I see the read-unread flip all the time as well.
Comment 127 Matthias Kretz 2019-02-05 13:02:49 UTC
The fact that I can have an inconsistent state stored in my Akonadi DB is telling. Apparently, while clients interact with Akonadi, the DB goes through inconsistent states. Those appear to be open to races, and unfortunate interrupts (crash, system hang, power loss).

What I'd do (and I'm guessing from a far away distance in the hopes it helps inspire a real fix, hoping not to step on any toes):

1. Update the DB schema to enforce consistent state. In particular all foreign keys need to be defined so that it's impossible to DELETE FROM parttable while something in pimitemtable still holds a key to it.

2. Presumably this will break clients which rely on the DB to allow inconsistent intermediate states. DB updates therefore need to be fixed to atomically go from one consistent state to the next. E.g. use DELETE together with INNER JOIN.

3. Races like "unread -> read -> unread" need to be guarded against via compare-exchange atomic operations using some modification counter. E.g.
mailcheck create id=X, state=unread
kmail read id=X, state=unread
kmail write state=read WHERE id=X
mailcheck update id=X, state=unread (why would this happen anyway, can the IMAP server indicate some update of metadata?)

If we add a version the UPDATE could be predicated on the version column still storing the same value as when it was SELECTED before. E.g. the mail client does
`UPDATE foo SET state=read WHERE id=X AND version=1` instead of `UPDATE foo SET state=read WHERE id=X`. I actually expect Akonadi already has such a facility and Allans guess is incorrect.

I have another guess from looking at my logs. I have lots of:

org.kde.pim.akonadiserver:   DB error:  "Lock wait timeout exceeded; try restarting transaction"
org.kde.pim.akonadiserver:   Error text: "Lock wait timeout exceeded; try restarting transaction QMYSQL3: Unable to execute statement"

I.e. KMail updates the UI to read state, tries to reflect the state change with Akonadi, but the DB is locked up. The UPDATE fails, and the KMail UI reverts to the state stored in Akonadi.

These locks might also be the reason why inconsistent entries in tables remain. A first DELETE goes through, the second (to make it consistent again) times out.

I have no clue where the locks come from though. One data point: After my DELETE FROM statement above I've not seen a single DB error message about lock timeouts again. Before I had lots.
Comment 128 davidblunkett 2019-03-11 20:04:27 UTC
Its back - this went away for a few months but its back and its so annoying - akonadi restarts 5 times today - akonadi just hangs.

anyone got a simple way to restart the imap part I can us in a script? it would be so helpful.
Comment 129 Søren Holm 2019-03-11 21:13:23 UTC
I don't think that *anyone* knows what the problem is. This "mail in database"-system should never have been done in the first place. I still use kmail/kontact but tt has been functioning suboptimal all the years.
Comment 130 Rafael Kitover 2019-03-11 22:26:47 UTC
I switched to Geary, it's beautiful and modern and works perfectly with gmail. It's GTK but it works perfectly in KDE :D
Comment 131 davidblunkett 2019-04-07 14:25:27 UTC
Could someone just add a watchdog timer to it so it just restarts if it gets stuck? I've tried scripting an akonadictl function to do this but akonadictl insists on spamming up the desktop with irrelevant error messages.
Comment 132 Daniel Vrátil 2019-05-31 11:43:42 UTC
Git commit 8f230d7d7f8a4e2b97273585374a68902b5ef6cf by Daniel Vrátil.
Committed on 31/05/2019 at 11:21.
Pushed by dvratil into branch 'master'.

Automatic recovery from Multiple Merge Candidates error

Summary:
Introduce a recovery codepath when Multiple Merge Candidates error
occurs during Item merging. Since clients generally do not use
merging, this really only happens during ItemSync. In such case we
quitely delete all the conflicting items from the database and reschedule
the collection sync. The next sync should then succeed and bring the
collection into a consistent state.

Note that this does not fix the Multiple Merge Candidates bug - it can
still happen (and we still don't know how), but Akonadi should now be
able to recover from it automatically without user intervention, thus
making this issue less of a PITA.

Test Plan: Successfuly auto-recovered a broken collection on my setup.

Reviewers: #kde_pim, dfaure

Reviewed By: dfaure

Subscribers: vkrause, dfaure, ngraham, asturmlechner, kde-pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D21455

M  +3    -3    autotests/server/fakedatastore.cpp
M  +1    -1    autotests/server/fakedatastore.h
M  +65   -4    src/server/handler/itemcreatehandler.cpp
M  +4    -0    src/server/handler/itemcreatehandler.h
M  +18   -16   src/server/storage/datastore.cpp
M  +3    -1    src/server/storage/datastore.h

https://commits.kde.org/akonadi/8f230d7d7f8a4e2b97273585374a68902b5ef6cf
Comment 133 Søren Holm 2019-05-31 20:17:47 UTC
Great work Daniel. I switched from kmail to thunderbird some months ago and I hate it. I look forward to this automatic repair getting into my distribution :)
Comment 134 Sandro Knauß 2019-09-01 10:33:23 UTC
Git commit 8332cf8a5aa39df6fb665cdbff1a48286d5698f5 by Sandro Knauß, on behalf of Daniel Vrátil.
Committed on 30/08/2019 at 08:48.
Pushed by knauss into branch 'Applications/18.08'.

Automatic recovery from Multiple Merge Candidates error

Summary:
Introduce a recovery codepath when Multiple Merge Candidates error
occurs during Item merging. Since clients generally do not use
merging, this really only happens during ItemSync. In such case we
quitely delete all the conflicting items from the database and reschedule
the collection sync. The next sync should then succeed and bring the
collection into a consistent state.

Note that this does not fix the Multiple Merge Candidates bug - it can
still happen (and we still don't know how), but Akonadi should now be
able to recover from it automatically without user intervention, thus
making this issue less of a PITA.

Test Plan: Successfuly auto-recovered a broken collection on my setup.

Reviewers: #kde_pim, dfaure

Reviewed By: dfaure

Subscribers: vkrause, dfaure, ngraham, asturmlechner, kde-pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D21455

(cherry picked from commit 8f230d7d7f8a4e2b97273585374a68902b5ef6cf)

M  +3    -3    autotests/server/fakedatastore.cpp
M  +1    -1    autotests/server/fakedatastore.h
M  +67   -5    src/server/handler/akappend.cpp
M  +4    -0    src/server/handler/akappend.h
M  +18   -16   src/server/storage/datastore.cpp
M  +3    -1    src/server/storage/datastore.h

https://commits.kde.org/akonadi/8332cf8a5aa39df6fb665cdbff1a48286d5698f5
Comment 135 Søren Holm 2019-12-30 00:32:09 UTC
I have had no problems for the last 3 months - so the recovery method seems to work for me!
Comment 136 Aron Heinecke 2020-01-11 18:15:37 UTC
For anyone stepping by (probably on KUbuntu 18.04)

https://docs.kde.org/trunk5/en/pim/kmail2/clean-start-after-a-failed-migration.html
- Kmail->Tools->Impoexrt/Export => Export backup
- close kmail
- akonadictl stop
- rm -rf ~/.local/share/akonadi ~/.config/akonadi
- akonadictl start
- imported data*

problems:
- * you can't import filters & account etc at once
  you have to first add all email accounts back, then import your data partially
- mailfilters file in backup is empty, dialogs question for unknown identities & folders as import is done before syncing imap accounts -> non-existing folders
- if you end up with tons of empty Identities, that take 100% CPU to remove and re-appear on kmail restart, edit ~/.config/emailidentities by hand
  my regex for this is \[Identity #\d+\]\nDefault Domain=ah\-laptop\nDisable Fcc=false\nIdentity=Unnamed\nImage Location=\nInline Signature=\nSignature Enabled=false\nuoid=\d+\n in Kate
Comment 137 davidblunkett 2020-09-08 14:40:55 UTC
It's still here - just had this on v 5.7.3
Comment 138 Christoph Feck 2020-09-08 21:46:28 UTC
David, commit from comment 132 was released with KDEPIM version 5.12.0. The fix is not in older versions, such as version 5.7.3.

Closing according to comment 135; no new reports appeared for recent versions.