purebred-mua / purebred

A terminal based mail user agent based on notmuch
GNU Affero General Public License v3.0
139 stars 19 forks source link

perform all database access from dedicated thread #476

Closed frasertweedale closed 2 years ago

frasertweedale commented 2 years ago

We have had several bugs due to concurrency issues when dealing with the notmuch database. It was long proposed to [attempt to] avoid these issues by forcing all database interaction to be performed by a single, dedicated server thread. This commit implements that proposal.

The implementation is across two modules:

The server thread responds to commands received via an STM TQueue. The Command type has three constructors:

Purebred spawns the server thread at application start, and immediately sends an Open command. If that command fails, Purebred terminates (because it indicates a misconfiguration or other critical problem).

To avoid holding a write lock on the notmuch database indefinitely, we close the database after 2 seconds of inactivity. The server will re-open the database upon receiving the next Command. We use the stm-delay library to implement the timeout behaviour.

The server is stored in the AppState and the functions from Purebred.Storage.Client take the server as an argument.

frasertweedale commented 2 years ago

@romanofski please try out this branch and let me know your feedback, particular around performance.

romanofski commented 2 years ago

Gave it a whirl and can't notice much of a difference. I'll fix up the nix errors after merging. Nice work @frasertweedale !!!

frasertweedale commented 2 years ago

@romanofski thanks for testing. I'll wait til #477 is merged, then rebase and "mark ready" this PR.

frasertweedale commented 2 years ago

@romanofski good for review and merge.