Bug 503551 - kmymoney prints "File ... is already opened by another process"
Summary: kmymoney prints "File ... is already opened by another process"
Status: RESOLVED FIXED
Alias: None
Product: kmymoney
Classification: Applications
Component: general (other bugs)
Version First Reported In: 5.1.95
Platform: Other Linux
: NOR normal
Target Milestone: ---
Assignee: KMyMoney Devel Mailing List
URL:
Keywords:
Depends on:
Blocks: 488491
  Show dependency treegraph
 
Reported: 2025-04-29 21:18 UTC by Ralf Habacker
Modified: 2025-07-03 06:08 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In: 5.2.1
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ralf Habacker 2025-04-29 21:18:13 UTC
STEPS TO REPRODUCE
1. build kmymoney from master branch
2. start kmymoney and open a file
3. kill kmymoney process or let kmymoney crash or run kmymoney with gdb and exit without closing the file
4. restart kmymoney 

OBSERVED RESULT
In the application there is a message printed "File ... is already opened by another process" and the file is not opened.

EXPECTED RESULT
The file should be opened

SOFTWARE/OS VERSIONS
Operating System: openSUSE Leap 15.6
KDE Plasma Version: 5.27.11
KDE Frameworks Version: 5.115.0
Qt Version: 5.15.12

ADDITIONAL INFORMATION
This issue looks to be related to bug 475674.
Comment 1 Thomas Baumgart 2025-04-30 05:58:05 UTC
I am unable to reproduce this. Things I tried using master branch build:

1. run kmymoney with gdb and option -n, open a file and exit gdb without closing the file
2. verify .lck file is still present
3. restart kmymoney -> no message, file is opened

1. run kmymoney with gdb no option to open last used and exit gdb without closing the file
2. verify .lck file is still present
3. restart kmymoney -> no message, file is opened

1. run kmymoney opening last used file
2. copy the lock file
3. quit kmymoney and make sure original lock file is gone
4. move copy of lock file back to original name
5. restart kmymoney -> no message, file is opened

1. run kmymoney with gdb no option to open last used and interrupt to get to gdb's cli. Don't quit
2. start kmymoney in second console -> message file is opened is shown (which is correct here)
3. quit gdb (which leaves the lock file on disk)
4. use running second instance of kmymoney to open file -> no message, file is opened

1. run kmymoney opening last used file
2. copy the lock file
3. quit kmymoney and make sure original lock file is gone
4. move copy of lock file back to original name
5. modify content of .lck file (first char on line 4)
6. restart restart kmymoney -> message file is opened is shown (which is correct here)
7. revert modification of step 5
8. use running second instance of kmymoney to open file -> no message, file is opened

SOFTWARE/OS VERSIONS (identical to yours)
Operating System: openSUSE Leap 15.6
KDE Plasma Version: 5.27.11
KDE Frameworks Version: 5.115.0
Qt Version: 5.15.12

Conclusion: is this something that is only happening on your system? And why?
Comment 2 Ralf Habacker 2025-04-30 07:19:00 UTC
I did some additional testing and made the following observations:

- on one computer I have the problem when the file in question is stored on an nfs share. Files on the local file system are not affected
- on another computer, the problem does not occur if the file was loaded from an nfs share on another server
- on both computers the .lck file is not removed after force quitting kmymoney
Comment 3 Ralf Habacker 2025-04-30 07:37:04 UTC
(In reply to Ralf Habacker from comment #2)
> - on one computer I have the problem when the file in question is stored on
> an nfs share. Files on the local file system are not affected

In the console I saw kmymoney printing the following message

Open file QUrl("file:///server/.../test.xml")
setNativeLocks failed: "Permission denied"
Comment 4 Ralf Habacker 2025-04-30 07:40:54 UTC
(In reply to Ralf Habacker from comment #3)
> In the console I saw kmymoney printing the following message
> 
> Open file QUrl("file:///server/.../test.xml")
> setNativeLocks failed: "Permission denied"

The share on the server in question is mounted by systemd automount support as

$ grep server /etc/fstab
server:/daten /server/daten nfs rw,relatime,vers=3,rsize=8192,wsize=8192,namlen=255,soft,mountvers=3,x-systemd.automount,x-systemd.idle-timeout=60s 0 0
Comment 5 Thomas Baumgart 2025-04-30 07:53:52 UTC
Here's where this message comes from: https://github.com/qt/qtbase/blob/v5.15.12-lts-lgpl/src/corelib/io/qlockfile_unix.cpp#L142-L193
Comment 6 Ralf Habacker 2025-04-30 11:48:00 UTC
I did run kmymoney with strace and got: 

write(2, "Open file QUrl(\"file:///server/d"..., 83) = 83
access("/server/daten/kmymoney.xml", F_OK) = 0
statx(AT_FDCWD, "/server/daten/kmymoney.xml", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=3649420, ...}) = 0
openat(AT_FDCWD, "/server/daten/kmymoney.xml", O_RDONLY|O_CLOEXEC) = 12
statx(12, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=3649420, ...}) = 0
statx(12, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=3649420, ...}) = 0
read(12, "<?xml version=\"1.0\" encoding=\"UT"..., 16384) = 16384
close(12)                               = 0
openat(AT_FDCWD, "/server/daten/kmymoney.xml.lck", O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC, 0666) = -1 EEXIST (Die Datei existiert bereits)

