Skip to content

RelayManager does not completely manage relays #1582

Closed

Description

@DanGould

async fn unwrap_relay_or_else_fetch(

We're doing this relay health check outside of the relay manager. This means the selection seems to be happening in multiple places. If all of this business logic were abstracted in a single manager, I think it'd make more sense in our implementation and be easier for downstream implementations to reference. This came up in the bdk-cli persistence integration pr because @Mshehu5 implementing was using our reference, and this reference is confusing.

Activity

  1. added this to the payjoin-cli-1.0 milestone on May 27, 2026
  2. vadim-anfv commented on May 28, 2026

    @vadim-anfv

    I'd like to pick this one up.

    From a quick look, the relay selection logic currently lives in three places outside RelayManager:

    1. App::unwrap_relay_or_else_fetch (v2/mod.rs)
    2. unwrap_ohttp_keys_or_else_fetch (free fn in ohttp.rs)
    3. fetch_ohttp_keys (free fn in ohttp.rs)

    Plan is to move all three into RelayManager so callers just ask it for a relay / ohttp keys and never touch its internal state.

    Do you prefer a single PR for the whole move, or a separate PR per function?

  3. benalleng commented on May 28, 2026

    @benalleng
    Collaborator

    imo I think would be the most pragmatic to approach this in a single PR to best be able to manage the context. I would just note to take some extra care in the commits so that it is easy to track context across them

  4. DanGould commented on May 28, 2026

    @DanGould
    MemberAuthor

    Plan is to move all three into RelayManager so callers just ask it for a relay / ohttp keys and never touch its internal state.

    I suppose this makes sense to hits this issue specifically and in isolation (as a commit).

    #1328 is beyond the scope of this but related. However you choose to touch or ignore it, please ensure to share the motivations behind the moves you make

    @benalleng do you mean to tackle both this issue and #1328 in one PR? Or just to tackle the unification of the 3 separate locations in one PR?

  5. DanGould commented on May 28, 2026

    @DanGould
    MemberAuthor

    Leaving another comment @va-an because I'd like Ben to clarify, I think his comment could be interpreted in at least 2 ways

  6. benalleng commented on May 28, 2026

    @benalleng
    Collaborator

    Sorry, just responding to this #1582 (comment) specifically, I think that it would be helpful to look over #1328 but the PR i am hoping to see should only deal with moving the following under the RelayManager

    1. App::unwrap_relay_or_else_fetch (v2/mod.rs)
    2. unwrap_ohttp_keys_or_else_fetch (free fn in ohttp.rs)
    3. fetch_ohttp_keys (free fn in ohttp.rs)
  7. vadim-anfv commented on May 31, 2026

    @vadim-anfv

    Turns out we can't move these into RelayManager methods as-is: with relay_manager as Arc<std::sync::Mutex<RelayManager>>, calling an async &mut self method would hold the MutexGuard across .await, but std MutexGuard is !Send - won't compile.

    Two options:

    1. replace std Mutex with tokio Mutex
    2. push the Mutex inside RelayManager via an inner struct, like Client in reqwest

    I prefer the second option, because the API is cleaner for the caller, especially if we push the Arc inside as well - no need to wrap in Arc<Mutex<...>> from the outsid, and relay_manager can be cloned freely.

    @benalleng going with the second option, the scope is the same - move 3 functions to RelayManager methods, plus remove Arc<Mutex<...>> for relay_manager in App and update call sites.

  8. benalleng commented on Jun 1, 2026

    @benalleng
    Collaborator

    Ok, yeah I don't want to swap to using tokio Mutex if we can avoid it. Looking forward to seeing the PR

  9. benalleng commented on Jun 1, 2026

    @benalleng
    Collaborator

    So I remembered that we are ultimately trying to move away from RelayManager handling state in #1399 so instead of creating more complexity with a Arc<Mutex<RelayManagerInner>> that we will need to rebase out later I am going to try and get #1399 merged and unblock this. I recommended @va-an to take a look at that PR in its current state to build on.

  10. vadim-anfv commented on Jun 1, 2026

    @vadim-anfv

    So I remembered that we are ultimately trying to move away from RelayManager handling state in #1399 so instead of creating more complexity with a Arc<Mutex<RelayManagerInner>> that we will need to rebase out later I am going to try and get #1399 merged and unblock this. I recommended @va-an to take a look at that PR in its current state to build on.

    @benalleng makes sense, thanks for the heads-up on #1399. After it lands the task becomes much easier - RelayManager is already a local &mut, so the inner-struct approach isn't needed, just plain &mut self async methods.

    Will continue on top of #1399.

  11. vadim-anfv commented on Jun 2, 2026

    @vadim-anfv

    Done on a branch in my fork - I'll open the PR once #1399 is merged.

  12. vadim-anfv commented on Jun 9, 2026

    @vadim-anfv

    #1399 removed the Arc<Mutex<>> wrapper around RelayManager, which made moving the functions into methods trivial. But #1399 was superseded by #1628, where RelayManager stayed wrapped in Arc<Mutex<RelayManager>>. So the original problem stands - a &self async method holds the outer MutexGuard across .await. std::sync::MutexGuard is !Send, so the future isn't Send and doesn't compile (see #1582 (comment)).

    After #1628 only one field needs synchronization (failed_relays), so a ful inner struct isn't necessary - it's enough to move the Arc<Mutex<>> off the whole object and onto that field:

    #[derive(Debug, Clone)]
    pub struct RelayManager {
        config: Config,
        failed_relays: Arc<Mutex<Vec<Url>>>,
    }

    Now the guard is acquired and released inside the synchronous methods and never lives across .await. The only thing held across the boundary is &RelayManager, which is Send (since Mutex: Sync). The functionss become &self methods and it compiles. Bonus: no external locking needed, and Clone is cheap via Arc.

    Current plan:

    • Drop the Arc<Mutex<>> wrapper around RelayManager
    • Put Arc<Mutex<>> on the failed_relays field instead
    • Move the functions into &self methods on RelayManager

    @benalleng happy to proceed with this approach if it looks good.

  13. benalleng commented on Jun 9, 2026

    @benalleng
    Collaborator

    I think this is correct. Go ahead and make a PR so I can look more closely at the code to try and parse the new behavior with dropping Arc<Mutex<>> from RelayManager and adding it to failed_relays

  14. benalleng commented on Jun 9, 2026

    @benalleng
    Collaborator

    just thinking does the failed_relay list get shared across sessions with this logic?

    Should we be sharing the failed_relay list across sessions or should we make sure each session starts with a fresh set of relays to check against in the case of a transient error?

    Does it even matter? I would like your thoughts @va-an

  15. vadim-anfv commented on Jun 9, 2026

    @vadim-anfv

    just thinking does the failed_relay list get shared across sessions with this logic?

    Yes, it will behave the same as now - the change doesn't alter the semantics, it's jst a code reorganization.
    Currently a single RelayManager is created for the lifetime of the cli command as Arc<Mutex<RelayManager>>. In PR the arc mutex wrapper moves inside RelayManager, but in both variants there stays one shared failed_relays list for the whole cli run.



    Should we be sharing the failed_relay list across sessions or should we make sure each session starts with a fresh set of relays to check against in the case of a transient error?

    I think within the cli comand (the RelayManager lifetime) we should share failed_relays, as it is now - otherwise each session would independently hit an unavailable relay and we'd get repeated error log entries about the same relay from every session. That said, it's worth considering adding retry logic and/or a mechanism to evict entries from failed_relays.



    Does it even matter?

    I think you've highlighted an important point - failed_relays only grows and is never freed. But this list has to live somewhere either way - either in RelayManager or in the caller outside of it - and it feels more logical for RelayManager to own it. So I'd keep it in RelayManager as it is now, and treat the "never freed" part as a separate improvement (retry/eviction).

  16. benalleng commented on Jun 10, 2026

    @benalleng
    Collaborator

    I want to answer the final question on whether we think the new function post_via_relay should live inside of the RelayManager or not before closing this issue

    async fn post_via_relay<F, T, E>(&self, mut build: F) -> Result<(reqwest::Response, T)>
    where
    F: FnMut(&str) -> std::result::Result<(payjoin::Request, T), E>,
    E: Into<anyhow::Error>,
    {
    loop {
    let relay =
    self.relay_manager.lock().expect("Lock should not be poisoned").choose_relay()?;
    let (req, ctx) = build(relay.as_str()).map_err(Into::into)?;
    match self.post_request(req).await {
    Ok(resp) => return Ok((resp, ctx)),
    Err(e) => {
    tracing::debug!("Request to relay {relay} failed: {e:?}");
    self.relay_manager
    .lock()
    .expect("Lock should not be poisoned")
    .add_failed_relay(relay);
    }
    }
    }
    }

  17. benalleng commented on Jun 10, 2026

    @benalleng
    Collaborator

    IMO I believe that the nature of having to also pull in post_request to RelayManager as a result is really a non-starter and that post_via_relay is ok where it is.

    Closing via #1635

    If anyone has strong objections to also merge post_via_relay feel free to chime in

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions