Skip to content

fix: Download post-messages only from one transport - #8939

Open
Hocuri wants to merge 2 commits into
mainfrom
hoc/no-double-post-msg-download
Open

Hocuri wants to merge 2 commits into
mainfrom
hoc/no-double-post-msg-download

Conversation

@Hocuri

@Hocuri Hocuri commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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 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 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:

loop {
    loop {
        fetch_delete(...);
        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 we should implement either of this?

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?
Comment thread src/download.rs
context: &Context,
rfc724_mid: String,
session: &mut Session,
_fetch_msgs_lock_guard: &MutexGuard<'_, ()>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vertical line minimization police here: if you gave a shorter name ruff format might produce less lines, which i much prefer for readability.

@Hocuri Hocuri Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread src/download.rs Outdated
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Hocuri Hocuri Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Hocuri Hocuri Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented it as 5c2ae2a, because it was easy to implement, and there is one narrow edgecase where it helps

@link2xt

link2xt commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Looks good, but i noticed an existing related problem: #8941

This branch has not been deployed

No deployments
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