Enabling languages causes 75+ second document loads (pt2)

This is a continuation of the conversation from Enabling languages causes 75+ second document loads - #3 by hackin_adam

This problem has returned even with the jails disabled. The /usr/bin/coolforkit-ns process takes up 100% CPU power for about a minute and a half and after that everything works fine. As you might imagine, a 80-second load time is not acceptable.

I’m currently running version 26.04.5-2, which is the latest available from the apt repo as of now.

I’ve tried re-enabling jails in /etc/coolwsd/coolwsd.xml and the bug persists there too. To be clear, to disable them, I set <mount_jail_tree>false</mount_jail_tree> directly under <config>.

There’s currently no workaround other than setting <allowed_languages></allowed_languages> to an empty set. This disables spell checking, which is undesirable, but not as bad as 80 second load times.

Both @darshan and @caolan were helpful in getting us part way to the solution (something about the dictionary files and seemingly an O(n^2) problem), but nobody has been able to determine the root cause and fix.

This issue is reproducible on every Collabora server on Debian that is using a similar configuration as mine. You can see the template for the configuration file I’m using here. Most of it is hard-coded and matches the default configuration file.

I’m happy to provide any additional information such as the size or line count of whatever files people think might be causing the problem. I’m also willing to move files out of the way temporarily to see if it resolve the slow loading issue and allows spellcheck to work.

I’d really like to help contribute a solution back to the project. :heart: I just need a little help getting there.

Hi Adam,

Thanks for picking this up again, and for offering to help fix it. I went back through the first thread with the source open, and I think we can now explain the whole chain. Even better, it ends in two small patches that would suit a first contribution.

Where the big file comes from

Your strace in July showed the 551,806-byte en_US.dic under tmp/sharedpresets/...richdocuments/wopi/settings.../wordbook/. That folder holds the shared settings that Nextcloud serves to Collabora Online. They are managed in Nextcloud’s Office admin settings, where an admin can upload custom dictionaries.

The upload code (browser/admin/src/integrator/WordBook.ts, wordbookValidation()) takes whatever text file it is given and adds a wordbook header (OOoUserDict1, lang: <none>, type: positive, ---) in front of it. It does not check that the file is really a word list. So if the hunspell en_US.dic from dict-en is uploaded there, it becomes a 49,568-entry wordbook, with the word count 49568 and the affix flags (0th/pt) kept as “words”. That matches the head output you posted exactly.

Why it costs 80 seconds

Every document load reads that wordbook. In engine/linguistic/source/dicimp.cxx, addEntry_Impl() runs this after inserting each word:

SAL_WARN_IF(!isSorted(), "linguistic", "dictionary entries unsorted");

isSorted() walks the whole list. Our Linux packages are built with --enable-sal-log, so the check runs even though the warning itself is filtered out by lokit_sal_log. With 49,568 words that is over a billion string comparisons: the O(n^2) you mentioned, and your pegged core. A real wordbook of a few dozen words never notices it.

Caolán’s typo fix from July (https://gerrit.collaboraoffice.com/c/online/+/8001) is in 26.04.3 and later, so your 26.04.5 already has it. It was a real bug, but not this one.

Fixing your server now

  1. In Nextcloud, open Administration settings, then Office, and look at the custom dictionaries in the Collabora settings section. Remove en_US.dic, and any other hunspell dictionary that was uploaded there.
  2. If your Ansible role uploads it, drop that step. The hunspell dictionaries from collaboraoffice-dict-* plus allowed_languages are all spell checking needs.
  3. Set allowed_languages back to what you want, re-enable the jail (please do, the chroot is a security boundary), restart coolwsd and open a document.

To confirm the file is gone from a running jail:

ls -la /opt/cool/child-roots/*/tmp/sharedpresets/*/wordbook/

Each file there should be a few hundred bytes, not hundreds of kilobytes.

I can’t yet explain why turning the jail off appeared to help, or why an empty allowed_languages hides the problem. If both go back to normal once the file is removed, I would not spend more time on them. If not, please post what you see.

The two patches

This is where we need your help, if you’d like to. Both changes are small, and you already understand the bug better than almost anyone:

  1. Engine, C++: in engine/linguistic/source/dicimp.cxx, remove the isSorted() check that runs after every insert in addEntry_Impl(). loadEntries() already checks once after the whole file is read, so the warning still fires for a broken dictionary. This turns your 80 seconds into milliseconds even for a huge file.
  2. Admin page, TypeScript: in browser/admin/src/integrator/WordBook.ts, make wordbookValidation() reject a hunspell .dic (a first line that is only a number, or many words containing /flags) with a clear message that says hunspell dictionaries are installed as packages. That stops the next admin from landing where you did.

How to send them:

Please post here how step 1 goes. If the load time drops, that confirms the whole chain, and you can name it as the reproducer in the commit message. You can also find us on Matrix in #cool-dev:matrix.org if you’d rather work through the patches live.

Thanks
Darshan

Deleting the file from Nextcloud and putting everything back the way it was did fix my server. :partying_face:

I’m struggling with the source code changes though. I downloaded the repo, but I don’t see isSorted being called anywhere other than from loadEntries (hxxps://github.com/CollaboraOnline/online.mirror/blob/main/engine/linguistic/source/dicimp.cxx#L378). I don’t see any sorting in addEntry_Impl (hxxps://github.com/CollaboraOnline/online.mirror/blob/main/engine/linguistic/source/dicimp.cxx#L673-L732). Digging a little deeper, it looks like someone beat us to it a few days ago. The commit comment also matches your description.

When I looked at the .ts file, I quickly saw the error message that says the file is a spelling dictionary, not a wordbook after this check ( hxxps://github.com/CollaboraOnline/online.mirror/blob/main/browser/admin/src/integrator/WordBook.ts#L459 ). And that looks for a line that is all digits (hxxps://github.com/CollaboraOnline/online.mirror/blob/main/browser/admin/src/integrator/WordBook.ts#L514). Sure enough, that was patched just yesterday, by the same person. Again, the comment indicates that I wasn’t the first one to get bit by this.

So it looks like some future release should stop admins from uploading the wrong kind of dictionary, and the code shouldn’t blow up if they find some way to do it anyway.

On top of that, I’m good to go until this patch lands because that file is gone. I must have uploaded it when I was trying to troubleshoot why the spell check wasn’t activating, and that was probably because I didn’t have any acceptable languages in the config.

It’s really great to see a project with developers who are curious enough to dedicate the time to solve these kinds of puzzles. Thank you for also taking the time to also write it all up in a way that I can understand it, even though I’ve never looked at this code base before. :heart:

And apparently I had it easy with my 80 second load times. Apparently someone was seeing a 20-minute delay for a document to load!

Thnaks @hackin_adam for the nice words it keeps me motiavted :slight_smile: