Bug 475674 - Create a Lock File when running KMyMoney
Summary: Create a Lock File when running KMyMoney
Status: RESOLVED FIXED
Alias: None
Product: kmymoney
Classification: Applications
Component: general (show other bugs)
Version: git (master)
Platform: Other Linux
: NOR wishlist
Target Milestone: ---
Assignee: KMyMoney Devel Mailing List
URL:
Keywords:
: 499729 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-10-16 00:05 UTC by Brendan
Modified: 2025-04-05 11:42 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Brendan 2023-10-16 00:05:06 UTC
I use KMM on multiple systems. My data file is shared between my various Linux systems and several backup servers via Synthing.

When I forget to close KMM before opening it on a different system, I can end up with 2 versions of the KMM data file.

I have created a workaround with a script that starts KMM (kmymoney.sh). It checks to see if there is a lock file present at the designated path. If the lock file is present, I use Zenity to display a message that file is already open on a different system and I display the name of the system where KMM is already running.

If the lock file is not present, I create the lock file which has the same name as the KMM data file that I am trying to open plus .lock. The lock file has the name of the host that is running KMM. Synthing syncs this file to my backup systems and any system that is online immediately. If I put a system to sleep without closing KMM, the lock file is still present. If I then wake up or boot up a different system, it will receive the lock file and prevent me from running KMM with the same data file.

It would be ideal if KMM handled all of this internally. By default i think the lock file should be stored in the same path as the data file but it might make sense to make it configurable. The home directory would not work for my setup since I do not sync my entire home directory with Syncthing.
Comment 1 Thomas Baumgart 2025-03-25 12:21:38 UTC
*** Bug 499729 has been marked as a duplicate of this bug. ***
Comment 2 Bug Janitor Service 2025-03-25 12:30:53 UTC
A possibly relevant merge request was started @ https://invent.kde.org/office/kmymoney/-/merge_requests/269
Comment 3 Thomas Baumgart 2025-04-01 12:20:10 UTC
Git commit 8f5cf1c82a90869e2cc62bfc624a9de2336c0193 by Thomas Baumgart.
Committed on 01/04/2025 at 08:39.
Pushed by tbaumgart into branch 'master'.

Protect opening file multiple times by a lockfile

This locks the file on any mounted drive but not when you access it on
some sort of cloud storage using a http(s) based URL.
Related: bug 497229
FIXED-IN: 5.2

M  +5    -0    kmymoney/interfaces/kmymoneyplugin.cpp
M  +20   -0    kmymoney/interfaces/kmymoneyplugin.h
M  +18   -24   kmymoney/kmymoney.cpp
M  +0    -2    kmymoney/kmymoney.h
M  +4    -0    kmymoney/plugins/gnc/import/gncimporter.cpp
M  +1    -0    kmymoney/plugins/gnc/import/gncimporter.h
M  +4    -0    kmymoney/plugins/sql/sqlstorage.cpp
M  +1    -0    kmymoney/plugins/sql/sqlstorage.h
M  +286  -184  kmymoney/plugins/xml/xmlstorage.cpp
M  +7    -25   kmymoney/plugins/xml/xmlstorage.h

https://invent.kde.org/office/kmymoney/-/commit/8f5cf1c82a90869e2cc62bfc624a9de2336c0193
Comment 4 Brendan 2025-04-01 15:29:12 UTC
I just tested this change and it does not appear to be working correctly. When I run KMM, it does create a file named FileName.kmy.lck which then appears on my other system after Syncthing syncs the folder.

When I try to open FileName.kmy on the second system, it opens fine, meaning I now have the same KMM file open on 2 systems. It overwrites the original lock file with the information from the second system.

Contents of the lck file:

First System:
699112
kmymoney
LNX-BMC
ac37039462fe4d0ab453b92a7b496f83
52b82ed4-97dd-4e78-a58a-4bf11c4bf95a

Second System:
38354
kmymoney
LNX-KIT
ac37039462fe4d0ab453b92a7b496f83
95ade840-0d72-431d-b996-0db4f80d0f57

When I close KMM on the second system, the lock file is deleted. When I close it on the second system, I get and error on the command line (because the file is no longer there):

Could not remove our own lock file "/PATH/FileName.kmy.lck" maybe permissions changed meanwhile?

Here is the commend line output from the First and Second system *referrint to the order that I open KMM):

First System:

WebConnect: Running in server mode
This is not KMyMoney plugin: '/usr/lib64/qt5/plugins/kmymoney_plugins/konlinetasks_sepa.so'
3:2025/04/01 09-21-00:(null)(699112):banking_update.c:  610: No AqBanking config folder found at [/home/bcoupe/.aqbanking/settings6/users] (-1)
3:2025/04/01 09-21-00:(null)(699112):banking_update.c:  610: No AqBanking config folder found at [/home/bcoupe/.aqbanking/settings/users] (-1)
3:2025/04/01 09-21-00:(null)(699112):banking_update.c:  411: There is no old settings folder, need initial setup
"kmymoneySHeZoO" Python module failed to load
Traceback (most recent call last):
  File "/tmp/kmymoneySHeZoO.py", line 10, in <module>
    import logging.config
  File "/usr/lib64/python3.13/logging/config.py", line 31, in <module>
    import logging.handlers
  File "/usr/lib64/python3.13/logging/handlers.py", line 30, in <module>
    import pickle
  File "/usr/lib64/python3.13/pickle.py", line 33, in <module>
    from struct import pack, unpack
  File "/usr/lib64/python3.13/struct.py", line 13, in <module>
    from _struct import *