-> here the file is opened with read and write access, which failed

openat(AT_FDCWD, "/server/daten/kmymoney.xml.lck", O_RDONLY|O_CLOEXEC) = 12

-> here the file is opened with read access, which was okay

statx(12, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=91, ...}) = 0
statx(12, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=91, ...}) = 0
read(12, "30208\nkmymoney\n...n6d5d7e29bea"..., 16384) = 91
read(12, "", 16293)                     = 0
close(12)                               = 0
uname({sysname="Linux", nodename="buero", ...}) = 0
openat(AT_FDCWD, "/var/lib/dbus/machine-id", O_RDONLY|O_CLOEXEC) = 12
read(12, "6d5d7e29bea34ce689e362c70e40def0", 32) = 32
close(12)                               = 0
openat(AT_FDCWD, "/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC) = 12
read(12, "18150db6-b484-488e-b1b4-9e5b844e"..., 36) = 36
close(12)                               = 0
kill(30208, 0)                          = -1 ESRCH (Kein passender Prozess gefunden)
newfstatat(AT_FDCWD, "/etc/localtime", {st_mode=S_IFREG|0644, st_size=2298, ...}, 0) = 0
newfstatat(AT_FDCWD, "/etc/localtime", {st_mode=S_IFREG|0644, st_size=2298, ...}, 0) = 0
newfstatat(AT_FDCWD, "/etc/localtime", {st_mode=S_IFREG|0644, st_size=2298, ...}, 0) = 0
statx(AT_FDCWD, "/server/daten/kmymoney.xml.lck", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=91, ...}) = 0
newfstatat(AT_FDCWD, "/etc/localtime", {st_mode=S_IFREG|0644, st_size=2298, ...}, 0) = 0
newfstatat(AT_FDCWD, "/etc/localtime", {st_mode=S_IFREG|0644, st_size=2298, ...}, 0) = 0
newfstatat(AT_FDCWD, "/etc/localtime", {st_mode=S_IFREG|0644, st_size=2298, ...}, 0) = 0

openat(AT_FDCWD, "/server/daten/kmymoney.xml.lck.rmlock", O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC, 0666) = 12

-> then an additional file is created with read/write access 

flock(12, LOCK_EX|LOCK_NB)              = -1 EACCES (Keine Berechtigung)

which could not locked and leads to the reported issue.



write(2, "setNativeLocks failed: \"Permissi"..., 43) = 43
Comment 7 Ralf Habacker 2025-04-30 11:56:51 UTC
(In reply to Ralf Habacker from comment #6)
> I did run kmymoney with strace and got: 

This was the log for the second run. 

Here is the one from the first run: 

write(2, "Open file QUrl(\"file:///server/d"..., 83) = 83
access("/server/daten/kmymoney.xml", F_OK) = 0
statx(AT_FDCWD, "/server/daten/kmymoney.xml", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=3649420, ...}) = 0
openat(AT_FDCWD, "/server/daten/kmymoney.xml", O_RDONLY|O_CLOEXEC) = 12
statx(12, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=3649420, ...}) = 0
statx(12, "", AT_STATX_SYNC_AS_STAT|AT_NO_AUTOMOUNT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=3649420, ...}) = 0
read(12, "<?xml version=\"1.0\" encoding=\"UT"..., 16384) = 16384
close(12)                               = 0
openat(AT_FDCWD, "/server/daten/kmymoney.xml.lck", O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC, 0666) = 12
flock(12, LOCK_EX|LOCK_NB)              = -1 EACCES (Keine Berechtigung)
write(2, "setNativeLocks failed: \"Permissi"..., 43) = 43

