Repository navigation
Conversation
Before this PR, post-messages were downloaded from all transports
concurrently, wasting network traffic.
The problem was that all transports queried the `downloads` table at
roughly the same time, then downloaded the message at the same time,
then removed the row from the `downloads` table at roughly the same time.
This PR fixes this by locking the mutex before querying the `downloads` table.
Also, I added a `_fetch_msgs_lock_guard` param to functions that need
this mutex guard to be held, because I was losing track of which
functions require it to be held.
---
A problem somewhat unrelated to this PR: If one or multiple messages take a very long time to download, then no small messages can be downloaded inbetween.
A straightforward solution that would somewhat help is fetching once
after downloading, then going idle when no more messages need to be
downloaded:
```rust
loop {
fetch_delete(...).await;
download_known_post_messages_without_pre_message(...);
if !download_msgs(...) {
break;
}
}
idle()
```
A more complete but more complex solution would be to split `fetch_msgs_mutex` into two
mutexes, and allow small messages to be fetched&processed while
post-messages are fetched, but I'm not sure if we'll be able to
implement this in a bug-free way.
Do you think I should implement either of this?
| context: &Context, | ||
| rfc724_mid: String, | ||
| session: &mut Session, | ||
| _fetch_msgs_lock_guard: &MutexGuard<'_, ()>, |
There was a problem hiding this comment.
vertical line minimization police here: if you gave a shorter name ruff format might produce less lines, which i much prefer for readability.
There was a problem hiding this comment.
Personally, I even like that these function calls get more vertical weight, because the function calls that need _fetch_msgs_lock_guard are usually the most important function calls. But it's not important, we can also rename it, maybe to _fetch_guard?
| pub(crate) async fn download_msgs(context: &Context, session: &mut Session) -> Result<()> { | ||
| // Lock the mutex before querying the `download` table to prevent | ||
| // multiple transports from downloading the same message at the same time: | ||
| let _fetch_msgs_lock_guard = context.fetch_msgs_mutex.lock().await; |
There was a problem hiding this comment.
doesn't this hold the lock for downloading potentially multiple messages? would be better if the lock is only held per-message, even if it means more sql checks.
There was a problem hiding this comment.
Edit: This message contains a lot of thoughts of mine, but eventually I did implement to hold the lock per-message, just because it was easy to implement
I thought about this, but this would only be helpful in one narrow case because all transports try to download the same list of messages (except for edge cases), so that in either case, fetching small messages will be blocked until the transports are done with downloading large messages:
When any transport is busy downloading a large message, every interrupt will lead the other two transports to be queued in dowload_msgs() too, because they try to fetch, see that there is nothing new (or fetch some small message), and then also land in download(). And there are many causes for interrupts, e.g. the user pressing "Download" on a message, or a downloaded message needing a delete job.
With the PR as-is, one transport will try to download all pending messages. Then, the other transports will try to download any messages that failed to download from the first transport.
Holding the lock per-message, one transport will try to download the first pending message. Then, the next transport gets the lock and tries to download the next pending message (or the same one, if it failed to be downloaded from the first transport), and so on.
In either case, while the transports are trying to download large messages, no new small messages are fetched. In order to fix this, we need something like I described in the PR description ("BTW, a related problem").
OTOH, there is a narrow case where locking per-message would help: While A is downloading, B is still in IDLE and nothing has interrupted it yet. Then a new message arrives on B's relay. Since the fix is simple enough, I'm wondering if we should go for it. I'll implement it to see how it looks
There was a problem hiding this comment.
I implemented it as 5c2ae2a, because it was easy to implement, and there is one narrow edgecase where it helps
|
Looks good, but i noticed an existing related problem: #8941 |
Before this PR, post-messages were downloaded from all transports concurrently, wasting network traffic. I noticed this while implementing #8930.
The problem was that all transports queried the
downloadstable at roughly the same time, then downloaded the message at the same time, then removed the row from thedownloadstable at roughly the same time.This PR fixes this by locking the mutex before querying the
downloadstable.Also, I added a
_fetch_msgs_lock_guardparam to functions that need this mutex to be locked, because I was losing track of which functions require it to be held.BTW, a related problem: If one or multiple messages take a very long time to download, then no small messages can be downloaded inbetween. This is made a little bit better by this PR because at least only one transport tries to download all pending messages rather than all transports. But it can still cause messages to arrive delayed, or only after restarting the app.
A straightforward solution that would somewhat help is fetching once after each download:
A more complete but more complex solution would be to split
fetch_msgs_mutexinto two mutexes, and allow small messages to be fetched&processed while post-messages are fetched, but I'm not sure if we'll be able to implement this in a bug-free way.Do you think we should implement either of this?