ImportError: /usr/lib64/python3.13/lib-dynload/_struct.cpython-313-x86_64-linux-gnu.so: undefined symbol: PyByteArray_Type
Could not remove our own lock file "/home/bcoupe/KMM/Coupes.kmy.lck" maybe permissions changed meanwhile?

(kmymoney:699112): GLib-GIO-CRITICAL **: 09:23:05.374: GFileInfo created without standard::icon

(kmymoney:699112): GLib-GIO-CRITICAL **: 09:23:05.374: file ../gio/gfileinfo.c: line 1765 (g_file_info_get_icon): should not be reached

(kmymoney:699112): GLib-GIO-CRITICAL **: 09:23:05.374: GFileInfo created without standard::icon

(kmymoney:699112): GLib-GIO-CRITICAL **: 09:23:05.374: file ../gio/gfileinfo.c: line 1765 (g_file_info_get_icon): should not be reached

(kmymoney:699112): GLib-GIO-CRITICAL **: 09:23:05.374: GFileInfo created without standard::icon


Second System:

WebConnect: Running in server mode
This is not KMyMoney plugin: '/usr/lib64/qt5/plugins/kmymoney_plugins/konlinetasks_sepa.so'
3:2025/04/01 09-22-18:(null)(38354):banking_update.c:  610: No AqBanking config folder found at [/home/bcoupe/.aqbanking/settings6/users] (-1)
3:2025/04/01 09-22-18:(null)(38354):banking_update.c:  610: No AqBanking config folder found at [/home/bcoupe/.aqbanking/settings/users] (-1)
3:2025/04/01 09-22-18:(null)(38354):banking_update.c:  411: There is no old settings folder, need initial setup
"kmymoneyJIOaix" Python module failed to load
Traceback (most recent call last):
  File "/tmp/kmymoneyJIOaix.py", line 10, in <module>
    import logging.config
  File "/usr/lib64/python3.13/logging/config.py", line 31, in <module>
    import logging.handlers
  File "/usr/lib64/python3.13/logging/handlers.py", line 30, in <module>
    import pickle
  File "/usr/lib64/python3.13/pickle.py", line 33, in <module>
    from struct import pack, unpack
  File "/usr/lib64/python3.13/struct.py", line 13, in <module>
    from _struct import *
ImportError: /usr/lib64/python3.13/lib-dynload/_struct.cpython-313-x86_64-linux-gnu.so: undefined symbol: PyByteArray_Type

(kmymoney:38354): GLib-GIO-CRITICAL **: 09:22:36.569: GFileInfo created without standard::icon

(kmymoney:38354): GLib-GIO-CRITICAL **: 09:22:36.569: file ../gio/gfileinfo.c: line 1765 (g_file_info_get_icon): should not be reached

(kmymoney:38354): GLib-GIO-CRITICAL **: 09:22:36.569: GFileInfo created without standard::icon

(kmymoney:38354): GLib-GIO-CRITICAL **: 09:22:36.569: file ../gio/gfileinfo.c: line 1765 (g_file_info_get_icon): should not be reached

(kmymoney:38354): GLib-GIO-CRITICAL **: 09:22:36.569: GFileInfo created without standard::icon

(kmymoney:38354): GLib-GIO-CRITICAL **: 09:22:36.569: file ../gio/gfileinfo.c: line 1765 (g_file_info_get_icon): should not be reached
Comment 5 Thomas Baumgart 2025-04-02 06:25:46 UTC
I analyzed the problem and it turns out that QSysInfo::machineUniqueId (https://doc.qt.io/qt-5/qsysinfo.html#machineUniqueId) is returning the same ID on both systems (ac37039462fe4d0ab453b92a7b496f83). Diving into Qt source code I found out that this value is extracted from /var/lib/dbus/machine-id (which in my case is a symlink to /etc/machine-id). https://wiki.debian.org/MachineId contains some more information including how to create a new value.
Comment 6 Brendan 2025-04-02 14:44:24 UTC
That was the problem. They are cloned systems.

Is it possible to display the hostname from the lck file in the error message?
Comment 7 Thomas Baumgart 2025-04-02 16:23:02 UTC
That's a Qt feature I don't have control over.
Comment 8 Jonatan Cloutier 2025-04-02 16:48:37 UTC
I haven't look in deep into the mr, but since the dbus check seems to be removed, if the lock file contains the same machine ID, shouldn't it prevent opening it, otherwise it would now allow opening or twice on the same device?
Comment 9 Brendan 2025-04-02 17:46:50 UTC
KMM already handled the case when you try to open the same file on the same computer and that still works after this change. The error message for both attempting to open the file a second time on the same system and on a remote system are the same.

"File FineName.kmy is already opened by another process."

I would argue that is should say on another system when it's based on the lock file and the machine IDs are different but that sounds like a Qt issue.

Not sure how KMM detects that the local file is already opened. It seems like different mechanisms with the same error message.
Comment 10 Thomas Baumgart 2025-04-03 06:42:00 UTC
> Not sure how KMM detects that the local file is already opened.
> It seems like different mechanisms with the same error message.

Not really. All handled by QLockFile. On the same machine it takes the process ID contained in the first line of the lockfile and checks that the process is still running.
Comment 11 Brendan 2025-04-03 10:16:32 UTC
Where was the lck file stored? I never saw that before the recent change.
Comment 12 Thomas Baumgart 2025-04-05 11:42:28 UTC
There was no lock file prior to this change. It was handled using calls over D-Bus between the KMyMoney processes. This sure does only work on systems that have a D-Bus instance and only on the local host not over a network.