Skip to content

Commit b6c0351

Browse files
committed
relay-pool: don't send ws close msg if the stream has been terminated
Don't send the WebSocket close message if the stream has been terminated. Fixes #984 Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
1 parent baeba7b commit b6c0351

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::policy::AdmitStatus;
3737
use crate::pool::RelayPoolNotification;
3838
use crate::relay::status::AtomicRelayStatus;
3939
use crate::shared::SharedState;
40+
use crate::transport::error::TransportError;
4041
use crate::transport::websocket::{BoxSink, BoxStream};
4142

4243
type ClientMessageJson = String;
@@ -742,7 +743,15 @@ impl InnerRelay {
742743
// Message sender handler
743744
res = self.sender_message_handler(&mut ws_tx, rx_nostr, &ping) => match res {
744745
Ok(()) => tracing::trace!(url = %self.url, "Relay sender exited."),
745-
Err(e) => tracing::error!(url = %self.url, error = %e, "Relay sender exited with error.")
746+
// Sink error!
747+
Err(Error::Transport(TransportError::Sink(e))) => {
748+
tracing::error!(url = %self.url, error = %e, "Relay sender exited with sink error.");
749+
750+
// The sink returned an error, so an additional call on the sink (like closing the sink) will cause a panic.
751+
// Issue: https://github.com/rust-nostr/nostr/issues/984
752+
return;
753+
},
754+
Err(e) => tracing::error!(url = %self.url, error = %e, "Relay sender exited with error."),
746755
},
747756
// Message receiver handler
748757
res = self.receiver_message_handler(ws_rx, &ping, ingester_tx) => match res {

crates/nostr-relay-pool/src/transport/error.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use core::fmt;
1111
pub enum TransportError {
1212
/// An error happened in the underlying backend.
1313
Backend(Box<dyn std::error::Error + Send + Sync>),
14+
/// Sink error
15+
Sink(Box<dyn std::error::Error + Send + Sync>),
16+
/// Stream error
17+
Stream(Box<dyn std::error::Error + Send + Sync>),
1418
}
1519

1620
impl std::error::Error for TransportError {}
@@ -19,6 +23,8 @@ impl fmt::Display for TransportError {
1923
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2024
match self {
2125
Self::Backend(e) => write!(f, "{e}"),
26+
Self::Sink(e) => write!(f, "{e}"),
27+
Self::Stream(e) => write!(f, "{e}"),
2228
}
2329
}
2430
}
@@ -34,4 +40,26 @@ impl TransportError {
3440
{
3541
Self::Backend(Box::new(error))
3642
}
43+
44+
/// Create a new sink error
45+
///
46+
/// Shorthand for `Error::Sink(Box::new(error))`.
47+
#[inline]
48+
pub fn sink<E>(error: E) -> Self
49+
where
50+
E: std::error::Error + Send + Sync + 'static,
51+
{
52+
Self::Sink(Box::new(error))
53+
}
54+
55+
/// Create a new stream error
56+
///
57+
/// Shorthand for `Error::Sink(Box::new(error))`.
58+
#[inline]
59+
pub fn stream<E>(error: E) -> Self
60+
where
61+
E: std::error::Error + Send + Sync + 'static,
62+
{
63+
Self::Stream(Box::new(error))
64+
}
3765
}

crates/nostr-relay-pool/src/transport/websocket.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ impl WebSocketTransport for DefaultWebsocketTransport {
9494

9595
// Split sink and stream
9696
let (tx, rx) = socket.split();
97-
let sink: BoxSink = Box::new(tx.sink_map_err(TransportError::backend)) as BoxSink;
98-
let stream: BoxStream = Box::new(rx.map_err(TransportError::backend)) as BoxStream;
97+
let sink: BoxSink = Box::new(tx.sink_map_err(TransportError::sink)) as BoxSink;
98+
let stream: BoxStream = Box::new(rx.map_err(TransportError::stream)) as BoxStream;
9999
Ok((sink, stream))
100100
})
101101
}

0 commit comments

Comments
 (0)