Bug 370627 - akonadi_kalarm_dir_resource watch triggered by temp files
Summary: akonadi_kalarm_dir_resource watch triggered by temp files
Status: CLOSED FIXED
Alias: None
Product: kalarm
Classification: Applications
Component: Akonadi (show other bugs)
Version: 2.11.4
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: David Jarvie
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-13 08:23 UTC by Andrey Bondarenko
Modified: 2020-08-17 23:51 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In: 16.08.3


Attachments
Patch for testing (1.74 KB, patch)
2016-10-27 21:49 UTC, David Jarvie
Details
Updated patch for testing (2.23 KB, patch)
2016-10-28 17:22 UTC, David Jarvie
Details
Patch with QFile::exists call moved to isFileValid function (1.43 KB, patch)
2016-10-30 09:04 UTC, Andrey Bondarenko
Details
Revised patch (3.03 KB, patch)
2016-10-30 17:08 UTC, David Jarvie
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrey Bondarenko 2016-10-13 08:23:06 UTC
Every time when I modify alarm from Kalarm UI, my xsession-errors gets filled with messages like:

load error
log_kalarmdirresource: Error loading "~/.local/share/kalarm/active/KAlarm-1949465381.810.XL3912"
log_kalarmdirresource: Invalid mime type for collection

I've found that KAlarm or Akonadi creates temporary files in kalarm agent directory during the edit. This temporary files trigger kdirwatch which calls KAlarmDirResource::fileChanged in kalarmdirresource.cpp. Error message occurs because temp file gets removed before akonadi_kalarm_dir_resource reads it.

Reproducible: Always

Steps to Reproduce:
1. Configure KAlarm calendar backed by kalarm dir resource
2. Add/modify Alarm with KAlarm UI in configured calendar



Expected Results:  
Akonadi should not index temporary files, they should be created in some other directory or should be filtered in isFileValid function.

Kubuntu 16.04 (amd64) with kubuntu ppa enabled

kalarm                      4:15.12.3-0ubuntu1
kdepim-runtime              4:15.12.3-0ubuntu1
libkf5calendarcore5:amd64   4:15.12.3-0ubuntu1
Comment 1 David Jarvie 2016-10-21 20:56:25 UTC
I don't see the error message in ~/.xsession-errors on my system, and therefore I can't test whether a fix will work. Can you confirm which line in kalarmdirresource.cpp produces the error - is it the call to loadFile(path, file) in fileChanged() ?

It also isn't obvious where any temporary files are created with a name pattern like the one you report. The only place I can see that creates a temporary file is in fileStorage->save() in writeToFile(), which creates a file with '~' suffix.

If I supply a patch, would you be able to rebuild and test it?
Comment 2 Andrey Bondarenko 2016-10-23 07:13:59 UTC
Here is a example of more detailed log from kalarm:

Qt-W [2016-10-23 11:53:10.027 +05] (kalarm:3906:default) unknown:0:unknown - QLayout: Attempting to add QLayout "" to YearlyRule "", which already has a layout
Qt-C [2016-10-23 11:53:14.502 +05] (akonadi_kalarm_dir_resource_3:3650:default) kcalcore/src/icalformat.cpp:89:KCalCore::ICalFormat::load - load error
Qt-C [2016-10-23 11:53:14.502 +05] (akonadi_kalarm_dir_resource_3:3650:default) kcalcore/src/icalformat.cpp:89:KCalCore::ICalFormat::load - load error
Qt-W [2016-10-23 11:53:14.502 +05] (akonadi_kalarm_dir_resource_3:3650:log_kalarmdirresource) kdepim-runtime/resources/kalarm/kalarmdir/kalarmdirresource.cpp:446:KAlarmDirResource::loadFile - Error loading "~/.local/share/kalarm/active/KAlarm-1136218218.463.Hv3650"
Qt-W [2016-10-23 11:53:14.504 +05] (akonadi_kalarm_dir_resource_3:3650:log_kalarmdirresource) kdepim-runtime/resources/kalarm/kalarmdir/kalarmdirresource.cpp:462:KAlarmDirResource::loadFile - File "~/.local/share/kalarm/active/KAlarm-1136218218.463" : event id differs from file name
Qt-C [2016-10-23 11:53:14.977 +05] (akonadi_kalarm_dir_resource_3:3650:default) kcalcore/src/icalformat.cpp:89:KCalCore::ICalFormat::load - load error
Qt-C [2016-10-23 11:53:14.978 +05] (akonadi_kalarm_dir_resource_3:3650:default) kcalcore/src/icalformat.cpp:89:KCalCore::ICalFormat::load - load error
Qt-W [2016-10-23 11:53:14.978 +05] (akonadi_kalarm_dir_resource_3:3650:log_kalarmdirresource) kdepim-runtime/resources/kalarm/kalarmdir/kalarmdirresource.cpp:446:KAlarmDirResource::loadFile - Error loading "~/.local/share/kalarm/active/KAlarm-1136218218.463.Hv3650"
Qt-W [2016-10-23 11:53:14.978 +05] (akonadi_kalarm_dir_resource_3:3650:log_kalarmdirresource) kdepim-runtime/resources/kalarm/kalarmdir/kalarmdirresource.cpp:986:KAlarmDirResource::modifyItem - Invalid mime type for collection

