Skip to content

Commit a2efe90

Browse files
committed
relay-pool: register the auto-closing subscription before sending the REQ msg
Fixes #985 Tested-by: reya <[email protected]> Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 487c6df commit a2efe90

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

crates/nostr-relay-pool/src/relay/inner.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl InnerRelay {
295295
subscription.get(id).map(|d| d.filter.clone())
296296
}
297297

298-
async fn remove_subscription(&self, id: &SubscriptionId) {
298+
pub(super) async fn remove_subscription(&self, id: &SubscriptionId) {
299299
let mut subscriptions = self.atomic.subscriptions.write().await;
300300
subscriptions.remove(id);
301301
}
@@ -1378,10 +1378,6 @@ impl InnerRelay {
13781378
) {
13791379
let relay = self.clone(); // <-- FULL RELAY CLONE HERE
13801380
task::spawn(async move {
1381-
relay
1382-
.add_auto_closing_subscription(id.clone(), filter.clone())
1383-
.await;
1384-
13851381
// Check if CLOSE needed
13861382
let to_close: bool = match relay
13871383
.handle_auto_closing(&id, &filter, opts, notifications, &activity)
@@ -1823,15 +1819,24 @@ impl InnerRelay {
18231819
}
18241820

18251821
let filter = Filter::new().ids(ids);
1826-
self.send_msg(ClientMessage::Req {
1822+
let msg: ClientMessage = ClientMessage::Req {
18271823
subscription_id: Cow::Borrowed(down_sub_id),
18281824
filter: Cow::Borrowed(&filter),
1829-
})?;
1825+
};
18301826

18311827
// Register an auto-closing subscription
1832-
self.add_auto_closing_subscription(down_sub_id.clone(), filter)
1828+
self.add_auto_closing_subscription(down_sub_id.clone(), filter.clone())
18331829
.await;
18341830

1831+
// Send msg
1832+
if let Err(e) = self.send_msg(msg) {
1833+
// Remove previously added subscription
1834+
self.remove_subscription(down_sub_id).await;
1835+
1836+
// Propagate error
1837+
return Err(e);
1838+
}
1839+
18351840
*in_flight_down = true;
18361841

18371842
Ok(())

crates/nostr-relay-pool/src/relay/mod.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,14 @@ impl Relay {
490490
filter: Filter,
491491
opts: SubscribeOptions,
492492
) -> Result<(), Error> {
493-
// Check if auto-close condition is set
493+
// Check if the auto-close condition is set
494494
match opts.auto_close {
495-
Some(opts) => self.subscribe_auto_closing(id, filter, opts, None),
495+
Some(opts) => self.subscribe_auto_closing(id, filter, opts, None).await,
496496
None => self.subscribe_long_lived(id, filter).await,
497497
}
498498
}
499499

500-
fn subscribe_auto_closing(
500+
async fn subscribe_auto_closing(
501501
&self,
502502
id: SubscriptionId,
503503
filter: Filter,
@@ -513,8 +513,19 @@ impl Relay {
513513
// Subscribe to notifications
514514
let notifications = self.inner.internal_notification_sender.subscribe();
515515

516+
// Register the auto-closing subscription
517+
self.inner
518+
.add_auto_closing_subscription(id.clone(), filter.clone())
519+
.await;
520+
516521
// Send REQ message
517-
self.inner.send_msg(msg)?;
522+
if let Err(e) = self.inner.send_msg(msg) {
523+
// Remove previously added subscription
524+
self.inner.remove_subscription(&id).await;
525+
526+
// Propagate error
527+
return Err(e);
528+
}
518529

519530
// Spawn auto-closing handler
520531
self.inner
@@ -571,7 +582,8 @@ impl Relay {
571582

572583
// Subscribe
573584
let id: SubscriptionId = SubscriptionId::generate();
574-
self.subscribe_auto_closing(id, filter, opts, Some(tx))?;
585+
self.subscribe_auto_closing(id, filter, opts, Some(tx))
586+
.await?;
575587

576588
// Handle subscription activity
577589
while let Some(activity) = rx.recv().await {

0 commit comments

Comments
 (0)