Skip to content

Use an asynchronous queue for notmuch queries - #147

Open
laarmen wants to merge 6 commits into
akissinger:masterfrom
laarmen:search-async-refresh
Open

laarmen wants to merge 6 commits into
akissinger:masterfrom
laarmen:search-async-refresh

Conversation

@laarmen

@laarmen laarmen commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

We have a number of hardcoded notmuch queries that can run into each other. Using a centralized queue makes conflicts rarer (though not yet completely eliminated since user-provided queries, e.g. full refreshes, are still off on their own), and the fact it's asynchronous means the UI will not block while, say, notmuch is waiting for you to input your very long and error-prone GPG passphrase. Purely hypothetical scenario, of course.

klement and others added 6 commits October 20, 2026 10:25
Use dataChanged for updates and beginRemoveRows for removals instead
of beginResetModel/endResetModel, so Qt only repaints the affected row
rather than rebuilding the entire view. Eliminates UI stalls on large
search results (e.g. 3500 threads).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This started out simple, but as all things, it grew quite a bit as edge
cases started to crop up.

There is a notion of "write" commands, e.g. when changing the tags of a
message, which is basically like taking a write lock on a rwlock (except
that Python doesn't have those): it won't run until all previous
commands have started, and nothing will start until it has finished. Any
command without a callback is assumed to be a write command, unless
stated otherwise, to avoid nasty surprised.

The system relies entirely on all code running on the same thread. If
you break that assumption, it's on you.

The goal is to use this system to replace all notmuch calls that are
currently on the main thread, so that we don't block the UI if those
take a while.
We changed the code to use the specialized model signals when untagging,
but we never added the additional wiring required for tab title updates.
The one small caveat is that technically, the updated-thread broadcast
on tag now fires concurrently to the notmuch tag action so any consumer
of that broadcat that doesn't use the async machinery might (and, from
experience, *will*) query the database before it actually get updated.

However, that's not too problematic since the main consumer of this
broadcast is the search tab, which does use this mechanism.
This actually needed some small layer on top of the simple async_notmuch
call so that we could get the results of two queries at the same time.

The real gain here is that we no longer lock up the UI thread if the
notmuch call takes forever, e.g. if it's trying to decrypt data and is
waiting for the user to enter their passphrase.
After each mail sync, check the notmuch database revision via
'notmuch count --lastmod' before doing any work. If the revision is
unchanged (no mail arrived), skip all refreshes entirely.

When mail does arrive, search panels refresh concurrently, which is fine
since it's all done asynchronously. This avoids the lag when switching
tabs, as the data will already be up-to-date.

Other panel types (thread, compose) stay on the existing lazy-dirty
path and refresh on next focus.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Klement Sekera <klement.sekera@gmail.com>
@laarmen

laarmen commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Note that it is based off #134 and thus supersedes it.

@laarmen

laarmen commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Known defect: the time column no longer updates if there is no new content, since it is currently sourced directly from notmuch. My current local workaround is to use the arrow library for a similar humanized string, but Python dependency and project management utterly confuses me...

@The-Compiler

Copy link
Copy Markdown
Collaborator

I've been trying this out since yesterday, and from an user's perspective, the complexity is absolutely worth it. The difference in how snappy the UI feels is day and night.

I did a quick skim through the diff, I suppose some things could be simplified (and admittedly I haven't entirely understood the whole error handling/coalescing logic yet). But I'll need get back to this at a point where I can properly focus on Dodo stuff for a few hours again.

Agreed calculating the times client-side is the right approach, and arrow is a good choice. From how this repo is structured, adding it to install_requires in setup.py (and the docs in the readme) should be all that's needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants