Skip to content

relay-pool: don't send ws close msg if the stream has been terminated #987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/nostr-relay-pool/src/relay/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::policy::AdmitStatus;
use crate::pool::RelayPoolNotification;
use crate::relay::status::AtomicRelayStatus;
use crate::shared::SharedState;
use crate::transport::error::TransportError;
use crate::transport::websocket::{BoxSink, BoxStream};

type ClientMessageJson = String;
Expand Down Expand Up @@ -742,7 +743,15 @@ impl InnerRelay {
// Message sender handler
res = self.sender_message_handler(&mut ws_tx, rx_nostr, &ping) => match res {
Ok(()) => tracing::trace!(url = %self.url, "Relay sender exited."),
Err(e) => tracing::error!(url = %self.url, error = %e, "Relay sender exited with error.")
// Sink error!
Err(Error::Transport(TransportError::Sink(e))) => {
tracing::error!(url = %self.url, error = %e, "Relay sender exited with sink error.");

// The sink returned an error, so an additional call on the sink (like closing the sink) will cause a panic.
// Issue: https://github.com/rust-nostr/nostr/issues/984
return;
},
Err(e) => tracing::error!(url = %self.url, error = %e, "Relay sender exited with error."),
},
// Message receiver handler
res = self.receiver_message_handler(ws_rx, &ping, ingester_tx) => match res {
Expand Down
28 changes: 28 additions & 0 deletions crates/nostr-relay-pool/src/transport/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use core::fmt;
pub enum TransportError {
/// An error happened in the underlying backend.
Backend(Box<dyn std::error::Error + Send + Sync>),
/// Sink error
Sink(Box<dyn std::error::Error + Send + Sync>),
/// Stream error
Stream(Box<dyn std::error::Error + Send + Sync>),
}

impl std::error::Error for TransportError {}
Expand All @@ -19,6 +23,8 @@ impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Backend(e) => write!(f, "{e}"),
Self::Sink(e) => write!(f, "{e}"),
Self::Stream(e) => write!(f, "{e}"),
}
}
}
Expand All @@ -34,4 +40,26 @@ impl TransportError {
{
Self::Backend(Box::new(error))
}

/// Create a new sink error
///
/// Shorthand for `Error::Sink(Box::new(error))`.
#[inline]
pub fn sink<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Sink(Box::new(error))
}

/// Create a new stream error
///
/// Shorthand for `Error::Stream(Box::new(error))`.
#[inline]
pub fn stream<E>(error: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self::Stream(Box::new(error))
}
}
4 changes: 2 additions & 2 deletions crates/nostr-relay-pool/src/transport/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl WebSocketTransport for DefaultWebsocketTransport {

// Split sink and stream
let (tx, rx) = socket.split();
let sink: BoxSink = Box::new(tx.sink_map_err(TransportError::backend)) as BoxSink;
let stream: BoxStream = Box::new(rx.map_err(TransportError::backend)) as BoxStream;
let sink: BoxSink = Box::new(tx.sink_map_err(TransportError::sink)) as BoxSink;
let stream: BoxStream = Box::new(rx.map_err(TransportError::stream)) as BoxStream;
Ok((sink, stream))
})
}
Expand Down