I don't understand what code creates temporary file. It is possible that temp file created implicitly by library code. Strace shows that temp file created by akonadi_kalarm_dir_resource_n process, but I cannot find code responsible for this.

I'm able to rebuild and test patch for version 15.12.3 available in Ubuntu 16.04, but I don't know how to properly upgrade entire stack to more recent versions like 16.04 or 16.08. It is possible to provide patch for old version?
Comment 3 Andrey Bondarenko 2016-10-24 18:09:38 UTC
Probably a source of temporary files is QSaveFile instances used in function ICalFormat::save from kcalcore file src/icalformat.cpp.

According to Qt docs (http://doc.qt.io/qt-5/qsavefile.html) QSaveFile creates temporary file, writes to it, then rename temporary file on commit.

Looks like Implicit temp files are created under kalarm_dir_resource by QSaveFile in kcalcore and KDirWatch fire fileChanged on them. Because temporary filenames do not start with dot and not end with ~ they pass isFileValid.
Comment 4 David Jarvie 2016-10-27 21:49:03 UTC
Created attachment 101844 [details]
Patch for testing

I've committed a fix in Applications/16.08 branch (commit 36009232d38e517b34816a014958a58fabd886a0).

I also attach a patch (which should apply to 15.12 branch) which I'd appreciate if you could test and let me know if it successfully fixes the bug.
Comment 5 Andrey Bondarenko 2016-10-28 08:21:43 UTC
I've recompiled kdepim-runtime with the patch but still have error messages

Qt-D [2016-10-28 13:03:31.366 +05] (akonadi_kalarm_dir_resource_3:17381:log_kalarmdirresource) .../kdepim-runtime/resources/kalarm/kalarmdir/kalarmdirresource.cpp:857:KAlarmDirResource::fileChanged - "~/.local/share/kalarm/active/KAlarm-1360478297.791.U17381"
Qt-D [2016-10-28 13:03:31.366 +05] (akonadi_kalarm_dir_resource_3:17381:log_kalarmdirresource) .../kdepim-runtime/resources/kalarm/kalarmdir/kalarmdirresource.cpp:442:KAlarmDirResource::loadFile - "~/.local/share/kalarm/active/KAlarm-1360478297.791.U17381"
Qt-D [2016-10-28 13:03:31.366 +05] (akonadi_kalarm_dir_resource_3:17381:log_kcalcore) .../kcalcore/src/icalformat.cpp:83:KCalCore::ICalFormat::load - load "~/.local/share/kalarm/active/KAlarm-1360478297.791.U17381"
Qt-C [2016-10-28 13:03:31.366 +05] (akonadi_kalarm_dir_resource_3:17381:log_kcalcore) .../kcalcore/src/icalformat.cpp:89:KCalCore::ICalFormat::load - load error: cannot open "~/.local/share/kalarm/active/KAlarm-1360478297.791.U17381" error "No such file or directory"                                                                                         
Qt-D [2016-10-28 13:03:31.366 +05] (akonadi_kalarm_dir_resource_3:17381:log_kcalcore) .../kcalcore/src/icalformat.cpp:83:KCalCore::ICalFormat::load - load "~/.local/share/kalarm/active/KAlarm-1360478297.791.U17381"
Qt-C [2016-10-28 13:03:31.366 +05] (akonadi_kalarm_dir_resource_3:17381:log_kcalcore) .../kcalcore/src/icalformat.cpp:89:KCalCore::ICalFormat::load - load error: cannot open "~/.local/share/kalarm/active/KAlarm-1360478297.791.U17381" error "No such file or directory"
Comment 6 Andrey Bondarenko 2016-10-28 08:26:19 UTC
Method fileStorage->load() from kcalcore called from KAEvent KAlarmDirResource::loadFile still reports error if file does not exists. I think you should check file presence before calling load.
Comment 7 David Jarvie 2016-10-28 17:22:19 UTC
Created attachment 101857 [details]
Updated patch for testing

Can you please test the attached patch which now checks for the file's existence before calling ICalFormat::load(). Thank you.
Comment 8 Andrey Bondarenko 2016-10-30 09:04:07 UTC
Created attachment 101896 [details]
Patch with QFile::exists call moved to isFileValid function

I still see errors in my ~/.xsession-errors file after updated patch.

Looks like moving QFile(file).exists() to isFileValid function solves the problem (see attached patch).
Comment 9 David Jarvie 2016-10-30 17:08:38 UTC
Created attachment 101901 [details]
Revised patch

Thanks for that. Your patch assumes that the current working directory is the calendar directory, which I'm not sure is a safe assumption. Can you please try out a revised version of your patch, which hopefully can be committed to fix the issue.
Comment 10 Andrey Bondarenko 2016-10-31 05:01:52 UTC
I don't see load errors after applying revised patch. The issue is resolved.
Comment 11 David Jarvie 2016-10-31 20:50:02 UTC
Thank you for your assistance in testing the fix. This is now fixed for KAlarm version 2.11.10-5, which will be in KDE Applications 16.08.3.

Commit 4cd6df12ffc7cc7455f986e87c0d79d00d3123fa (16.08 branch).