Since the same error message is displayed, the cause of the error is probably somewhere else (indicated by the mentioned file '*.rmlock')
Comment 8 Ralf Habacker 2025-04-30 11:59:10 UTC
(In reply to Ralf Habacker from comment #7)

> Since the same error message is displayed, the cause of the error is
> probably somewhere else (indicated by the mentioned file '*.rmlock')

Here is a related location https://github.com/qt/qtbase/blob/ae6ba06e55dbdae98eb05069e007044bf70f45da/src/corelib/io/qlockfile.cpp#L304
Comment 9 Ralf Habacker 2025-04-30 12:05:14 UTC
(In reply to Ralf Habacker from comment #8)

> Here is a related location

which is called from kmymoney code at https://github.com/KDE/kmymoney/blob/284c619bf10638a98fdf8a0ab24be6aacebd5efc/kmymoney/plugins/xml/xmlstorage.cpp#L291
Comment 10 Thomas Baumgart 2025-05-01 06:23:32 UTC
Looking at the Qt code here is what happens:

KMyMoney calls QLockFile::tryLock() on the .lck file which fails in QLockFilePrivate::tryLock_sys() with QLockFile::LockFailedError because the .lck file already exists. tryLock() then calls QLockFilePrivate::isApparentlyStale() which uses getLockInfo_helper() to read the information about the file (opening as read-only). isApparentlyStale() rightfully returns true, because this machine created the lock in the first run and then quit prematurely.  The next thing in tryLock() is to create another QLockFile object on filename.lck.rmlock which should prevent that two processes remove the file at the same time. For that matter it calls tryLock() on it but setting the lock on this .rmlock file is where the permission error is encountered.

So the question is why NFS allowed to set the lock on the first .lck file but not on the second .lck.rmlock.

Not much we can do about in KMyMoney :(
Comment 11 Ralf Habacker 2025-05-13 03:19:34 UTC
Another use case where an error occurs every time is that a kmymoney file cannot be opened from a write-protected location. The message that the file is already open appears immediately.
Comment 12 Thomas Baumgart 2025-05-13 12:45:47 UTC
Git commit 1f89085be1e6c89ee19eebe2e9ce514f36f48558 by Thomas Baumgart.
Committed on 13/05/2025 at 12:45.
Pushed by tbaumgart into branch 'master'.

Allow to open write protected file without locking

M  +9    -1    kmymoney/plugins/xml/xmlstorage.cpp

https://invent.kde.org/office/kmymoney/-/commit/1f89085be1e6c89ee19eebe2e9ce514f36f48558
Comment 13 Ralf Habacker 2025-05-27 21:25:06 UTC
The KCrash library contains a function KCrash::setEmergencySaveFunction() (https://api.kde.org/frameworks/kcrash/html/kcrash_8h_source.html#l00095), which can be used to install a handler for emergencies. This file can then be closed and the lock removed again.

static void emergencySaveFunction(int)
{
    if (kmymoney)
        kmymoney->slotFileClose();
}

int main()
{
...
    KCrash::setEmergencySaveFunction (emergencySaveFunction);
...
Comment 14 Bug Janitor Service 2025-06-11 03:47:36 UTC
๐Ÿ›๐Ÿงน โš ๏ธ This bug has been in NEEDSINFO status with no change for at least 15 days. Please provide the requested information, then set the bug status to REPORTED. If there is no change for at least 30 days, it will be automatically closed as RESOLVED WORKSFORME.

For more information about our bug triaging procedures, please read https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging.

Thank you for helping us make KDE software even better for everyone!
Comment 15 Bug Janitor Service 2025-06-26 03:48:07 UTC
๐Ÿ›๐Ÿงน This bug has been in NEEDSINFO status with no change for at least 30 days. Closing as RESOLVED WORKSFORME.
Comment 16 Bug Janitor Service 2025-06-27 08:16:42 UTC
A possibly relevant merge request was started @ https://invent.kde.org/office/kmymoney/-/merge_requests/279
Comment 17 Ralf Habacker 2025-06-30 19:53:53 UTC
Git commit d800d2fa5c6b5a008d51fef91324f053dc37cd7d by Ralf Habacker.
Committed on 30/06/2025 at 19:53.
Pushed by habacker into branch 'master'.

In emergency case close file if possible

This prevents obsolete locks that may prevent a file from being
reloaded after a crash or similar.

With this commit the additional library 'KCrash' is added as dependency.

M  +10   -9    .kde-ci.yml
M  +1    -1    CMakeLists.txt
M  +1    -0    kmymoney/CMakeLists.txt
M  +10   -0    kmymoney/main.cpp
M  +3    -0    tools/ci-install.sh

https://invent.kde.org/office/kmymoney/-/commit/d800d2fa5c6b5a008d51fef91324f053dc37cd7d
Comment 18 Thomas Baumgart 2025-07-02 16:00:54 UTC
Git commit f2edb599d7e8903645fd8e93c73b0206bd5257f9 by Thomas Baumgart.
Committed on 01/07/2025 at 10:36.
Pushed by tbaumgart into branch '5.2'.

In emergency case close file if possible

This prevents obsolete locks that may prevent a file from being
reloaded after a crash or similar.

With this commit the additional library 'KCrash' is added as dependency.
(cherry picked from commit d800d2fa5c6b5a008d51fef91324f053dc37cd7d)

M  +10   -9    .kde-ci.yml
M  +1    -1    CMakeLists.txt
M  +1    -0    kmymoney/CMakeLists.txt
M  +10   -0    kmymoney/main.cpp
M  +3    -0    tools/ci-install.sh

https://invent.kde.org/office/kmymoney/-/commit/f2edb599d7e8903645fd8e93c73b0206bd5257f9