Bug 157841 - LibreOfficeKit is not thread safe
Summary: LibreOfficeKit is not thread safe
Status: RESOLVED WONTFIX
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2023-10-20 08:45 UTC by stevefan1999
Modified: 2024-01-17 17:51 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description stevefan1999 2023-10-20 08:45:00 UTC
Description:
We want to use LibreOfficeKit in multiple threads, but it seems like it does not work at the moment

Steps to Reproduce:
1. Install LibreOffice
2. Follow the conceptual code in this comment: https://github.com/undeflife/libreoffice-rs/issues/17#issuecomment-1772300024 

Actual Results:
Segfaults on libmergedlo.so

Expected Results:
It should work out of the box.


Reproducible: Always


User Profile Reset: No

Additional Info:
I think we need to make all the static variables thread local, especially gImpl here: https://github1s.com/LibreOffice/core/blob/HEAD/desktop/source/lib/init.cxx#L245
Comment 1 Ashod Nakashian 2023-10-20 13:52:03 UTC
Hi!

What is the expectation of calling LOKit from multiple threads? That it will execute those calls faster? That editing will be done in parallel?

Unfortunately, that can't work. The API calls and commands sent to LOKit must still execute in a single thread inside Core. There is no way to parallelize that, since the document model is not concurrent.

This means that the only sensible thing to do here is to add locks on all the API, so that calls from different threads block each other and get exclusive access. This would work in your case, but then it would penalize all other cases that design the LOKit usage based on the Core threading model, which is single-threaded. In other words, it would add complexity and slow down the normal case.

That is, normally, you want to handle multi-threading (if necessary) elsewhere, and serialize the access to LOKit. Since document modifications are done sequentially anyway, it's a good idea to issue those commands/calls in order as well. Notice that Undo will also be sensible, as you'd know what change the Undo will do (it will be in the reverse order of the change commands/calls).

If, for whatever reason, you cannot change your design of having multi-threaded input, then the sensible thing to do is to make the LOKit reference exclusive. That is, the threads need to call an API to get the LOKit reference, which of course uses a lock to pass the reference from one thread to the other. If the lock is taken, it means the reference is not available.

TL;DR; adding locks in every API is very costly, when there are simpler solutions.

Thank you.
Comment 2 Buovjaga 2024-01-17 17:51:01 UTC
Thanks, Ashod. Let's close.