You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RelayManager does not completely manage relays #1582
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.
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
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?
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
App::unwrap_relay_or_else_fetch (v2/mod.rs)
unwrap_ohttp_keys_or_else_fetch (free fn in ohttp.rs)
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:
replace std Mutex with tokio Mutex
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.
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.
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.
#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:
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.
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
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
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).
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
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.
Activity
vadim-anfv commentedon May 28, 2026
I'd like to pick this one up.
From a quick look, the relay selection logic currently lives in three places outside
RelayManager:App::unwrap_relay_or_else_fetch(v2/mod.rs)unwrap_ohttp_keys_or_else_fetch(free fn in ohttp.rs)fetch_ohttp_keys(free fn in ohttp.rs)Plan is to move all three into
RelayManagerso 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?
benalleng commentedon May 28, 2026
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
DanGould commentedon May 28, 2026
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?
DanGould commentedon May 28, 2026
Leaving another comment @va-an because I'd like Ben to clarify, I think his comment could be interpreted in at least 2 ways
benalleng commentedon May 28, 2026
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
RelayManagervadim-anfv commentedon May 31, 2026
Turns out we can't move these into
RelayManagermethods as-is: with relay_manager asArc<std::sync::Mutex<RelayManager>>, calling an async&mut selfmethod would hold theMutexGuardacross.await, but stdMutexGuardis!Send- won't compile.Two options:
RelayManagervia an inner struct, like Client in reqwestI 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
RelayManagermethods, plus removeArc<Mutex<...>>for relay_manager inAppand update call sites.benalleng commentedon Jun 1, 2026
Ok, yeah I don't want to swap to using tokio Mutex if we can avoid it. Looking forward to seeing the PR
benalleng commentedon Jun 1, 2026
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.vadim-anfv commentedon Jun 1, 2026
@benalleng makes sense, thanks for the heads-up on #1399. After it lands the task becomes much easier -
RelayManageris already a local&mut, so the inner-struct approach isn't needed, just plain&mut selfasync methods.Will continue on top of #1399.
vadim-anfv commentedon Jun 2, 2026
Done on a branch in my fork - I'll open the PR once #1399 is merged.
vadim-anfv commentedon Jun 9, 2026
#1399 removed the
Arc<Mutex<>>wrapper aroundRelayManager, which made moving the functions into methods trivial. But #1399 was superseded by #1628, whereRelayManagerstayed wrapped inArc<Mutex<RelayManager>>. So the original problem stands - a&selfasync method holds the outerMutexGuardacross.await.std::sync::MutexGuardis!Send, so the future isn'tSendand 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 theArc<Mutex<>>off the whole object and onto that field: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 isSend(sinceMutex: Sync). The functionss become&selfmethods and it compiles. Bonus: no external locking needed, andCloneis cheap viaArc.Current plan:
Arc<Mutex<>>wrapper aroundRelayManagerArc<Mutex<>>on thefailed_relaysfield instead&selfmethods onRelayManager@benalleng happy to proceed with this approach if it looks good.
benalleng commentedon Jun 9, 2026
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_relaysbenalleng commentedon Jun 9, 2026
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
vadim-anfv commentedon Jun 9, 2026
Yes, it will behave the same as now - the change doesn't alter the semantics, it's jst a code reorganization.
Currently a single
RelayManageris created for the lifetime of the cli command asArc<Mutex<RelayManager>>. In PR the arc mutex wrapper moves insideRelayManager, but in both variants there stays one sharedfailed_relayslist for the whole cli run.I think within the cli comand (the
RelayManagerlifetime) we should sharefailed_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 fromfailed_relays.I think you've highlighted an important point -
failed_relaysonly grows and is never freed. But this list has to live somewhere either way - either inRelayManageror in the caller outside of it - and it feels more logical forRelayManagerto own it. So I'd keep it inRelayManageras it is now, and treat the "never freed" part as a separate improvement (retry/eviction).benalleng commentedon Jun 10, 2026
I want to answer the final question on whether we think the new function
post_via_relayshould live inside of theRelayManageror not before closing this issuerust-payjoin/payjoin-cli/src/app/v2/mod.rs
Lines 1056 to 1076 in b28a1d8
benalleng commentedon Jun 10, 2026
IMO I believe that the nature of having to also pull in
post_requestto RelayManager as a result is really a non-starter and thatpost_via_relayis ok where it is.Closing via #1635
If anyone has strong objections to also merge
post_via_relayfeel free to chime in